【问题标题】:MATLAB overlaying white area of binary image with rgb imageMATLAB用rgb图像覆盖二值图像的白色区域
【发布时间】:2016-10-08 18:00:23
【问题描述】:

我正在使用 MATLAB 2012b。

我能够获得图像中的对象轮廓:
使用主动轮廓分割的方法,结果是二进制掩码:

如何用原始 rgb 图像填充二进制图像的白色区域?

基本上我想要的是使背景完全变黑。

这是我的代码:

gambarOri = imread(pathGambar);
A = rgb2gray(gambarOri ); 
mask = zeros(size(A)); mask(10:end-10,10:end-10) = 1; 
BW = activecontour(A, mask, 500);
figure, subplot(1, 2, 1), imshow(A), title('Grayscale'); 
subplot(1, 2, 2), imshow(BW), title('Segmented image in Binary');

【问题讨论】:

    标签: matlab image-segmentation imagefilter


    【解决方案1】:

    您不能在二进制图像上叠加 RGB,因为数据类型不匹配。您可以做的是根据二进制图像修改您的RGB图像。例如,您可以将BWfalse 区域中的RGB 图像值替换为零:

    % separating RGB channels:
    R = gambarOri(:, :, 1);
    G = gambarOri(:, :, 2);
    B = gambarOri(:, :, 3);
    % placing zeros where BW is FALSE:
    R(~BW) = 0;
    G(~BW) = 0;
    B(~BW) = 0;
    % concatenate color layers into final result:
    blackBG = cat(3, R, G, B);
    % figure; imshow(blackBG)
    

    使用您提供的图像的此结果:

    【讨论】:

    • 所以无法叠加不同数据类型的图片,谢谢。顺便说一句,我尝试了你的方法,result 没有将图层连接成黑色背景。这是否意味着我的 BW 错误区域不是白色区域?
    • 嗯,这看起来很奇怪,因为如果你在你的代码之后复制并粘贴我的代码,你应该得到与我得到的相同的结果。你在这个过程中错过了什么吗?或者您可能同时对变量进行了其他操作?
    【解决方案2】:

    你可以使用:

    blackBG = uint8(bsxfun(@times, double(gambarOri), BW));
    

    【讨论】:

      猜你喜欢
      • 2012-09-27
      • 2022-01-18
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多