Text skew detection and correction in image
조회 수: 2 (최근 30일)
이전 댓글 표시
![](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
답변 (1개)
cr
2022년 11월 19일
편집: cr
2022년 11월 19일
In general, pattern maching is used. See estimateGeometricTransform()
But the result may not be exactly the angle it's rotated to.
댓글 수: 2
Image Analyst
2022년 11월 22일
@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.
참고 항목
카테고리
Help Center 및 File Exchange에서 3-D Volumetric Image Processing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!