In MATLAB, an RGB image is basically a 3-D Image array ( M*N*3 ) of color pixel, where each color pixel is associated with three values which correspond to red, blue and green color component 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 image as result of complement.

**Complementing colors of an RGB Image Using MATLAB Library Function : **

`% 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); ` |

*chevron_right*

*filter_none*

**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 RGB image belongs to class ‘uint8’, each pixel will have values between [ 0 – 255]. So, for ‘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 ‘uint16’ class type maximum value a pixel can have is 65535. Similarly, Maximum possible pixel value In ‘double’ class type RGB image is 1.0.

For example, let an RGB image of ‘uint8’ class

If a image pixel have value 127 then, in complemented RGB image same pixel will have value ( 255 – 127 ) = 128.

If RGB image pixel have value 255 then, in complemented RGB image same pixel will have value ( 255 – 255 ) = 0.

Below is the Implementation of above idea.

`% 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); ` |

*chevron_right*

*filter_none*

**Alternate way: **

In MATLAB, Arrays are basic data structure. They can be manipulated very easily. For example `Array = 255 - Array ;`

The above code will subtract each element of the array from 255. Array can have any number of dimensions. So, Instead of using three loop to subtract 255 to each pixel of RGB image. We can directly write it as `comp=255 - img`

Here ‘img’ is a 3-D array representing our RGB image.

Below code will also 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); ` |

*chevron_right*

*filter_none*

**Input: **

**Output:**

** References : **https://in.mathworks.com/help/images/ref/imcomplement.html

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

## Recommended Posts:

- MATLAB | Complement colors in a Binary image
- MATLAB | Complement colors in a Grayscale Image
- MATLAB | RGB image to grayscale image conversion
- MATLAB | RGB image representation
- Image Complement in Matlab
- MATLAB | Converting a Grayscale Image to Binary Image using Thresholding
- Extract bit planes from an Image in Matlab
- Image Processing in MATLAB | Fundamental Operations
- MATLAB | Display histogram of a grayscale Image
- LSB based Image steganography using MATLAB
- Display the red, green and blue color planes of a color image in MATLAB
- Image Resizing in Matlab
- Flip image across Secondary Diagonal in MATLAB
- Matlab | Edge Detection of an image without using in-built function
- Matlab | Erosion of an Image
- Matlab | Dilation of an Image
- Create Mirror Image using MATLAB
- How to vertically flip an Image using MATLAB
- Matlab program to rotate an image 180 degrees clockwise without using function
- MATLAB - Ideal Lowpass Filter in Image Processing

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.