如何拉直图像中的倾斜方形? [英] How to straighten a tilted square shape in an image?
本文介绍了如何拉直图像中的倾斜方形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在图像中拉直倾斜的方形?
我不知道它倾斜的角度,代码必须计算它然后自动旋转它。
How can I straighten a tilted square shape in an image? I do not know the angle with which it is tilted and the code must calculate it and then rotate it automatically.
例如,我有以下图像:
应旋转以提供以下输出图像:
which should be rotated to give the following output image:
推荐答案
一种只使用顶角和底角的简单方法。请注意,此方法依赖于上下角:
A simple way using only the top and bottom corners. Note that this approach relies on the upper and lower most corners:
i = imread('sq.jpg');
i_bw = im2bw(i)==0;
% Modify the strel as 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
< a href =https://i.stack.imgur.com/Gq8qx.jpg =nofollow noreferrer>
AFTER
这篇关于如何拉直图像中的倾斜方形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文