The code in the video is the small part of a some project. I do not want to explain the whole project which is about image processing. I just try to show the some part of algorithm which can give you an idea about image processing.The algorithm is created to process each pixel on the sample image. Then, as user wish, the desired part of the image can be altered or removed. I mean the user can change the color of the desired part or can delete. Like popular image processing software which is called photoshop ( ps ).

As it is seen using java and the algorithm, pixel processing is so easy. Lets talk about the code. To get the information about the pixels getRGB() function is so useful. getRBG() function gives the 32 bit data information about the any pixel on the image. To search the image it is used basic
x-y coordinate system in java. Therefore as it is known, the coordinate system is a little bit different than math. I mean ( 0,0 ) point is on the top-left corner.

Actually, in 32 bit information have 4 different information which are about color and the opacity of the image. Each information is 8 bit. 4*8=32. In other words, there are 4 data which are 8 bit on the each pixel. The first 8 bit is about the opacity. Second 8 bit is about the red color of the pixel. Third one is the green and fourth one is the blue.

By the way, RGB means Red Green Blue, all colors in the nature combines of these three colors.

If you can get information of all pixels about the image then it is so easy to manipulate it. You can do whatever you want with image basicly.

There are some points you have to care about. for example If your image is jpg file, then you can loose some data. But If you use png file then you never loose any data,so If you do not interested in the difference between the jpg file and png file then use png.

Lets see some example.Below the image there are different circles with different colors,algorithm process the image and removes all pixels which are not black.

before processing
after processing

Below the code, there are explanation how each 8 bit datas can be taken from the 32 bit information. Just read the comments..

/*
 * www.ulasdikme.com
 * This code makes you can take and redraw
 * black points on the image
 * */



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Window {

public static String image = "1.png";

public static BufferedImage readImage(String fileLocation) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(fileLocation));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}

public static void Window() {

TakeBlackPointsFromImage example = new TakeBlackPointsFromImage();

BufferedImage image1 = readImage("bin/" + image);
int width = image1.getWidth();
int height = image1.getHeight() + 30;

JFrame frame = new JFrame("ReDraw");
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(example, BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setSize(width, height);
}

public static void init() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Window();
}
});
}

} // end of class Window

class TakeBlackPointsFromImage extends JPanel {

public int t=0;
public static String image = Window.image;

private static final long serialVersionUID = 1L;
int b = 0;


public static BufferedImage readImage(String fileLocation) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(fileLocation));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}

public static int image() {

BufferedImage image1 = readImage("bin/" + image);
int width = image1.getWidth();

return width;
}

public static int takeNumberOfDesiredPixels(){
BufferedImage image22 = readImage("bin/" + image);
int t = 0; // how many desired points existed ?
int width = image22.getWidth();
int height = image22.getHeight();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {

int pixel = image22.getRGB(i, j);
int red = (pixel >> 16) & 0x000000FF;
int green = (pixel >> 8) & 0x000000FF;
int blue = (pixel) & 0x000000FF;

if (red == 0 && green == 0 && blue == 0) { // for the take Black
// pixels
// Black pixel has RGB value is zero
t++;

}
}
}// end of the outer for loop
return t;
}
/** Take x coordinates of desired pixels  **/
public static int[] getXCoordinates() {
BufferedImage image22 = readImage("bin/" + image);

int width = image22.getWidth();
int height = image22.getHeight();

int[] ArrayX = new int[takeNumberOfDesiredPixels()];

int d = -1;
for (int w = 0; w < width; w++) {
for (int h = 0; h < height; h++) {

int pixel = image22.getRGB(w, h);
int red = (pixel >> 16) & 0x000000FF;
/** 
* Shift all bits to the rigt as 16 bit
* then multiply or sth like filter with
* 0x000000FF then get the only last 8 bit 
* which is the red information 
* between 0 and 255
* **/
int green = (pixel >> 8) & 0x000000FF;
/** 
* Shift all bits to the rigt as 8 bit
* then multiply or sth like filter with
* 0x000000FF then get the only last 8 bit 
* which is the green information 
* between 0 and 255
* **/
int blue = (pixel) & 0x000000FF;
/**
* multiply or sth like filter with
* 0x000000FF then get the only last 8 bit 
* which is the blue information 
* between 0 and 255
* **/

if (red == 0 && green == 0 && blue == 0) {
// for the take Black pixels
// Black pixel has RGB value is zero

d++;
ArrayX[d] = w;

}
}
}// end of the outer for loop

return ArrayX;

}

/** Take y coordinates of desired pixels  **/
public static int[] getYCoordinates() {
BufferedImage image22 = readImage("bin/" + image);

int width = image22.getWidth();
int height = image22.getHeight();

int[] ArrayY = new int[takeNumberOfDesiredPixels()];

int d = -1;
for (int w = 0; w < width; w++) {
for (int h = 0; h < height; h++) {

int pixel = image22.getRGB(w, h);
int red = (pixel >> 16) & 0x000000FF;
int green = (pixel >> 8) & 0x000000FF;
int blue = (pixel) & 0x000000FF;

if (red == 0 && green == 0 && blue == 0) { // for the take Black
// pixels
// Black pixel has RGB value is zero

d++;

ArrayY[d] = h;

}
}
}// end of the outer for loop

return ArrayY;

}

@Override
protected void paintComponent(Graphics g) {

int c[] =getXCoordinates();
int a = c.length;

int d[] =getYCoordinates();

g.setColor(Color.black);
for (int y = 0; y < a; y++) {

g.drawLine(c[y], d[y], c[y], d[y]);
}

}
}// end of the class cizgi


// -start- for button 

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Example {

private JFrame frmExample;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Example window = new Example();
window.frmExample.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Example() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmExample = new JFrame();
frmExample.setTitle("Example");
frmExample.setBounds(100, 100, 266, 144);
frmExample.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmExample.getContentPane().setLayout(null);

JButton btnNewButton = new JButton("Draw Black Points");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Window.init();
}
});
btnNewButton.setBounds(63, 34, 138, 48);
frmExample.getContentPane().add(btnNewButton);
}
}
Zip file of the example

18/01/2015

Leave a Comment

Your email address will not be published. Required fields are marked *