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 :
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:
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:
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.
- MATLAB | RGB image to grayscale image conversion
- MATLAB | Converting a Grayscale Image to Binary Image using Thresholding
- MATLAB | Complement colors in a Binary image
- MATLAB | Complement colors in RGB Image
- MATLAB | Display histogram of a grayscale Image
- Image Complement in Matlab
- Java | Converting an Image into Grayscale using cvtColor()
- Extract bit planes from an Image in Matlab
- Image Processing in MATLAB | Fundamental Operations
- MATLAB | RGB image representation
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.