Image Processing in Java - Read, Write, to Black and White, to Gray, to Negative and Bit-Plane Slicing.



Image Processing in Java

To store an image numbers are used in computers in an 2-Dimensional Array format the more the numbers in a pixel the more the clarity. Digital Image Processing is an important aspect which plays a major role in Artificial Intelligence, Machine learning, adjusting to Human Vision System, etc. Today our goal is to read an Image, write an Image, convert an Image to Black and White, convert an Image to Negative, and Bit-Plane Slicing using Java Programming Language.

Read and Write Image

Here you are going to read and write an Image by giving the pathname of an Image file to read and a new pathname to write an Image.

PROGRAM



Here's the Interesting part of the code, the above two lines determine the Image format while writing an Scanned Image. In the above code, type of the Image file was set to png with the help of this you can convert an Image file to another format. While converting a Image to another type file size also changes for Ex: converting a file to JPG will increase 4KB and converting the same file to PNG will increase file size to more than 100KB.

In the above code io package's File Constructor is used to read the Image file using it's pathname and then readed by the BufferedImage using ImageIO.read(File), as I mentioned above again the destination pathname is stored in new File with a new File name and using ImageIO.write(Image readed using BufferedImage,"format",pathname) Image is written in that path.

To read and write an Image you have to use one of the AWT package sub classes called BufferedImage. In BufferedImage TYPE_3BYTE_BGR represents all the three colours red, green and blue are going to be in a 3 Byte format and it has no alpha value included, here you doesn't need any of the types from BufferedImage to read and write an Image.

Black and White Image

Here you are going to read Image and convert it to Black and White and then write it in a destination folder.


PROGRAM



Graphics2D is also a sub class of AWT package it helps to draw an Image. Here the BufferedImage type is set to TYPE_BYTE_BINARY which creates Black Image by default and with the help of Graphics class image is rendered to Black and White. And for converting Image to Grey you have to set BufferedImage's type to TYPE_BYTE_GRAY in the above code. And the Try block is to check whether the code runs properly if not an error will be raised through the Catch block.

Negative Image

Read an Image and get each pixel's RGB value and subtract it with 255 to get a negative image atlas all we have to do is write that Image.


PROGRAM



By getting Images height and width we can loop into each pixel of the Image and change it. The RGB value of each pixel is stored in p by getRGB(width, height). Each value is extracted through right shift operator p>>16 the right shift here used is a signed right shift operator which will shift the 32 bit pattern to shift right 16 bit which means the last 16 bit has been dropped and the left top bit is converted to zeros. Similarly for green the last eight has been dropped and all the bits are shifted to right and then all left top bits are converted to zeros and in Blue none of the bits has been shifted it remains the same. Each RGB value is subtracted to 255 to get the Negative Image. Alpha is an Intensity value so it remains the same for all pixel values as 255. Again the values are shifted left and the RGB value is set for that pixel. This process continues for every pixel of the Image.

Bit-Plane Slicing

Bit-Plane Slicing is to show or highlight important features of an Image. Most of the time the top most bits are responsible for an important part of an Image. Bit-Plane Slicing which means showing only specific parts of the Image by isolating other pixel values of an Image.


PROGRAM



Here 0x80 represents the Most Significant bit of a 8 bit byte set. Blue = colour & 0x80 which means top most 8 bits remain same all the other bits are converted to zero. Green bits are shifted right here the top most bits are from 8 to 15 they only remain the same all the other bits are converted to zero. Similarly in the Red top most bits are shifted to 16 to 23 all the other bits become zero.

Here is the link for Google KickStart Round A 2020 Plates problem using JAVA.

0 Comments