Search code examples
matlabmaskimage-segmentation

How to create a mask to segment a color image by activecontour?


I want to use the function activecontour in matlab to segment a color image, but I don't know how to create the mask.

The documentation says:

For color and multi-channel images, mask must be a 2-D logical array where the first two dimensions match the first two dimensions of the image A.

But I don't understand what has to be done. Any suggestions?


Solution

  • Let's consider that the size of your image is NxM pixels, N is the number of rows, M the number of columns.

    If it is a color image, each pixel is probably composed of 3 values, one for intensity of red (R), one intensity of blue (B) and one for intensity of green (G). These are called the color channels. So the real shape of the matrix representing your image is NxMx3.

    What the documentation says is that the mask should be 2-D, and the dimensions should match the first two dimension of your image. It means the mask should have the same number of rows and cols than your image, but each pixel of the mask is not composed of 3 values anymore. It is composed of 1 value (a logical value : 0 or 1).

    So what you need to do is to give the function a matrix NxM with only 0 and 1 as possible values. The doc says that the mask is the :

    Initial contour at which the evolution of the segmentation begins, specified as a binary image the same size as A.

    So the mask needs to represent an initial guess of the contour. If you already know that what you want to see is in the upper left corner of the image, you can set the initial contour as a square located in the upper left corner for example.

    Now to represent the contour by a matrix of logicals, you simply set all the elements of the matrix to 0 and just the elements representing the contour to 1 I guess.

    Lets me know if there is something you don't understand, I'd be glad to answer you.