Computing Negative, Darker and Brighter Versions of an Image in MATLAB

Image Processing

Image processing is one of the methods to deal with image alteration. This processing method allows the user to perform various algorithms on given images according to specific requirements. Image processing proves helpful in performing image enhancements or extracting valuable information from images. This process takes an image as its input and provides output in either an image form or some features associated with it. There are the following two types of methods in image processing:

  1. Analog Image Processing
  2. Digital Image Processing

Advantages of Using Image Processing

Image processing includes utilizing simple algorithms and tools to provide users with various aspects of images or datasets. Following are some of the primary advantages of using image processing:

1.      Time and Cost Efficiency: It is way faster and economical than the conventional filming methods and using fixing chemicals for processing any image by working with photographing equipment.

2.      Accurate Analysis: Image processing systems allow performing analysis with higher accuracy rates, faster output, and advanced color shade processing, recognizing contrast in colors more effectively.

3.      Ease of Storage: Methods of image processing remove any unnecessary details of noises from images, anticipates accurate image density, and provides easy storage and retrieval of images by using various tools for this purpose.

4.      Desired Formats for Images: Image processing is an essential method for providing users with various image formats. These methods can manipulate images using relevant algorithms to get formats like the negative of an image, displaying black, white, darker, and lighter versions.

Image Processing using MATLAB

MATLAB performs image processing using its image processing toolbox (IPT). This method allows converting an image into its digital format and applying numerous operations to extract crucial characteristics of the image. It happens with the help of designed algorithms for the sole purpose. The image processing toolbox in MATLAB is a combination of various functions. These functions allow the expansion of the working environment of the language. Additionally, it happens due to an extension in the capability of the numeric computing environment. Following are some possible fundamental operations to perform on images using MATLAB:

  1.  Segmentation of an image
  2. Enhancement of an image
  3. Reducing noise from images
  4. Performing geometric transformation
  5. Image registration
  6. 3D image processing

Reading Image File in MATLAB

Moreover, MATLAB uses the imread () function to read images. This function takes the image file’s name as an input, reads the image into the MATLAB environment, and loads it as an array of pixels. A sample code for this purpose is the following:

imread ( ‘image_file.jpg’ );

Displaying Image file in MATLAB

After reading the image into an array, it uses the imshow () function to display the image. A matrix F of 256*256 dimensions stores grayscale image, and in the case of an RGB image, a matrix G is a 3-dimensional matrix of values 256*256*3 stores red, green, and blue components of the pixels. The following code is an example of displaying an image In MATLAB:

imshow ( F );

imshow ( F, G );

Computing Negative Version of an Image

Usually, the intensity of an image corresponds to the brightness of the image. The greater the intensity of an image’s pixels, the brighter the image is and vice versa. It can define the brightening and darkening operation of an image. To compute the negative version of an image, a user can replace the intensity ‘i ‘of an image with ‘i -1 ‘. This replacement results in brightening the darkest pixels and darkening the brightest pixels. In simple words, this process subtracts each pixel from the maximum intensity value.

Algorithm for Negative Image

The algorithm is to read the image into the MATLAB environment using the imread () function and then calculate the image levels. After that, it utilizes the following formula to calculate corresponding negative values of every pixel:

V= maximum intensity value – input pixel value

Then, it converts each pixel residing at the location for example location i, j to its negative value and assigns it to another matrix. This matrix displays the negative version of an image by using the imshow () function of MATLAB. Following is a sample code for such example:

image = imread("image.jpg");
subplot(1, 2, 1),
imshow(image);
title("This is the original image");
Levels = 2 ^ 8; 
negative = (Levels - 1) - image; % Levels-1 is the maximum intensity value
subplot(1, 2, 2),
imshow(negative);
title("This is the Negative version of the Image")

Algorithm for Darker Image

The images are essentially matrices of some numbers that give different colors to every bit of the images. Therefore, to compute the darker version of the image, the users need to tune these values in the image matrix. To understand the concept, let the representation of an image be matrix Z. The users can create a new image K by subtracting some value from matrix Z such as Z-5. This subtraction is equivalent to subtracting that particular number from every brightness value in the matrix Z. In this way, it reduces the brightness of the image, thus darkening it.

Sample Code

Following is the sample code for the above algorithm. It first reads the original image file and plots it on the screen. It then calculates the darker version of the original image by subtracting a random value 5 from the image and plotting the new image on the screen using subplot (), imshow () functions and giving it “This is the darker version of the image” as its title. Therefore, the users can compare the difference between both images by viewing them on their screen.

image = imread("image.jpg");
subplot(1, 2, 1),
imshow(image);
title("This is the original image"); 
dark = image -5; 
subplot(1, 2, 2),
imshow(dark);
title("This is the darker version of the Image")

Algorithm for Brighter Image

The users can calculate the given image’s brighter version by adding a chosen value to the image matrix. This addition adds the number to each brightness value in the image matrix, as discussed above.

Sample Code

Following is a sample code for this algorithm. It first reads the image and plots it on the screen. Moreover, it adds a random value to the image and plots it on the screen with the title “This is the brighter version of the image.”

image = imread("image.jpg");
subplot(1, 2, 1),
imshow(image);
title("This is the original image"); 
bright = image +2; 
subplot(1, 2, 2),
imshow(bright);
title("This is the brighter version of the Image")

It is important to note that darkening and brightening the whole image by these algorithms do not affect the values’ standard deviation. The algorithm adds to or subtracts from the whole image, thus keeping the contrasts between the values intact.

Writing the image

These image algorithms discussed above create a new image by making some changes to the original image and display them on the user’s screen. However, these algorithms do not save the new images in the user’s local storage. Therefore, when they close the MATLAB program, the images are gone. In some scenarios, where the users want to save the new images, they can use the imwrite () function to write them in the current directory.

This function takes the image and file name with the valid file format.

imwrite(image, ‘filename’)
imwrite(image, 'filename.tif')
imwrite(image, 'filename', 'tif')

Note: There could be more than two parameters of the imwrite () function depending on the selected file format.