问题描述
我有一个 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
假设每个布尔值类似于一个正方形,而左下角的一个坐在<$ c $之上c> 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.
您可以像这样调用函数:
You can call the function like so:
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
是列co-坐标。我已将 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 - 查找二进制位图的轮廓?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!