Javatpoint Logo
Javatpoint Logo

MATLAB | Complement Colors in a Binary Image

Digital images with only 0 and 1 are known as binary images. Usually, white and black are the two separate colors these values represent. Typically, in this context, 0 denotes white, and 1 denotes black. Because binary pictures are straightforward and effective at representing specific information, like object masks or outlines, they are frequently employed in image processing and computer vision tasks.

  • Complementing a binary image entails switching the white and black pixels, effectively reversing the image's colors.
  • Dark areas turn into light throughout this procedure, similar to taking a photographic negative when light areas turn into darkness.

Here is a thorough explanation of how complementing a binary image works:

Binary Image Representation:

A binary image is one in which every pixel is represented by 0 or 1. A white pixel has a value of 0, commonly represented in an image as a white area. A black pixel, represented by a pixel value of 1, is often displayed as a black area.

Color Complement: A binary image's colors can be complemented by switching every 0 and every 1 from one to the other. This process effectively inverts the colors of the image. Black areas (1s) turn black, while white regions (0s) turn white.

Final image: It is still a binary image with only two potential pixel values, but their meanings are now reversed. This is known as color complementation. This inversion of the direction might be seen as an inversion of the image's content. Any shapes or objects from the original image remain in the supplemented version, but their colors have been switched.

Applications: A variety of image processing jobs benefit from complementing binary images. It can be used, for instance, in image segmentation to distinguish between objects and backgrounds. In situations where the relative colors of objects in an image need to be reversed for analysis, it can also be utilized for pattern recognition or feature extraction.

  • Depending on your desire and convenience, you can utilize the techniques stated in the preceding response, such as arithmetic operations or the imcomplement Function, to perform this color complementation operation in MATLAB or any other image processing software.
  • The outcome is a binary image in which the hues of black and white have been switched about or complemented.

You can complement the colors in a binary image by inverting the pixel values in MATLAB. Simple arithmetic operations or the imcomplement Function can be used for this.

Here's how to accomplish it using both approaches:

Method 1: Using Arithmetic Operations

As previously mentioned, binary images only have two possible pixel values: 0, which often represents white, and 1, which typically represents black. We carry out a simple arithmetic operation for each pixel in such an image to complete the colors.

A more thorough explanation of each stage in this process is provided below:

Create or Load a Binary Image

Start by creating a binary image programmatically or loading an existing one into MATLAB using the imread Function. You already have a binary image called bwImage for this example.

Ensure that the pixels in bwImage have values of 0 and 1, corresponding to the colors white and black, respectively.

Complement the Colors:

It is possible to utilize a straightforward arithmetic technique to complete the colors in a binary image. You deduct the pixel's present value from 1 for each one in the image.

This line of MATLAB code flips the colors by subtracting the values of bwImage from 1. Any pixel with a value of 0 (white) will turn into a 1 (black) pixel, and vice versa for any pixel with a value of 1 (black).

Display the Complemented Image:

Use the imshow Function to see the finished image with complementing colors:

This command causes the MATLAB figure window to display the bwImageComplement variable as an image. Formerly white areas will appear black in the complemented binary image, and vice versa.

Save the Complemented Image (Optional):

The imwrite Function can be used to store the complementing binary image to a file:

The complemented binary picture is saved using this line of code to a file called "complemented_image.png." The file name and format can be changed as necessary.

  • To get the complemented colors using Method 1, a binary image's pixel values are subtracted from 1.
  • Making white pixels black and black pixels white in a binary image in MATLAB is a simple and easy procedure.

Method 2 entails complementing the colors in a binary image using MATLAB's imcomplement Function. This approach is a practical way to avoid using explicit arithmetic operations while getting the same result.

Method 2 Using the imcomplement Function

Pixel values for binary images in MATLAB are 0 and 1, with 0 commonly denoting white and 1 denoting black. Utilizing the imcomplement Function, perform the following actions to complement the colors of a binary image:

Create or Load a Binary Image

Use the imread Function in MATLAB to import an existing binary image or generate one programmatically to get started. Assume you already have a binary picture called bwImage.

Complement the Colors Using imcomplement:

Imcomplement, a built-in function in MATLAB, was created specifically to complement photograph colors.

  • Send your binary picture to the Function as an input to use it:

The complemented binary image bwImageComplement is created by the complement Function using the input binary image bwImage.

Display the Complemented Image:

Use the imshow Function to see the finished image with complementing colors:

This command causes the MATLAB figure window to display the bwImageComplement variable as an image. Formerly white areas will appear black in the complemented binary image, and vice versa.

