英文:
Binary mask of RGB image in Matlab
问题
我试图获得一个具有白色背景和黑色图案的RGB图像的蒙版(这朵花),但我不明白哪里有问题...
flower = imread('flower.png'); %合并两个图像
mask = zeros(size(flower(:,:,1)));
mask(25:end-25,25:end-25) = 1;
bw = activecontour(flower, mask, 600);
imshow(bw)
英文:
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?
答案1
得分: 3
根据 activecontour
的文档:
如果对象区域的灰度强度明显不同,那么"Chan-Vese"方法可能无法分割图像中的所有对象。例如,如果图像包含比背景更亮和更暗的对象,"Chan-Vese"方法通常只会分割出暗或亮的对象之一。
我不了解"Chan-Vese"方法的算法,但看起来将对象与背景比较亮可能有助于算法区分对象和背景。
由于我们知道背景是黑色,我们可以简单地使用 imbinarize
:
bw = imbinarize(rgb2gray(flower), 0);
假设对象包括一些暗区域,我们可以使用 imadjust 使其变亮。将gamma参数设置为较低的值会使对象更亮,同时保持背景为黑色。
我们还可以通过设置 low_in
参数将一些较低的像素值变为零:
bright_flower = imadjust(flower, [0.05, 1], [0, 1], 0.3); % low_in = 0.05 将低于0.05*255的值(约为13)裁剪为零。
bw = activecontour(bright_flower, mask, 600);
为了获得更好的结果,我们可以使用 imbinarize
来初始化掩模:
mask = imbinarize(rgb2gray(flower), 0);
代码示例:
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)
我们可以使用 imopen
和 bwareaopen
来改善结果:
bw = imopen(bw, ones(2));
bw = bwareaopen(bw, 50, 4);
白色背景和黑色图形:
bw = not(bw);
注意:
上述问题中的 img_addition
不清楚是什么。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论