# Image Complement in Matlab

• Last Updated : 04 Dec, 2019

Prerequisite: RGB image representation
MATLAB stores most images as two-dimensional matrices, in which each element of the matrix corresponds to a single discrete pixel in the displayed image. Some images, such as truecolor images, represent images using a three-dimensional array. In truecolor images, the first plane in the third dimension represents the red pixel intensities, the second plane represents the green pixel intensities, and the third plane represents the blue pixel intensities.

Image Complement:
In the complement of a binary image, zeros become ones and ones become zeros. Black and white are reversed.
In the complement of a grayscale or color image, each pixel value is subtracted from the maximum pixel value supported by the class (or 1.0 for double-precision images). The difference is used as the pixel value in the output image. In the output image, dark areas become lighter and light areas become darker. For color images, reds become cyan, greens become magenta, blues become yellow, and vice versa.

Approach:

• Complement the image using imcomplement function.
• Display the image using imshow.

Below is the implementation:

 `% Read the binaryImage``i=imread(``'cameraman.png'``);``% Display the image``imshow(i);``% Complement the image using function``i=imcomplement(i);``% Display the complemented image``figure, imshow(i);``% Read the colored image``a=imread(``'flower.png'``);``% Display the image``figure, imshow(a);``% Complement the image using function``a=imcomplement(a);``% Display the complemented image``figure, imshow(a);`

Output:

Binary Image

Complement of Binary Image:
Here in binary image 0 becomes 1, black and white are reversed.

Colored image

Complement of colored Image:
Each color channel of the resulting image is the complement of the corresponding color channel in the original image. Regions that were dark, become light. In the original image, the leaves appear green, and petals appear red. In the complement image, the leaves appear magenta. The flower petals appear cyan.

My Personal Notes arrow_drop_up