Dania
Dania

Reputation: 1688

Detecting the geometry of irregular regions in an image in MATLAB

I have a 2D image that has been produced as the result of applying k-means clustering to an original image. The clustering was applied to detect different color regions in the original image. Here is one example of the resulting clusters,

enter image description here

The colored region (purple) is the shape of the cluster in original the 2D image. I want to be able to detect (and store) the shape of this cluster and apply it to another picture. To elaborate more, after capturing this shape I want to draw (or construct) a region with a similar shape in another image and apply the same color to it.

I have searched for this detecting ability in MATLAB, but most of the algorithms I found detect regular shapes like squares, and circles. I tried to use active activecontour as suggested by the answer here, Random object detection matlab. Based on the first code provided at MATLAB website here, http://www.mathworks.com/help/images/ref/activecontour.html, I've written this code,

I = imread('clusterimg.png');
imshow(I)
title('Original Image');

mask = zeros(size(I));
mask(25:end-25,25:end-25) = 1;
  
figure, imshow(mask);
title('Initial Contour Location');

bw = activecontour(I,mask,300);
  
figure, imshow(bw);
title('Segmented Image');

But, when I run it I get this error,

Undefined function 'activecontour' for input arguments of type 'uint8'.
Error in geometrydetection (line 11)
bw = activecontour(I,mask,300); 

What I need to know is the following,

1- What mistake I did in the code above?

2- Is activecontour a good way to achieve the geometry detection I described above? If yes, after detecting the geometry of a region, how can I construct a similar one in another image? If no, can you please provided an alternative method that can detect the geometry of a region, and construct a similar one in another image?

EDIT: The suggestion of using bwconncomp by mvai may be good. I've found this example (available here, http://www.mathworks.com/help/images/labeling-and-measuring-objects-in-a-binary-image.html),

cc = bwconncomp(BW)
cc = 

    Connectivity: 8
       ImageSize: [8 9]
      NumObjects: 3
    PixelIdxList: {[6x1 double]  [6x1 double]  [5x1 double]}

Can anyone please tell me if there is a way to know the distance that separates every component from the another one?

Thank You.

Upvotes: 1

Views: 1426

Answers (1)

natario
natario

Reputation: 25194

The function bwconncomp might help. It looks for connected components in a binary image.

CC = bwconncomp(I,connectivity);

From CC you can get the number of regions and their indexes, see the documentation for help.

The connectivity parameter, for 2D images, is basically a 3x3 matrix of 0s and 1s. It represents the neighborhood locations in respect to the central pixel. If you use the scalar value 4, the resulting matrix will be:

0  1  0
1  1  1
0  1  0

while 8 is:

1  1  1
1  1  1
1  1  1

Since your regions don't have particular shapes, I believe the default (8) is ok for you.

Upvotes: 1

Related Questions