问题描述
我有一个 10x10
二进制位图,如下所示.我正在寻找一种有效的方法来在 MATLAB 中找到它的轮廓.(我曾尝试让每个值环顾"其邻居的值并做出决定,但效率太低.我希望算法能够扩大规模.)
I have a 10x10
binary bit map as follows. I am looking for an efficient way of finding its contour in MATLAB. (I have tried letting every value "look around" its neighbors' values and decide, but it is too inefficient. I expect the algorithm to scale up.)
false false false false false false false false false false
false false true true true true true true false false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false false true true true true true true false false
false false false false false false false false false false
让我们假设每个布尔值类似于一个正方形,左下角的值位于 x: 0-1;y:0-1
.输出应该是形成边界的点.你可以假设内部的 true
块总是凸的.
Let's assume each boolean value resembles a square, and the left-bottom one sits over x: 0-1; y: 0-1
. The output should be the points that form the boundary. You may assume the inner true
block is alway convex.
推荐答案
这太简单了.在 MATLAB 中使用 bwperim
命令,假设您有图像处理工具箱.
This is dead simple. Use the bwperim
command in MATLAB, assuming you have the image processing toolbox.
你可以这样调用函数:
out = bwperim(A); %//or
out = bwperim(A,conn);
第一种方法假设像素连通性是一个 4 像素的邻域.这只会查看北、南、东和西方向.
The first way assumes that the pixel connectivity is a 4-pixel neighbourhood. This will only look at the north, south, east and west directions.
如果您指定一个名为 conn
的附加参数,它是单个数字,您可以覆盖此行为并指定查看相邻像素时所需的行为类型.例如,如果 conn=8
,您将查看 2D 的 8 像素邻域(因此 N、NE、E、SE、S、SW、W、NW),或者您可以进入 3D如果你有一个 3D 二进制图像......但现在,我假设它只是 2D.为获得最佳精度,请使用 8.
If you specify an additional parameter called conn
which is a single number, you can override this behaviour and specify the kind of behaviour you want when looking at neighbouring pixels. For example, if conn=8
, you would look at 8-pixel neighbourhoods for 2D (so N, NE, E, SE, S, SW, W, NW), or you can go into 3D if you have a 3D binary image... but for now, I'm assuming it's just 2D. For the best accuracy, use 8.
因此,我们有:
A = [false false false false false false false false false false
false false true true true true true true false false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false false true true true true true true false false
false false false false false false false false false false];
out = bwperim(A,8);
它看起来像:
out =
0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0
0 1 1 0 0 0 0 1 1 0
0 1 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 1 0
0 1 1 0 0 0 0 1 1 0
0 0 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0
MATLAB 输出 1 为真,0 为假.
MATLAB outputs 1 for true and 0 for false.
作为奖励,这就是并排的形状:
As a bonus, this is what the shapes look like side by side:
根据您的评论,您希望找到构成周长的点集.因此,您只需使用 find
命令即可.
Going with your comments, you wish to find the set of points that make the perimeter. As such, you can simply use the find
command to do that for you.
[X,Y] = find(out == 1);
coords = [X Y];
find
命令的作用是搜索您的数组并在数组中查找与find
参数中给出的布尔表达式匹配的位置.在这种情况下,我们希望找到 out
中的像素等于 1 的所有坐标,并且 out
是我们的周边图像.因此,这可以有效地找到所有属于周边像素的像素.
What the find
command does is that it searches your array and finds locations in the array that match the Boolean expression given in the parameter of find
. In this case, we wish to find all co-ordinates that have a pixel in out
equal to 1, and out
is our perimeter image. As such, this effectively finds all pixels that are perimeter pixels.
我们得到:
coords =
3 2
4 2
5 2
6 2
7 2
8 2
2 3
3 3
8 3
9 3
2 4
9 4
2 5
9 5
2 6
9 6
2 7
9 7
2 8
3 8
8 8
9 8
3 9
4 9
5 9
6 9
7 9
8 9
X
是行坐标,而 Y
是列坐标.我已将 X
和 Y
放入单个 2D 数组中以获得更好的展示效果,但您可以使用 X
和 Y
变量自己做进一步处理.
X
are the row co-ordinates, while Y
are the column co-ordinates. I've placed X
and Y
into a single 2D array for better presentation, but you can take the X
and Y
variables by themselves for further processing.
希望这有帮助!
这篇关于MATLAB - 查找二进制位图的轮廓?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!