📌  相关文章
📜  MATLAB |软件开发工具补充RGB图像中的颜色

📅  最后修改于: 2021-04-17 04:04:09             🧑  作者: Mango

在MATLAB中,RGB图像基本上是彩色像素的3-D图像阵列(M * N * 3),其中每个彩色像素与三个值相关联,这三个值分别对应于指定颜色下RGB图像的红色,蓝色和绿色分量空间位置。

为了补充RGB图像的颜色,将RGB图像中的每种颜色替换为其互补色。

补全的结果是,RGB图像中的暗区域变亮,而亮区域变暗。

使用MATLAB库函数对RGB图像的颜色进行补充:

% read an RGB Image in MATLAB Environment
img=imread('apple.jpg');
  
% complement colors of read RGB image 
% using imcomplement() function
comp=imcomplement(img);
  
% Display Complemented Image 
imshow(comp);

在不使用库功能的情况下补充RGB图像的颜色:

通过从RGB图像类别所支持的最大像素值中减去每个像素值来对RGB图像进行互补,并且将差值用作互补RGB图像中的像素值。

如果RGB图像属于类’uint8’,则每个像素的值将在[0 – 255]之间。因此,对于“ uint8”类类型的最大值,一个像素可以为255。如果RGB图像属于“ uint16”类,则每个像素的值将在[0 – 65535]之间。因此,对于“ uint16”类类型的像素,一个像素可以具有的最大值是65535。类似地,在“ double”类类型的RGB图像中,最大可能像素值为1.0。

例如,让’uint8’类的RGB图像
如果图像像素的值为127,则在补充RGB图像中,同一像素的值将为(255 – 127)= 128。
如果RGB图像像素的值为255,那么在互补RGB图像中,同一像素的值将为(255 – 255)= 0。

以下是上述想法的实现。

% This function will take an RGB image as input
% and will complement the colors in it
  
function [complement] = complementRGB(img)
       
    % determine number of rows, column 
    % and dimension in input image
    [x, y, z]=size(img);
      
    % convert class of RGB image to 'uint8' 
    img=im2uint8(img);
  
    % create a image array of 'uint8' class having 
    % same  number of rows and columns and having 
    % same dimension, with all elements as zero.
    complement = zeros(x, y, z, 'uint8');
      
    % loop to subtract each pixel value from 255 
    for i=1:x
        for j=1:y
             for k=1:z
                   % copy the difference to complement image array
               complement(i, j, k)=255-img(i, j, k);
         end
    end
    end
end
      
  
% Driver Code
  
% read an RGB Image in MATLAB Environment
img=imread('apple.jpg');
  
% call complementRGB() function to
% complement colors in the read RGB Image
comp=complementRGB(img);
  
% Display Complemented RGB Image
imshow(comp);

替代方式:

在MATLAB中,数组是基本的数据结构。它们可以很容易地被操纵。例如Array = 255 - Array ;
上面的代码将从255中减去数组的每个元素。数组可以具有任意数量的维。因此,而不是使用三个循环将255像素减去RGB图像的每个像素。我们可以直接将其写为comp=255 - img
这里的“ img”是代表我们的RGB图像的3-D阵列。

下面的代码还将补充RGB图像:

% read an RGB Image in MATLAB Environment
img=imread('apple.jpg');
  
% convert class of RGB image to 'uint8'
img=im2uint8(img);
  
% complement each pixel by subtracting it from 255.
comp=255-img;
  
% Display Complemented RGB Image 
imshow(comp);

输入:
RGB图像

输出:
倒置的RGB图像

参考资料: https : //in.mathworks.com/help/images/ref/imcomplement.html