Text skew detection and correction in image
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1199533/image.jpeg)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1199538/image.png)
Rotate orgin image angle= 30
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1199543/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1199548/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1199553/image.png)
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.
0 Kommentare
Antworten (1)
cr
am 19 Nov. 2022
Bearbeitet: cr
am 19 Nov. 2022
In general, pattern maching is used. See estimateGeometricTransform()
But the result may not be exactly the angle it's rotated to.
2 Kommentare
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.
Siehe auch
Kategorien
Mehr zu 3-D Volumetric Image Processing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!