How to make an image straightener like that of cam scanner app?
64 次查看(过去 30 天)
显示 更早的评论
Hello, everyone, I want to make an application just like camScanner which lets the user to select the corners of the object and then straights the image according to those corners in such a way that the text is still readable. I need help in this context
0 个评论
回答(2 个)
DGM
2022-4-20
编辑:DGM
2022-4-20
Dead post, I know, but you can use image transformation tools like imwarp() to do the perspective correction.
A = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/214874/56260471_568057093678369_4947302863245148160_n.jpg');
% these are the coordinates of the box corners
% you can get these using getpts() or impixelinfo() or datatips
boxm = [28 3; % [x y]
31 959;
1297 783;
1343 195];
% assert that this is where they're supposed to be
% any coordinates that define a rectangle
boxf = [30 30; % [x y]
30 900;
1300 900;
1300 30];
TF = fitgeotrans(boxm,boxf,'projective');
outview = imref2d(size(A));
B = imwarp(A,TF,'fillvalues',255,'outputview',outview);
imshow(B)
Normally, the easy way would be to just find the corners of the board, but since they aren't visible, just pick points that define a quadrilateral whose sides are apparently parallel to the board edges.
After the transformation, you may elect to crop the result and do further post-processing. Gotta love that glare, ghosting, and the feeble smudges of half-dry markers. Reminds me why I can't stand whiteboards.
B = imcrop(B,[14 33 1332 869]); % crop it
B = imflatfield(B,40); % try to suppress the glare
B = imadjust(B,[0.4 0.65]); % adjust levels
imshow(B)
You might be able to clean it up a bit further with some morphological operations. The compression artifacts are really going to end up being a limiting factor. That, and some of the written text is no darker than the ghost text.
B = imcomplement(imbothat(B,ones(7)));
imshow(B)
Clay Swackhamer
2019-4-17
编辑:Clay Swackhamer
2019-4-17
Hi GMD,
Here is a bit of code where I implemented this type of thing. The objective was to straighten a calibration ruler so that it would be aligned with the image borders. I'll attach the image showing the scale before it was rotated and an image showing the rotated scale. The gist of the code is that I find the "hypotenuse" as in the diagonal line that connects the far corners of the scale, then depending on which way it is going I decide whether to rotate the image clockwise or counterclockwise. Since your implementation will be a bit different I'll skip the rest of the code but hopefully this gives you the main idea.
%Decide which way to rotate the image
if (slope > 0) %Look like negative slopes
angle_rotate = 45 - abs(angle_hypot);
field_rotated = imrotate(field, -angle_rotate);
elseif (slope <= 0) %Look like positive slopes
angle_rotate = 45 - abs(angle_hypot);
field_rotated = imrotate(field, angle_rotate);
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!