Image processing in Java is possible

Image processing basics


This section explains the basic concepts of digital image processing. For the time being, only pixel-based images are considered. Vector graphics are not covered.

As a first application, RED, GREEN and BLUE images and histograms are created from a given color image.
Every image (e.g. PNG or JPEG) has a certain width (X) and height (Y). These two details define the number of image points (pixels). All pixels together form the image area:

Number of pixels in an image = width x height
or
Image area M = X
· Y

Image area M and image points (pixels). The color value w of each pixel can be stored in a table W. So W is the set of all color values.



Each pixel has a color value. The set of all color values ​​is denoted by W.

A digitized image (b) is an image in which each pixel from the set M of the image area is assigned a color value.
Definition: Digitized image: b: M → W
M. is the image area
W. is the set of all color values


The RGB system and Java

A color is defined by its proportions in the three basic colors red, green and blue. This so-called RGB-System (R.ot-Grün-B.lau system) is based on additive color mixing and is only one of several color models that is completely sufficient for our purposes

Since each of the three basic colors can be represented in 256 levels (0-255), around 16.7 million different colors can be defined in the RGB system.


  • 256 red values ​​256 green values ​​256 blue values ​​= 16 777 216 ≈ 16.7 million colors


For Java it now applies that every pixel is an object (variable) of the color type.
The following code fragment creates all colors that can be seen in the picture:

Color red = color (255,0.0); Color green = color (0,255.0); Color blue = color (0,0,255); Color magenta = color (255,0.255); Color cyan = color (0,255,255); Color yellow = color (255,255,0); Color black = color (0,0,0); Color white = color (255,255,255);
In the RGB system, every color is given
a certain amount of red, green and blue.

  • Color (int r, int g, int b) creates a color object with the primary colors red, green and blue. The values ​​must be in the range 0 to 255. Each color value is therefore 1 byte = 8 bits in size.
  • The methods: int getRed (), int getGreen (), int getBlue () return the red, green and blue components of the color object. Each color value is a number in the range from 0 to 255.
  • Color BLACK = Color (0,0,0)
  • Color WHITE = Color (255,255,255)
A color object is 32 bits in size. The last byte, the α value, is the transparency (opacity).

Bit: 0-7 Red value Range of values ​​0-255Bit: 8-15 Green value Range of values ​​0-255Bit: 16-23 Blue value Range of values ​​0-255Bit: 24-31 transparency
(Opacity)
construction java.awt.Color object

The following program loads an image and displays it within a window:

import de.informatics4kids.Picture; import de.informatics4kids.PictureViewer; public class Viewer {public static void main (String [] args) {// Create and open picture Picture pic = new Picture ("/ home / student / testbild.jpg"); // make viewer available PictureViewer viewer = new PictureViewer (pic.getPicture ()); // show the picture viewer.show (); }}
The picture from the previous program example displayed with the PictureViewer class.



Red, green and blue images


A red image can be generated from the above image by determining the color value of each pixel and determining the red component. The color value of the pixel is then replaced by a color object that only contains the determined red component.
The following program demonstrates this using the pixel at position x = 4 and y = 5.
The first pixel has the position (0,0) and is in the top left of the image.
import java.awt.Color; import de.informatics4kids.Picture; public class Main {public static void main (String [] args) {// open testbild.jpg Picture pic = new Picture (); pic.open ("/ home / student / Bilder / testbild.jpg"); // create an empty copy Picture rotBild = new Picture (pic.widthX (), pic.heightY ()); // Read out the color and create a new one Color color = pic.getColor (3, 4); // insert only red component! Color red = new Color (color.getRed (), 0, 0); // set color rotBild.setColor (3, 4, red); }}


If you go through all the pixels of the image and only ever save one color component, you get red, green or blue images. This is how the "colorful dogs" were created.
Red, green and blue images each contain only one color value of the original image.

Histograms

The image of the red dog consists only of red color values. With the Java method getRed () the color value of each image point (pixel) can be read out and delivers a value in the range of 0-255. If the amount of all possible color values ​​(0-255) is now plotted on the x-axis and how often a special color value was found in the image on the associated y-axis, a histogram is created.

Red and green dog histogram. The y-axis shows how often a single color value is present in the respective image. If you divide all values ​​of the Y-axis by the largest available value, then the frequencies are between 0 and 1. This is called normalization.

A histogram H of an image b is a picture of the crowd W.of the color range to the interval [0.1]. That color value (G), which occurs most frequently, is assigned the value 1. A color value that is not represented at all would be assigned the value 0.
Definition: histogram H
h: W → [0.1]
W: color range
[0,1] Number interval from 0 to 1.
with h (g) =Set of all pixels with the color value g

Total number of pixels in the image


Histograms and red, green and blue images are dealt with in the "Java" task area.