开发者

How to draw the contour of the objects in label matrix

I have a label matrix of segmented image. For example,

1 1 1 2 2 2 3 3 3 4 4 4

1 1 1 2 2 2 3 3 3 4 4 4

1 1 1 2 2 2 3 3 3 4 4 4

1 1 1 2 2 2 3 3 3 4 4 4

1 1 1 2 2 2 3 3 3 4 4 4

1 1 1 2 2 2 3 3 3 4 4 4

1 1 1 2 2 2 3 3 3 4 4 4

1 1 1 2 2 2 3 3 3 4 4 4

Now I would like to draw contour of each object so tha开发者_运维知识库t the pixel value of edge of each object is 1 and other pixels value is 0.

Something like this

1 1 1 1 1 1 1 1 1 1 1 1

1 0 1 1 0 1 1 0 1 1 0 1

1 0 1 1 0 1 1 0 1 1 0 1

1 0 1 1 0 1 1 0 1 1 0 1

1 0 1 1 0 1 1 0 1 1 0 1

1 0 1 1 0 1 1 0 1 1 0 1

1 0 1 1 0 1 1 0 1 1 0 1

1 1 1 1 1 1 1 1 1 1 1 1

I hope there is a function which will do this in Matlab but I dont know.


The answer by @Laurent' allows you to co 4-connected labelling and works as long as the objects in your image are at least three pixels wide everywhere.

If your perimeter needs to be 8-connected, or if your objects can be narrow, you should look into repeated application of BWPERIM (assuming, again, that A is your image):

conn = 8; %# select connectedness
nLbl = max(A(:)); %# note this works even if some labels are missing

out = false(size(A)); %# preassign the output

for lbl = 1:nLbl
    %# get the perimeter for each object
    out = out | bwperim(A==lbl,connn);
end


Let A be your matrix, I think that this is what you're looking for:

(conv2(A,[1 -2 1],'same') ~=0)|(conv2(A,[1; -2; 1],'same') ~=0)

Here's the result with your example:

 1     1     1     1     1     1     1     1     1     1     1     1
 1     0     1     1     0     1     1     0     1     1     0     1
 1     0     1     1     0     1     1     0     1     1     0     1
 1     0     1     1     0     1     1     0     1     1     0     1
 1     0     1     1     0     1     1     0     1     1     0     1
 1     0     1     1     0     1     1     0     1     1     0     1
 1     0     1     1     0     1     1     0     1     1     0     1
 1     1     1     1     1     1     1     1     1     1     1     1


I can't remember matlab's syntax offhand, but I'd suggest an algorithm along the lines of a function that determines the maximum of values from x-1,y-1 to x+1,y+1, and subtracts the minimum of the same range. If that's 0, then you want a 0 output, otherwise 1.

You might need to tweak it slightly for the edge of the image, perhaps with whatever code gets the value of each pixel returning -1 for coordinates that are out of range, or something like that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