MATLAB | Complement colors in a Grayscale Image

In MATLAB, a Grayscale image is a 2-D Image array ( M*N ) of color pixel. When we complement colors in a Grayscale image, Each color pixel in grayscale image is replaced with their complementary color pixel.

Dark areas become lighter and light areas become darker in the grayscale image as result of complement.

Complementing colors of a Grayscale Image Using MATLAB Library Function :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


 

Complementing colors of a Grayscale Image without Using MATLAB Library Function :

Grayscale Images have their pixel value in the range 0 to 255. we can complement a grayscale image by subtracting each pixel value from maximum possible pixel value a Grayscale image pixel can have (i.e 255 ), and the difference is used as the pixel value in the complemented Grayscale image.

It means if an image pixel have value 100 then, in complemented Grayscale image same pixel will have value ( 255 – 100 ) = 155.
And if Grayscale image pixel have value 0 then, in complemented grayscale image same pixel will have value ( 255 – 0 ) = 255.
Similarly, if Grayscale image pixel have value 255 then, in complemented grayscale image same pixel will have value ( 255 – 255 ) = 0.

Below is the Implementation of above idea:

filter_none

edit
close

play_arrow

link
brightness_4
code

% This function will take a Grayscale image as input
% and will complement the colors in it.
  
function [complement] = complementGray(img)
       
    % Determine the number of rows and columns
    % in the Grayscale image array
   
    [x, y]=size(img);
     
    % create a array of same number rows and
    % columns as original Grayscale image array
    complement=zeros(x, y);
      
    % loop to subtract 255 to each pixel.
    for i=1:x
        for j=1:y
              complement(i, j) = 255-img(i, j);
        end
   end
end
  
% Driver Code
  
% read a Grayscale Image in MATLAB Environment
img=imread('apple.jpg');
  
% call complementGray() function to
% complement colors in the Grayscale Image
comp=complementGray(img);
  
% Display complemented Grayscale image
imshow(comp);

chevron_right


 

Alternatively –

Instead of using two loops to subtract 255 to each pixel of grayscale image. We can directly write it as comp = 255-image. Above code will subtract each value of image array from 255.

Below code will also complement a Grayscale Image:

filter_none

edit
close

play_arrow

link
brightness_4
code

% read a Grayscale Image in MATLAB Environment
img=imread('apple.jpg');
  
% complement each pixel by subtracting it from 255.
comp=255-img;
  
% Display Complemented Grayscale Image 
imshow(comp);

chevron_right


Input:
Complemented Grayscale image

Output:
Grayscale Image



My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.