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 black color of the image is reversed.

**Complementing Binary image using MATLAB library function : **

`% read a Binary Image in MATLAB Environment ` `img=imread(` `'geeksforgeeks.jpg'` `); ` ` ` `% complement binary image ` `% using imcomplement() function ` `comp=imcomplement(img); ` ` ` `% Display Complemented Binary Image ` `imshow(comp); ` |

*chevron_right*

*filter_none*

**Complementing Binary image without using library function : **

We can complement a binary image by subtracting each pixel value from maximum possible pixel value a binary image pixel can have (i.e 1 ), and the difference is used as the pixel value in the complemented image. It means if an image pixel have value 1 then, in complemented binary image same pixel will have value ( 1 – 1 ) = 0 and if Binary image pixel have value 0 then, in complemented binary image same pixel will have value ( 1 – 0 ) = 1.

Below is the Implementation of above idea-

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

*chevron_right*

*filter_none*

**Alternate way :**

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

Above code will subtract each element of the array from 1. So, Instead of using two loops to subtract 1 to each pixel of binary image. We can directly write it as –

`comp=1-img`

Here ‘img’ is our binary image array.

Below code will also complement a binary Image

`% read a Binary Image in MATLAB Environment ` `img=imread(` `'geeksforgeeks.jpg'` `); ` ` ` `% complement each pixel by subtracting it from 1 ` `comp=1-img; ` ` ` `% Display Complemented binary 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 RGB Image
- MATLAB | Complement colors in a Grayscale Image
- Image Complement in Matlab
- MATLAB | Converting a Grayscale Image to Binary Image using Thresholding
- MATLAB | RGB image to grayscale image conversion
- Extract bit planes from an Image in Matlab
- Image Processing in MATLAB | Fundamental Operations
- MATLAB | RGB image representation
- 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.