matlab - How to straighten a tilted square shape in an image? -


how can straighten tilted square shape in image? not know angle tilted , code must calculate , rotate automatically.

for example, have following image:

input_image

which should rotated give following output image:

output_image

a simple way using top , bottom corners. note approach relies on upper , lower corners:

i = imread('sq.jpg'); i_bw = im2bw(i)==0; % modify strel required se = strel('square', 10); i_ed = imopen(i_bw, se);  limits = sum(i_ed, 2);  top_y = find(limits>0, 1); bottom_y = find(limits>0, 1, 'last'); top_x = round(mean(find(i_ed(top_y, :)>0))); bottom_x = round(mean(find(i_ed(bottom_y, :)>0)));      slope = -1 * (top_y - bottom_y)/(top_x - bottom_x); rot_angle = 2 * pi * atan(slope);  i2 = imrotate(i, -rot_angle); imshow(i2) 

before before_sq

after after_sq


Comments

Popular posts from this blog

php - isset function not working properly -

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -