Negative of an image in MATLAB

The negative of an image is achieved by replacing the intensity ‘i’ in the original image by ‘i-1’, i.e. the darkest pixels will become the brightest and the brightest pixels will become the darkest. Image negative is produced by subtracting each pixel from the maximum intensity value.

For example in an 8-bit grayscale image, the max intensity value is 255, thus each pixel is subtracted from 255 to produce the output image.

The transformation function used in image negative is :

s = T(r) = (L – 1) – r Where L - 1 is the max intensity value, s is the output pixel value and r is the input pixel value

**Algorithm**

- Read RGB color image into the MATLAB environment using Matlab inbuilt function
`imread()`

- Calculate the levels of the image, for example an 8-bit image has 256 levels
- Use the formula stated above on every pixel of the image to get corresponding negative pixel value.
- Convert each RGB pixel value at location (i, j) to its negative image values and assign it to the corresponding location (i, j) of another matrix
- Display the negative image using Matlab in-built
`imshow()`

function.

`% reading the RGB file into the Matlab environment` `skI = imread(` `"sakura.jpg"` `); ` `subplot(1, 2, 1),` ` ` `% displaying the RGB image` `imshow(skI);` `title(` `"Original image"` `);` ` ` `% levels of the 8-bit image` `L = 2 ^ 8; ` ` ` `% finding the negative ` `neg = (L - 1) - skI;` `subplot(1, 2, 2),` ` ` `% displaying the negative image` `imshow(neg);` `title(` `"Negative Image"` `)` |

**Output :**