Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Vogel committed Mar 18, 2012
0 parents commit 0569dc7
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
@@ -0,0 +1,15 @@
# eclipse project files
.classpath
.project
.settings

# compiled sources
bin
*.class

# Mac
.DS_Store

# do not ignore this file
!.gitignore

5 changes: 5 additions & 0 deletions README.md
@@ -0,0 +1,5 @@
Image Segmentation
==================

The project purpose is to show how image segmentation can be done with several
different approaches.
Binary file added example/game.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/pic.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions src/de/imagesegmentation/Run.java
@@ -0,0 +1,24 @@
package de.imagesegmentation;

import de.imagesegmentation.entity.Image;
import de.imagesegmentation.util.ImageUtil;
import de.imagesegmentation.view.ImageClient;

public class Run {

/**
* @param args
*/
public static void main(String[] args) {

Image img = Image.loadImage("example/game.jpg");

ImageClient c1 = new ImageClient();
c1.show(img);

Image image = ImageUtil.processGayScaling(img);
ImageClient c2 = new ImageClient();
c2.show(image);
}

}
98 changes: 98 additions & 0 deletions src/de/imagesegmentation/entity/Image.java
@@ -0,0 +1,98 @@
package de.imagesegmentation.entity;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Image {

BufferedImage img = null;

RGBEntry[][] colorEntries;

public Image() {}

public static Image createImage(RGBEntry[][] colorEntries) {
Image rImage = new Image();

rImage.img = new BufferedImage(colorEntries.length, colorEntries[0].length, BufferedImage.TYPE_INT_RGB);

for(int i = 0; i < colorEntries.length; i++) {
for(int j = 0; j < colorEntries[0].length; j++) {
rImage.img.setRGB(i, j, colorEntries[i][j].getRGB());
}
}

return rImage;
}

public boolean save(String path) {
File outputfile = new File(path);
try {
ImageIO.write(img, "jpg", outputfile);
} catch (IOException e) {
System.err.println("Saving image interrupted. Message: " + e.getMessage());
return false;
}

return true;
}

public static Image loadImage(String imagefilename) {
Image rImage = new Image();

if(imagefilename == null) {
System.err.println("Argument is null!");
return null;
}

File f = new File(imagefilename);

if(!f.exists()) {
System.err.println("File do not exists!");
return null;
}

try {
rImage.img = ImageIO.read(f);
} catch (IOException e) {
System.err.println("Error during loading image. Message: " + e.getMessage());
return null;
}

rImage.createRGBEntries();

return rImage;
}

private void createRGBEntries() {
colorEntries = new RGBEntry[img.getWidth()][img.getHeight()];

for(int row=0;row<img.getWidth(); row++) {
for(int col=0;col<img.getHeight();col++) {
Color rgb = new Color(img.getRGB(row, col));

colorEntries[row][col] = new RGBEntry(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
}
}
}

public int getHeight() {
return img.getHeight();
}

public RGBEntry[][] getColorEntries() {
return colorEntries;
}

public int getWidth() {
return img.getWidth();
}

public BufferedImage getImage() {
return img;
}
}
58 changes: 58 additions & 0 deletions src/de/imagesegmentation/entity/RGBEntry.java
@@ -0,0 +1,58 @@
package de.imagesegmentation.entity;

public class RGBEntry {
private int r,g,b;

public RGBEntry(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}

public int getR() {
return r;
}

public int getG() {
return g;
}

public int getB() {
return b;
}

public int getRGB() {
return ((255 & 0xFF) << 24) |
((r & 0xFF) << 16) |
((g & 0xFF) << 8) |
((b & 0xFF) << 0);
}

public RGBEntry normalize() {
float sumRGB = r + g + b;
RGBEntry normEntry = null;

if(sumRGB != 0) {
float normR = (float)r/sumRGB;
float normG = (float)g/sumRGB;
float normB = (float)b/sumRGB;
normEntry = new RGBEntry((int) (normR*255+0.5),
(int) (normG*255+0.5), (int) (normB*255+0.5));
} else {
normEntry = new RGBEntry(0, 0, 0);
}

return normEntry;
}

public RGBEntry grayScale() {
int brightness = (int) (0.212671 * r + 0.715160 * g + 0.072169 * b);

return new RGBEntry(brightness, brightness, brightness);
}

@Override
public String toString() {
return r + "," + g + "," + b;
}
}
36 changes: 36 additions & 0 deletions src/de/imagesegmentation/util/ImageUtil.java
@@ -0,0 +1,36 @@
package de.imagesegmentation.util;

import de.imagesegmentation.entity.Image;
import de.imagesegmentation.entity.RGBEntry;

public class ImageUtil {

public static Image processImageNormalization(Image imageToNormalize) {
RGBEntry[][] color = imageToNormalize.getColorEntries();

RGBEntry[][] norm = new RGBEntry[color.length][color[0].length];

for(int i=0;i<color.length;i++) {
for(int j=0;j<color[i].length;j++) {
norm[i][j] = color[i][j].normalize();
}
}

return Image.createImage(norm);
}

public static Image processGayScaling(Image image) {
RGBEntry[][] color = image.getColorEntries();

RGBEntry[][] norm = new RGBEntry[color.length][color[0].length];

for(int i=0;i<color.length;i++) {
for(int j=0;j<color[i].length;j++) {
norm[i][j] = color[i][j].grayScale();
}
}

return Image.createImage(norm);
}

}
42 changes: 42 additions & 0 deletions src/de/imagesegmentation/view/ImageClient.java
@@ -0,0 +1,42 @@
package de.imagesegmentation.view;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import de.imagesegmentation.entity.Image;

public class ImageClient {

public ImageClient() {}

public void show(String filename) {

JFrame f = new JFrame("Load Image Sample");

f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

f.add(new ImagePanel(filename));
f.pack();
f.setVisible(true);
}

public void show(Image img) {
JFrame f = new JFrame("Load Image Sample");

f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

f.add(new ImagePanel(img));
f.pack();
f.setVisible(true);
}
}
38 changes: 38 additions & 0 deletions src/de/imagesegmentation/view/ImagePanel.java
@@ -0,0 +1,38 @@
package de.imagesegmentation.view;

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JComponent;

import de.imagesegmentation.entity.Image;

public class ImagePanel extends JComponent {

private static final long serialVersionUID = -1698374606968040177L;

private Image img = null;

public ImagePanel(String imagePath) {
img = Image.loadImage(imagePath);
}

public ImagePanel(Image img) {
this.img = img;
}

@Override
public Dimension getPreferredSize() {
if(img == null) {
return new Dimension(100,100);
} else {
return new Dimension(img.getWidth(), img.getHeight());
}
}

@Override
public void paint(Graphics g) {
g.drawImage(img.getImage(), 0, 0, null);
}

}

0 comments on commit 0569dc7

Please sign in to comment.