Text skew detection and correction in image
Show older comments

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.
Answers (1)
In general, pattern maching is used. See estimateGeometricTransform()
But the result may not be exactly the angle it's rotated to.
2 Comments
Yahya
on 22 Nov 2022
Image Analyst
on 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.
Categories
Find more on Detect, Extract, and Match Features in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!