【问题标题】:Converting location into binary masks and filtering the image with such masks将位置转换为二进制掩码并使用此类掩码过滤图像
【发布时间】:2013-08-31 21:00:32
【问题描述】:

假设我们有一个像素标记为12 的图像。我们如何在MATLAB 中执行以下操作?

  • 1s2s 的位置转换为二进制掩码
  • 使用这些蒙版过滤图像

谢谢。

【问题讨论】:

    标签: image matlab mask roi image-masking


    【解决方案1】:

    例子:

    % some RGB image
    img = im2double(imread('peppers.png'));
    [h,w,~] = size(img);
    
    % lets create some random labels. I'm simply dividing into two halves,
    % where upper half is labeled 1, and lower half labeled 2
    labels = [ones(h/2,w)*1; ones(h/2,w)*2];
    
    % compute masks and filter image using each
    img1 = bsxfun(@times, img, labels==1);
    img2 = bsxfun(@times, img, labels==2);
    
    % show result
    subplot(121), imshow(img1)
    subplot(122), imshow(img2)
    

    【讨论】:

    • 感谢您的回复。关于img1 = bsxfun(@times, img, labels==1);,您的意思是这里将img 与具有值1 的像素相乘吗?这里怎么是二进制掩码?
    • @Simplicity: labels==k 将为标签为k 的位置创建一个带有true 的逻辑矩阵,否则为false。然后我将此蒙版乘以图像的像素(逐元素)。乘以一保持原始值,乘以零将使像素归零。 bsxfun 用于在 R、G、B 图像通道的第三维广播掩码。
    • 抱歉,关于subplot,我返回了文档,但不明白121122 的值代表什么?
    • @Simplicity:subplot(1,2,1) 的简写形式,含义:1 行 2 列,并使用第一个图块作为当前轴(指定为线性索引)。这在页面底部的“提示”部分进行了解释:mathworks.com/help/matlab/ref/subplot.html#f33-521269
    • 我应该提请您注意我使用“双”图像这一事实,而不是将其保留在 uint8 数据类型中。这是因为如果您将整数与混合类型相乘,MATLAB 会烦人地抱怨
    猜你喜欢
    • 2020-04-13
    • 2011-02-15
    • 2018-07-21
    • 2019-02-13
    • 2021-07-15
    • 1970-01-01
    • 2016-07-17
    • 2020-03-12
    • 2020-12-22
    相关资源
    最近更新 更多