Text skew detection and correction in image

clc;clear all; close all;
a=imread('eq2.jpg');
figure;
imshow(a)
if size(a,3)==3
a=rgb2gray(a);
end
b=im2bw(a);
ar=imrotate_white(b,30);
figure
imshow(ar);
br=~(ar);
figure
imshow(br)
[r c]=size(b);
X=[];
for i=1:r
for j=1:c
if br(i,j)==1;
X=[X; i j];
end
end
end
size(X);
[coeff, score, latent]=pca(X);
figure;
biplot(coeff(:,1:2),'Scores',score(:,1:2),'VarLabels',{'v_1','v_2'});
r=sqrt((coeff(1,1))^2+(coeff(1,2))^2);
th=real(double(vpa(acos(coeff(1,1)/r)*180/pi)))
syms x
th=real(double(vpa(solve(x==th-90))))
rotatedImage=imrotate(br,-th);
figure
imshow(rotatedImage)
function rotated_image = imrotate_white(image, rot_angle_degree)
RA = imref2d(size(image));
tform = affine2d([cosd(rot_angle_degree) -sind(rot_angle_degree) 0; ...
sind(rot_angle_degree) cosd(rot_angle_degree) 0; ...
0 0 1]);
Rout = images.spatialref.internal.applyGeometricTransformToSpatialRef(RA,tform);
Rout.ImageSize = RA.ImageSize;
xTrans = mean(Rout.XWorldLimits) - mean(RA.XWorldLimits);
yTrans = mean(Rout.YWorldLimits) - mean(RA.YWorldLimits);
Rout.XWorldLimits = RA.XWorldLimits+xTrans;
Rout.YWorldLimits = RA.YWorldLimits+yTrans;
rotated_image = imwarp(image, tform, 'OutputView', Rout, 'interp', 'cubic', 'fillvalues', 255);
end
The output is
Rotate orgin image angle= 30
The answer is angle = 25.1508 !!!!
The angle must be 30 .
How do I solve this problem?
This problem appears to me in the rational equations only, but in the linear equations the angle is shown with high accuracy.

Antworten (1)

cr
cr am 19 Nov. 2022
Bearbeitet: cr am 19 Nov. 2022

0 Stimmen

In general, pattern maching is used. See estimateGeometricTransform()
But the result may not be exactly the angle it's rotated to.

2 Kommentare

Yahya
Yahya am 22 Nov. 2022
Not useful :( ,thanks for answering
Image Analyst
Image Analyst am 22 Nov. 2022
@Yahya Then why did you accept the answer?
In general, for an arbitrary, random set of blobs, there is no way to determine what is the "correct" skew and orientation angle.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 19 Nov. 2022

Kommentiert:

am 22 Nov. 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by