“Binary mask of RGB image in Matlab” can be translated to “Matlab中的RGB图像二值掩模”.

huangapple go评论55阅读模式
英文:

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...

“Binary mask of RGB image in Matlab” can be translated to “Matlab中的RGB图像二值掩模”.

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?

“Binary mask of RGB image in Matlab” can be translated to “Matlab中的RGB图像二值掩模”.

答案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)

我们可以使用 imopenbwareaopen 来改善结果:

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)

bright_flower:
“Binary mask of RGB image in Matlab” can be translated to “Matlab中的RGB图像二值掩模”.

bw:
“Binary mask of RGB image in Matlab” can be translated to “Matlab中的RGB图像二值掩模”.


We may improve the result using imopen and bwareaopen:

bw = imopen(bw, ones(2));
bw = bwareaopen(bw, 50, 4);

“Binary mask of RGB image in Matlab” can be translated to “Matlab中的RGB图像二值掩模”.


White background and black figure:

bw = not(bw);

“Binary mask of RGB image in Matlab” can be translated to “Matlab中的RGB图像二值掩模”.


Note:
It's unclear what is img_addition in the above question.

huangapple
  • 本文由 发表于 2023年5月6日 21:09:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定