I'm trying to get a mask of an RGB image (this flower) with white background and black figures but I don't understand what is not working...
flower= imread('flower.png'); %sum up the two images
mask = zeros(size(flower(:,:,1)));
mask(25:end-25,25:end-25) = 1;
bw = activecontour(img_addition,mask,600);
imshow(bw)
I get this instead of the full shape. What am I missing?
According to activecontour
documentation:
If object regions are of significantly different grayscale intensities, then the "Chan-Vese" method might not segment all objects in the image. For example, if the image contains objects that are brighter than the background and some that are darker, the "Chan-Vese" method typically segments out either the dark or the bright objects only.
I don't no the "Chan-Vese" method algorithm, but it looks like making the object brighter compared to the background may help the algorithm distinguish the object from the background.
Since we know that the background is black, we may simply use imbinarize
:
bw = imbinarize(rgb2gray(flower), 0);
Assuming the object includes some dark region we may make it brighter using imadjust.
Setting the gamma argument to low value, makes the object brighter, while keeping the background black.
We may also make some lower pixels go to zero by setting low_in
argument:
bright_flower = imadjust(flower, [0.05, 1], [0, 1], 0.3); % low_in = 0.05 clips values below 0.05*255 = about 13 to zero.
bw = activecontour(bright_flower, mask, 600);
For getting better results, we may use the imbinarize
for initializing the mask:
mask = imbinarize(rgb2gray(flower), 0);
Code sample:
flower = imread('flower.png');
mask = imbinarize(rgb2gray(flower), 0);
bright_flower = imadjust(flower, [0.05, 1], [0, 1], 0.3);
figure;imshow(bright_flower)
bw = activecontour(bright_flower, mask, 600);
figure;imshow(bw)
We may improve the result using imopen
and bwareaopen
:
bw = imopen(bw, ones(2));
bw = bwareaopen(bw, 50, 4);
White background and black figure:
bw = not(bw);
Note:
It's unclear what is img_addition
in the above question.