# MATLAB Complement colors in a Grayscale Image

## Introduction

The intriguing discipline of image processing has numerous applications, including digital photography, computer vision, and medical imaging. MATLAB's robust numerical computing programming environment offers a flexible environment for dealing with images.

Complementing colors in a grayscale image is a basic yet fascinating image-processing problem that will be covered in this article. Using this method, we may produce aesthetically stunning effects and learn more about how image modification in MATLAB works.

## Understanding Grayscale Images

It's important to comprehend grayscale images and how they differ from their colorful counterparts before we dig into complimenting hues.

• A grayscale image is a single-channel image where each pixel represents a different shade of gray. In an 8-bit image, this typically ranges from black (0) to white (255).
• These pictures are frequently used because they are straightforward and effectively convey visual information.

Images in grayscale are especially useful in situations where color information is redundant or even disturbing. We may concentrate on the image's brightness by removing color, which makes it simpler to interpret and evaluate. Tasks like edge detection, feature extraction, and facial recognition frequently use grayscale photos.

## Grayscale Image's Complementary Colors without MATLAB Library Function:

Each pixel in a grayscale image has a value, which in an 8-bit image typically spans from 0 (black) to 255 (white). In a grayscale image, complementing colors essentially invert the pixel values.

• If a pixel in the original grayscale image has a value of 100, it will have a value of (255 - 100) = 155 in the complemented grayscale image. This implies that what was initially a middle-toned grayscale shade becomes that shade's opposite.
• If a pixel in the original grayscale image has a value of 0 (black), it will have a value of (255 - 0) = 255 in the complemented grayscale image. This is so that white can turn into black and vice versa.
• Similarly, if a pixel in the original grayscale image has a value of 255 (white), it will have a value of (255 - 255) = 0 in the complemented grayscale image. In this instance, white has completely reversed into black.

### Implementation:

Grayscale Value Inversion: Inverting the grayscale values is the primary processing step. This is accomplished by deducting the grayscale value of each pixel from 255 (the highest grayscale value possible in an 8-bit image). The variable complementedImage holds the outcome.

Images Displayed: To display two images side by side in a single figure, the code uses the imshow and subplot functions. The initial grayscale image is displayed in the first subplot, while the complemented grayscale image is shown in the second subplot. Each image has a title to help identify it.

Output:

The script will load the 'gray.jpg' image, and its grayscale status will be checked. A warning notice that the input image is not in the proper format will be shown if it is not grayscale.

• If the image is grayscale, it will continue by complementing the grayscale values to produce an inverted-color version of the original image.
• There will be two subplots presented in a single figure. The initial grayscale image will be displayed in the first subplot, and the complemented grayscale image will be displayed in the second subplot.
• If the saving portion of the code hasn't been commented out, the grayscale image will be stored as "complemented_image.jpg" in the same directory as the script.

## Complementing Colors in Grayscale Images:

To produce a remarkable visual effect in a grayscale image, it is simple but effective to use complementary hues. Darker areas are supposed to turn lighter and vice versa by inverting the grayscale values. The original image is inverted to produce a visually pleasing negative-like representation in which objects and features frequently stand out more.

• Using simple pixel-wise procedures, you may complement colors in a grayscale image in MATLAB.
• The fundamental idea is as follows: For each grayscale pixel, subtract its value from the highest value that may be achieved (255 for an 8-bit image). As a result, the original color is effectively complemented by a new grayscale value.

You can utilize built-in MATLAB functions like imread and imshow to implement this procedure by reading and displaying the image, respectively.

Utilize a straightforward subtraction operation to create the complemented image after that. To add color to a grayscale image, use the following MATLAB code:

The path to your grayscale image should be substituted for 'grays.jpg' in this code. Two images, the original grayscale image, and its complemented version, are produced as a consequence, side by side.

Output:

The script will load the provided grayscale image.

• The grayscale values will then be inverted to produce a complemented grayscale image. As a result, darker areas of the original image will seem lighter in the complemented version, and vice versa.
• The appearance of the two image windows is consecutive. The original grayscale image will be shown in the first window with the title "Original Grayscale Image," the complemented grayscale image will be shown in the second window with the title "Completed Grayscale Image."
• The complementing hues frequently produce a startling and reversed effect on the original image and will be visible to you. This method is frequently used in image processing to highlight details, improve image contrast, and achieve artistic goals.

## Visual Impact and Applications

In grayscale photos, complementary hues can provide visually striking effects. As a result of the dramatic contrast between objects and their backgrounds produced by the inverted grayscale values, details stand out, and patterns and structures are highlighted.

This effect can be especially helpful in a variety of applications, including:

Creative and Artistic Activities: In digital art and graphic design, complementary colors can create distinctive and eye-catching compositions. Artists frequently use this method to give their work a distinctive, striking appearance.

Enhancing Image Features: In scientific and medical imaging, complementary colors can highlight particular aspects of an image, such as anomalies in scans or critical data points in scientific visualizations.

Photography: Photographers can employ color complementation to add drama and intensity to their black-and-white images, giving their work a touch of originality.

Forensic Analysis: Complementary hues can expose hidden information in grayscale photographs during forensic investigations, potentially assisting with examining surveillance film or crime scene evidence.

Education and visualization: Teachers and educators can utilize this strategy to improve the interest level of instructional materials. They can draw viewers' attention to crucial components in diagrams, charts, and presentations by producing complementing grayscale visuals.

While the fundamental approach outlined above is straightforward and efficient, MATLAB also provides a wide range of sophisticated image-processing methods that can be used to enhance colors in grayscale photos. These methods provide you with more control and personalization over the outcome:

Histogram Equalization: The contrast of the supplemented image can be improved by histogram equalization, making it even more visually arresting. To do this, the histeq function in MATLAB might be used.

Custom Color Mapping: To add a particular color tone or gradient to the complementing grayscale image, use custom color mapping. This is accomplished using MATLAB's colormap routines to map grayscale values to a color scale.

Regions of Interest (ROI): Instead of matching colors throughout the entire image, you can use this technique to focus on particular areas of interest. MATLAB's region-based image processing tools can help you target and improve particular image sections.

Real-Time Image Processing: MATLAB offers facilities for recording video streams from cameras and utilizing color complementation methods in real-time for applications that call for real-time processing.

Batch Processing: MATLAB's batch processing capabilities can be used to effectively automate the color complementation procedure across numerous photos when working with huge amounts of grayscale images.

## Uses:

Using MATLAB to complement colors in a grayscale image is a fascinating and simple method with many uses.

• Grayscale values can be inverted to produce aesthetically pleasing effects that boost image contrast and highlight unnoticed details.
• This technique offers a useful tool for picture improvement and visualization, whether you're an artist hoping to add any creative touch to your digital art, a scientist examining photos in a lab, or an educator trying to interest your students.
• The adaptability and broad range of image processing features of MATLAB make it the perfect tool for investigating and testing color complementation in grayscale photos.
• . Start experimenting, investigating, and combining colors to make your grayscale photographs into eye-catching works of art.