MATLAB | Complement colors in RGB Image
Last Updated :
24 Feb, 2023
In MATLAB, an RGB image is basically a 3-D Image array ( M*N*3 ) of the color pixel, where each color pixel is associated with three values which correspond to red, blue, and green color components of RGB image at a specified spatial location. In complement of colors an RGB image, Each color in RGB image is replaced with their complementary color.
For example,
red color ( 255, 0, 0) is replaced with cyan color ( 0, 255, 255 ).
blue color ( 0, 0, 255 ) is replaced with yellow color ( 255, 255, 0).
here, cyan is complementary color for red and yellow are complementary color for blue.
Dark areas become lighter and light areas become darker in RGB images as a result of complement.
Example:
MATLAB
% MATLAB code read an RGB Image
img=imread('apple.jpg');
% Complement colors of read RGB image
% Using imcomplement() function
comp=imcomplement(img);
% Display Complemented Image
imshow(comp);
Complementing colors of an RGB Image Without Using Library functions:
Complement an RGB image by subtracting each pixel value from the maximum pixel value supported by the class of RGB image and the difference is used as the pixel value in the complemented RGB image.
- If the RGB image belongs to class 'uint8', each pixel will have values between [ 0 - 255]. So, for the 'uint8' class type maximum value a pixel can have is 255. If an RGB image belongs to class 'uint16', each pixel will have values between [ 0 - 65535]. So, for the 'uint16' class type maximum value a pixel can have is 65535. Similarly, the maximum possible pixel value In the 'double' class type RGB image is 1.0.
For example, let an RGB image of 'uint8' class - If an image pixel has a value of 127 then, in complemented RGB image same pixel will have a value ( 255 – 127 ) = 128.
- If RGB image pixel has value 255 then, in complemented RGB image same pixel will have value ( 255 – 255 ) = 0.
Example:
MATLAB
% 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);
Alternate way:
In MATLAB, Arrays are basic data structures. They can be manipulated very easily. For example Array = 255 - Array; The above code will subtract each element of the array from 255. The array can have any number of dimensions. So, Instead of using a three-loop to subtract 255 to each pixel of RGB image. We can directly write it as comp=255 - image. Here ‘img’ is a 3-D array representing our RGB image.
Example:
MATLAB
% MATLAb code for complement an RGB Image:
% 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);
Input:

Output:

2. Modifying the color of RGB image
The images are a collection of pixels and pixels values are integers representing the different colour intensities. To change the background colour or background scene we make use of image processing techniques to perform such actions. To change the colour of a particular pixel we can change the colour value by applying a loop and verifying conditions on the image.
The same technique is being used in this article to change the colour and background colour of the image. This is a very simple and basic example of a very large domain of digital image processing. GFG logo is being considered for this illustration. The background is white and the foreground logo is dark green in colour.
Steps:
- Read the given coloured image.
- Analyse the colour of foreground and background of image.
- Change the foreground colour.
- Change the background colour.
- Display the images.
Function used:
- imread( ) inbuilt function is used to read the image.
- imtool( ) inbuilt function is used to display the image.
- pause( ) inbuilt function is used to halt the execution for specified seconds.
- size( ) inbuilt function is used to get size of the image.
Example:
Matlab
% Change the colour of Image and its background.
function ChangeColor(img)
%Change the color of the image.
%input: RGB image
img1=img;
[x,y,~]=size(img);
for i=1:x
for j=1:y
if(img1(i,j,2)>100 && img1(i,j,1)<100)
img1(i,j,1)=255;
img1(i,j,2)=0;
end
%change the colour of logo
if(img1(i,j,1)==255 && img1(i,j,2)==255 && img1(i,j,3)==255)
img1(i,j,3)=0;
end
%change the BG colour.
end
end
imtool(img,[]);
imtool(img1,[]);
pause(20);
imtool close all;
end
Output:

Code Explanation:
- [x,y,~]=size(img); This line gets the size of the input image.
- if(img1(i,j,2)>100 && img1(i,j,1)<100) This line checks for the green colour pixels.
- img1(i,j,1)=255; This line modifies the green pixels into red coloured pixels.
- img1(i,j,2)=0; This makes the green component 0.
- if(img1(i,j,1)==255 && img1(i,j,2)==255 && img1(i,j,3)==255) This line checks for white background pixels.
- img1(i,j,3)=0; THis makes blue component of background white pixel 0, so that resultant colour is yellow for BG.
- pause(20); This line halts the execution for 20 seconds.
- imtool close all; This line closes all image windows.
Similar Reads
MATLAB | Complement colors in a Binary image
Binary image is a digital image that has only two possible value for each pixel - either 1 or 0, where 0 represents white and 1 represents black. In the complement of a binary image, the image pixel having value zeros become ones and the image pixel having value ones become zeros; i.e white and blac
3 min read
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
3 min read
MATLAB | RGB image to grayscale image conversion
An RGB image can be viewed as three images( a red scale image, a green scale image and a blue scale image) stacked on top of each other. In MATLAB, an RGB image is basically a M*N*3 array of colour pixel, where each colour pixel is a triplet which corresponds to red, blue and green colour component
4 min read
Combine two images in MATLAB
In this article, we will learn how to combine two images of the same size in MATLAB. Now, let's suppose we have been given two colored images of different sizes. One image has the main figure on the left side and the other has one on the right side. We need to combine the two images such that both f
2 min read
How to Perform Contrast Enhancement of Color Image in MATLAB?
Colour Images consist of 3 channels each representing particular colour information in RGB format. RGB stands for Red, Green and Blue. All the colours are generated by the proportional mixture of these 3 colours only. When it comes to contrast enhancement, there is a typical dilemma in deciding the
3 min read
How to Convert HSI Image to RGB Image in MATLAB?
HSI stands for Hue Saturation Intensity. HSI is color space, it provides a way of numeric to readout the image that is corresponding to the color name contained. RGB it's basically called the RGB triplet. in MATLAB the RGB image is a kind of  MxNx3 M cross N cross 3 arrays of colors pixel which is u
3 min read
Image Processing in Java - Contrast Enhancement
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image C
2 min read
How to Converting RGB Image to HSI Image in MATLAB?
Converting the color space of an image is one of the most commonly performed operations in Image Processing. It is used so much as a lot of transformation/filters are performed on a specific color mode of an image. i.e. Thresholding is performed in Grayscale, Color slicing in HSV etc. A color space
2 min read
How to Partially Colored Gray Image in MATLAB?
Partially colored images are a common way of enhancing the objects within the image. It is sometimes used as a tool to emphasize the presence of certain objects within the scene. And the processing required to create one is negligible, in contrast to the effect it produces. In this article you will
3 min read
Display the red, green and blue color planes of a color image in MATLAB
Prerequisite - RGB image representation A colored image can be represented as a 3 order matrix. The first order is for the rows, the second order is for the columns and the third order is for specifying the color of the corresponding pixel. Here we use the RGB color format, so the third order will t
2 min read