How to make an image straightener like that of cam scanner app?

56 views (last 30 days)
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

Answers (2)

DGM on 20 Apr 2022
Edited: DGM on 20 Apr 2022
Dead post, I know, but you can use image transformation tools like imwarp() to do the perspective correction.
A = imread('');
% 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);
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
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)));

Clay Swackhamer
Clay Swackhamer on 17 Apr 2019
Edited: Clay Swackhamer on 17 Apr 2019
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);
  1 Comment
GMD Baloch
GMD Baloch on 19 Apr 2019
Thankyou for answering but you didn't get my point, I have an image given in attachment and I want to extract the whiteboard's pixels and then want to straight it like a rectangle

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!