【问题标题】:MATLAB: finding values in one array and using those locations to change values in one channel of a separate 3 channel arrayMATLAB:在一个数组中查找值并使用这些位置更改单独的 3 通道数组的一个通道中的值
【发布时间】:2015-04-12 03:21:22
【问题描述】:

所以我有一个带有连接感兴趣区域的标记数组 (array1)(背景全为零,连接区域对于第一个区域全为 1,第二个区域全为 2,第三个区域为 3,等等)我还有一个我认为重要的区域标签的向量(vector1)(例如 1,6,9)。我想在标记数组中找到这些值的位置,然后在相同位置更改单独的 3 通道数组的一个通道中的值(想要将图像的某些部分着色为在另一张图像中发现的基于感兴趣区域的绿色)。

我可以使用下面的代码更改所有通道,但不知道如何指定 (img(y,x,1=0), img(y,x,2=0), img(y,x ,3=255))。

for i=1:1:length(vector1)
    img(array1==vector1(i))=255;
end

【问题讨论】:

    标签: image matlab image-processing


    【解决方案1】:

    如果我理解正确,您有一个对象的标签映射 - 每个对象都有一个关联的 ID,以 0 作为背景。您还有一个包含重要 ID 的向量和一个与此标签映射关联的彩色图像。

    您希望将所有具有重要 ID 的位置设置为 1 种颜色。我将首先创建一个逻辑掩码,其中true 表示像素很重要,否则false。我的意思是,如果我们以您的示例为例,像素是 1、6 或 9。您可以使用bsxfun 结合any 创建此蒙版,然后我们可以使用它来索引您的图像并为这些位置设置正确的颜色。

    因此,这样做:

    %// Create your logical mask
    mask3D = bsxfun(@eq, array1, permute(vector(:), [3 2 1]));
    mask = any(mask3D, 3);
    
    %// Set the image pixels to blue at these locations
    red = img(:,:,1);
    green = img(:,:,2);
    blue = img(:,:,3);
    red(mask) = 0;
    green(mask) = 0;
    blue(mask) = 255;
    
    img = cat(3, red, green, blue);
    

    这是一个快速运行的示例。假设我们有这张带有正方形的图片:

    我们可以看到有三个正方形。让我们将对象 1 和对象 3 更改为蓝色。在我们这样做之前,我们需要得到一个标签图:

    %// Originally a binary image
    im = imread('http://i.stack.imgur.com/DnYQS.png');
    
    %// Get label map 
    array1 = bwlabel(im);
    
    %// Create colour version out of this binary image
    img = 255*uint8(im);
    img = cat(3, img, img, img);
    

    array1 是您在问题中也提到的我们的标签映射,img 是输入图像的彩色版本。现在,vector = [1 3] 我们可以更改这些对象。标签是这样的,左上角是标签1,中间是标签2,右下角是标签3。

    一旦我这样做并运行上面的代码,这就是我得到的图像:

    【讨论】:

      【解决方案2】:

      你可以用两行来实现它-

      %// Create a 3D mask to select the labels [1,6,9] only, as listed in "vector1"
      mask = bsxfun(@and,ismember(array1,vector1),ones(1,1,3));
      
      %// Map "img" with "mask" and set them to tuple:[0,0,255] (blue color)
      img(mask) = reshape(repmat([0 0 255],nnz(mask)/3,1),[],1)
      

      示例运行 -

      array1 =  %// Input
           2     0     1     1     0     6
           0     0     1     0     0     6
           9     0     0     0     0     6
           9     9     0     4     0     0
      vector1 =  %// Input
           1     6     9
      img(:,:,1) =   %// Input: Before code run
         228    19   175    30   192   188
         204    23    34   164   149   248
         188   204   185    84   189   222
          14   241    29   167    60    22
      img(:,:,2) =
          94   202   197    59   200   136
          95    94    53   164    26    24
         175    53   100   124    75   104
         153    23   141    39    61    27
      img(:,:,3) =
          29   246   111    48   197   172
         201   111   168    68   101   110
          75   178    28   204    70   116
         154   194   239   125    10   156
      img(:,:,1) =   %// Output: After code run
         228    19     0     0   192     0
         204    23     0   164   149     0
           0   204   185    84   189     0
           0     0    29   167    60    22
      img(:,:,2) =
          94   202     0     0   200     0
          95    94     0   164    26     0
           0    53   100   124    75     0
           0     0   141    39    61    27
      img(:,:,3) =
          29   246   255   255   197   255
         201   111   255    68   101   255
         255   178    28   204    70   255
         255   255   239   125    10   156
      

      注意[1,6,9]array1中的位置以及在代码运行前后img1中对应值的变化。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-08
        • 2016-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多