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:
which should rotated give following 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) 



Comments
Post a Comment