How to convert a positive image to Negative to using OpenCV library?

Algorithm to convert an image to negative

  • Get the red green blue values of each pixel

  • Subtract each color value from 255 and save them as new color values.

  • Create a new pixel value from the modified colors.

  • set the new value to the pixel.

Implementation in Java

  • Read the required image using ImageIO.read() method.

  • Get the height and width of the image.

  • Using nested for loops traverse through each pixel in the image.

  • Get the pixel value using the getRGB() method.

  • Create a Color object bypassing the above-retrieved pixel value as parameter.

  • Get the red, green, blue values from the color object using the getRed(), getGreen() and getBlue() methods respectively.

  • Calculate the new red, green and blue values as specified in the algorithm.

  • Create a new Color object bypassing the new RGB values as parameters.

  • Get the pixel

  • Set the new pixel value(s) using the setRGB() method.

Example

import java.io.File;
import java.io.IOException;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Color2Negative {
   public static void main(String args[])throws IOException {
      //Reading the image
      File file= new File("D:\Images\car3.jpg");
      BufferedImage img = ImageIO.read(file);
      for (int y = 0; y 

Input

Output

Updated on: 2020-04-09T13:59:35+05:30

411 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements