【问题标题】:How can I invert a binary image in MATLAB?如何在 MATLAB 中反转二进制图像?
【发布时间】:2011-07-13 17:12:13
【问题描述】:

我有一个二值图像,需要将所有黑色像素转换为白色像素,反之亦然。然后我需要将新图像保存到文件中。有没有办法做到这一点,而不是简单地循环每个像素并翻转它的值?

【问题讨论】:

标签: matlab image-processing binary


【解决方案1】:

如果您有一个只有 0 和 1 的二进制图像 binImage,有许多简单的方法可以将其反转:

binImage = ~binImage;
binImage = 1-binImage;
binImage = (binImage == 0);

然后只需使用函数IMWRITE保存倒影即可。

【讨论】:

    【解决方案2】:

    您可以使用imcomplement matlab 函数。假设你有一个二值图像 b 那么,

    bc = imcomplement(b); % gives you the inverted version of b
    b = imcomplement(bc); % returns it to the original b
    imwrite(bc,'c:\...'); % to save the file in disk
    

    【讨论】:

      【解决方案3】:

      在 Matlab 中,通过使用not,我们可以将 1 转换为 0,将 0 转换为 1

      inverted_binary_image = not(binary_image)
      

      【讨论】:

      • not 函数正是使用 ~ 运算符时调用的函数。
      【解决方案4】:
      [filename, pathname] = uigetfile({'*.bmp'},'Text as image');
      
      img=imread(filename);
      img=im2double(img);
      [r,c,ch]=size(img);
      %imshow(img);
      invert_img=img;
      if(ch==1)
       for i=1:r
          for j=1:c
              if(invert_img(i,j)==0)
                invert_img(i,j)=1;
              else
                invert_img(i,j)=0;
              end
          end 
      end
      end
      

      【讨论】:

      • 与前面介绍的解决方案相比,这是一个非常低效的解决方案,甚至不考虑请求的图像保存。
      猜你喜欢
      • 2014-01-01
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      • 2011-02-22
      • 1970-01-01
      • 2011-02-28
      • 1970-01-01
      相关资源
      最近更新 更多