Save the Complemented Image (Optional):

The imwrite Function can be used to store the complementing binary image to a file:

saves the complemented binary image to the file "complemented_image.png." The file name and format can be changed as necessary.

  • To complement the colors in a binary image in MATLAB, Method 2 requires utilizing the complement Function, a practical and simple method.
  • It is a quick substitute for manually executing arithmetic operations and was created with this exact use in mind.
  • In the binary image, this procedure produces the same outcome as procedure 1, where white pixels turn black and black pixels into white.

Complementing Binary image using MATLAB library function :


MATLAB | Complement Colors in a Binary Image


The imread Function is first used by the application to read the original binary image. The picture file path you gave was 'zx.jpg.' The actual path to your binary image file should be substituted here.

Add to the original binary image: The complemented binary image (complementImg) is produced from the original binary image (originalImg) using the complement Function. The colors are inverted using this Function, converting white into black and black into white.



MATLAB | Complement Colors in a Binary Image


In MATLAB, complementing an image converts all black (0s) pixels to white (1s) and vice versa. When processing images, this operation can be helpful in several ways. The benefits of complementing binary pictures in MATLAB include the following:

Object Extraction: Complementing a binary image can assist in extracting important background objects. Segmenting and examining the objects is simpler when you invert the image so that the background is black and the objects are white.

Background removal: In some circumstances, you should remove an image's backdrop. You can better isolate and concentrate on the objects in the front by complementing the binary image.

Object detection: By improving the contrast between items and the backdrop, complementing can make identifying and finding objects in an image simpler.

Morphological Operations: The preprocessing phase of complementing binary pictures is frequently utilized for morphological operations like erosion, dilation, opening, and closing.

  • These procedures can aid in reducing noise, filling in blank spaces, or altering the forms of objects in the image.
  • Complementing may be involved in the feature extraction process. Complementing makes calculating features like an object's area, perimeter, and centroid easier.
  • Complementing can make differentiating between items and the backdrop simpler during segmentation when working with complicated images.

Visualization: If the original binary image has a mostly black or white background, viewing or analyzing the data may be simpler when it is complemented.

Preprocessing for Machine Learning: Complementing can be a useful preprocessing technique to boost the model's performance when preparing binary images as input data for machine learning algorithms.


Depending on the exact use case and needs, complementing a binary image in MATLAB by inverting the colors (converting 0s to 1s and 1s to 0s) may have certain drawbacks. Here are a few possible drawbacks:

Loss of Information: To complement a binary image, all foreground pixels must be converted to background pixels, and vice versa. If the original binary picture contains significant patterns or structures lost following the complement operation, this may result in information loss.

Reversal of Meaning: The significance of black and white pixels in a binary image depends on the application. The message may be reversed by complementing the image, making it harder to understand. For instance, in medical imaging, white might depict a tumor.

Issues with Contrast: Complementing a binary image may reduce the contrast between foreground and background elements. This difference might not be preferred depending on the particular application. It can make segmenting objects or carrying out additional picture analysis more difficult.

Artifacts: When complementing a binary image, particularly if the original image contains noise or artifacts, these artifacts may be more obvious or appear differently. Image processing and analysis may become more difficult as a result.

Processing Binary Images Pixel by Pixel in an Inefficient Nested Loop: The code you gave processes each pixel in a binary picture in an inefficient nested loop, which can be problematic for large images. There are more effective ways to complement binary images utilizing MATLAB's various built-in image processing functions, such as complement, which is made expressly for this purpose.

Absence of Error Handling: Your code assumes that the input is a binary image but lacks any error handling or validation. The code will provide unexpected results or errors if the input is not a binary image (for example, a grayscale or color image).

Lack of Support for Other Data Types: It is presumptively a double or logical data type for the supplied picture. If the source picture has, You can experience problems with data type conversion if the incoming image has a different data type.

Inefficient Memory Usage: For large photos, creating a new array (complement) that is the same size as the original image can be memory-intensive. In some circumstances, you can directly edit the input picture in MATLAB rather than constructing a new array, which can save memory and increase productivity.

Lack of Flexibility: Your code is particular to complementing binary pictures and may be difficult to modify for more difficult image processing jobs or for working with new sorts of images.

The built-in functions in MATLAB, including complement for binary image complementation, are performance optimized, handle various data formats, and offer improved error handling. Additionally, MATLAB includes many functions and toolboxes that help streamline your workflow and increase efficiency if you need to carry out more complicated image-processing jobs.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA