将图像裁剪成几个矩形

时间:2014-05-16 19:27:36

标签: matlab image-processing

我需要自动处理像这样enter image description here的灰度图像并将它们裁剪成矩形图块。我使用contour()enter image description here来查找边缘但是如何使用轮廓结果裁剪图像? 非常感谢!

2 个答案:

答案 0 :(得分:0)

假设您的图像已经是双灰度(否则使用im2doublergb2gray),您可以使用以下方法创建一个简单的二进制掩码:

IM %your image
M=IM>.5

这意味着对于大于.5的所有值,掩码包含true。要选择这些像素,请使用:

IM(M)

要选择相反的设置,请使用:

IM(~M)

答案 1 :(得分:0)

我认为基本算法会有点像这样:

1) recognize all horizontal lines
2) recognize all vertical lines
3) create a grid using all the lines from (1) and (2) - this will create more rectangles than you ultimately want, but each rectangle will be a single color
4) combine rectangles that are the same color and adjacent in one direction
5) combine the results of (4) in the other direction, possibly splitting some of the earlier combinations to come up with more a more optimal solution (e.g. in your example the two white rectangles in the first row and the three in the second row could be combined into a group of 2x2 rectangles by splitting the three into 1+2, giving you a 2x2 and a 1x1 rectangle, rather than a 2x1 and a 3x1).