Skip to content

Commit e0eaf9c

Browse files
committed
Image Processing Final Project
0 parents  commit e0eaf9c

10 files changed

+390
-0
lines changed

ImageProcess/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

ImageProcess/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ImageProcess</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8

ImageProcess/bin/Filter.class

6.95 KB
Binary file not shown.

ImageProcess/bin/ImageHandler.class

1.47 KB
Binary file not shown.

ImageProcess/bin/Main.class

1.8 KB
Binary file not shown.

ImageProcess/src/Filter.java

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
import java.awt.Color;
2+
import java.awt.image.BufferedImage;
3+
import java.awt.image.ConvolveOp;
4+
import java.awt.image.DataBufferByte;
5+
import java.awt.image.Kernel;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import javax.imageio.ImageIO;
9+
import java.lang.Math.*;
10+
import java.util.Arrays;
11+
12+
public class Filter {
13+
// 轉成灰階值
14+
public static BufferedImage toGray(BufferedImage image){
15+
int width = image.getWidth();
16+
int height = image.getHeight();
17+
for(int y = 0; y < height; y++){
18+
for(int x = 0; x < width; x++){
19+
int p = image.getRGB(x,y);
20+
int a = (p>>24)&0xff;
21+
int r = (p>>16)&0xff;
22+
int g = (p>>8)&0xff;
23+
int b = p&0xff;
24+
int avg = (r+g+b)/3;
25+
p = (a<<24) | (avg<<16) | (avg<<8) | avg;
26+
image.setRGB(x, y, p);
27+
}
28+
}
29+
return image;
30+
}
31+
// 轉成負片效果
32+
public static BufferedImage toNegative(BufferedImage image){
33+
int width = image.getWidth();
34+
int height = image.getHeight();
35+
for(int y = 0; y < height; y++){
36+
for(int x = 0; x < width; x++){
37+
int p = image.getRGB(x,y);
38+
int alpha = new Color(image.getRGB(x,y)).getAlpha();
39+
int red = new Color(image.getRGB(x,y)).getRed();
40+
int green = new Color(image.getRGB(x,y)).getGreen();
41+
int blue = new Color(image.getRGB(x,y)).getBlue();
42+
alpha = 255 - alpha;
43+
red = 255 - red;
44+
green = 255 - green;
45+
blue = 255 - blue;
46+
p = (alpha<<24) | (red<<16) | (green<<8) | blue;
47+
image.setRGB(x, y, p);
48+
}
49+
}
50+
return image;
51+
}
52+
// 利用 gamma 轉換
53+
public static BufferedImage setGamma(BufferedImage image, double gamma){
54+
image = toGray(image);
55+
int width = image.getWidth();
56+
int height = image.getHeight();
57+
BufferedImage gamma_cor = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
58+
59+
for(int y = 0; y < height; y++){
60+
for(int x = 0; x < width; x++){
61+
62+
int p = image.getRGB(x,y);
63+
int alpha = new Color(image.getRGB(x,y)).getAlpha();
64+
int red = new Color(image.getRGB(x,y)).getRed();
65+
int green = new Color(image.getRGB(x,y)).getGreen();
66+
int blue = new Color(image.getRGB(x,y)).getBlue();
67+
68+
red = (int) (255 * (Math.pow((double) red / (double) 255, gamma)));
69+
green = (int) (255 * (Math.pow((double) green / (double) 255, gamma)));
70+
blue = (int) (255 * (Math.pow((double) blue / (double) 255, gamma)));
71+
p = (alpha<<24) | (red<<16) | (green<<8) | blue;
72+
gamma_cor.setRGB(x, y, p);
73+
}
74+
}
75+
return gamma_cor;
76+
}
77+
// 胡椒鹽雜訊
78+
public static BufferedImage saltpepper(BufferedImage image)
79+
{
80+
int height = image.getHeight();
81+
int width = image.getWidth();
82+
int nSalt = 5; // Percentage of salt
83+
int nPepper = 5;
84+
int salt = height * width * nSalt / 100; // Amount of salt
85+
int pepper = height * width * nSalt / 100; // Amount of pepper
86+
87+
for( int i = 0; i < salt; i++ )
88+
{
89+
int x = (int) (Math.random() * width);
90+
int y = (int) (Math.random() * height);
91+
int p = image.getRGB(x,y);
92+
int alpha = new Color(image.getRGB(x,y)).getAlpha();
93+
int red = 255;
94+
int green = 255;
95+
int blue = 255;
96+
97+
p = (alpha<<24) | (red<<16) | (green<<8) | blue;
98+
image.setRGB( x, y, p );
99+
}
100+
101+
for( int i = 0; i < pepper; i++ )
102+
{
103+
int x = (int) (Math.random() * width);
104+
int y = (int) (Math.random() * height);
105+
int p = image.getRGB(x,y);
106+
int alpha = new Color(image.getRGB(x,y)).getAlpha();
107+
int red = 0;
108+
int green = 0;
109+
int blue = 0;
110+
111+
p = (alpha<<24) | (red<<16) | (green<<8) | blue;
112+
image.setRGB( x, y, p );
113+
}
114+
return image;
115+
}
116+
// 3 X 3中值濾波器
117+
public static BufferedImage median_filter(BufferedImage image) {
118+
int maskSize = 3;
119+
int width = image.getWidth();
120+
int height = image.getHeight();
121+
int outputPixels[] = new int[width * height];
122+
int red[], green[], blue[];
123+
int xMin, xMax, yMin, yMax;
124+
int argb, reD, greenN, bluE;
125+
/** Median Filter operation */
126+
for (int y = 0; y < height; y++) {
127+
for (int x = 0; x < width; x++) {
128+
int a = new Color(image.getRGB(x,y)).getAlpha();
129+
red = new int[maskSize * maskSize];
130+
green = new int[maskSize * maskSize];
131+
blue = new int[maskSize * maskSize];
132+
int count = 0;
133+
xMin = x - (maskSize / 2);
134+
xMax = x + (maskSize / 2);
135+
yMin = y - (maskSize / 2);
136+
yMax = y + (maskSize / 2);
137+
for (int r = yMin; r <= yMax; r++) {
138+
for (int c = xMin; c <= xMax; c++) {
139+
if (r < 0 || r >= height || c < 0 || c >= width) {
140+
/** Some portion of the mask is outside the image. */
141+
continue;
142+
} else {
143+
argb = image.getRGB(c, r);
144+
reD = (argb >> 16) & 0xff;
145+
red[count] = reD;
146+
greenN = (argb >> 8) & 0xff;
147+
green[count] = greenN;
148+
bluE = (argb) & 0xFF;
149+
blue[count] = bluE;
150+
count++;
151+
}
152+
}
153+
}
154+
155+
/** sort red, green, blue array */
156+
Arrays.sort(red);
157+
Arrays.sort(green);
158+
Arrays.sort(blue);
159+
160+
/** save median value in outputPixels array */
161+
int index = (count % 2 == 0) ? count / 2 - 1 : count / 2;
162+
int p = (a << 24) | (red[index] << 16) | (green[index] << 8) | blue[index];
163+
outputPixels[x + y * width] = p;
164+
}
165+
}
166+
for (int y = 0; y < height; y++) {
167+
for (int x = 0; x < width; x++) {
168+
image.setRGB(x, y, outputPixels[x + y * width]);
169+
}
170+
}
171+
return image;
172+
}
173+
// 3 X 3 平均濾波器
174+
public static BufferedImage mean_filter(BufferedImage image){
175+
int height = image.getHeight();
176+
int width = image.getWidth();
177+
for (int x = 1; x < height - 1; x++) {
178+
for (int y = 1; y < width - 1; y++) {
179+
Color p1 = new Color(image.getRGB(x - 1 , y - 1));
180+
Color p2 = new Color(image.getRGB(x - 1 , y));
181+
Color p3 = new Color(image.getRGB(x - 1, y + 1));
182+
Color p4 = new Color(image.getRGB(x, y - 1));
183+
Color p5 = new Color(image.getRGB(x, y));
184+
Color p6 = new Color(image.getRGB(x, y + 1));
185+
Color p7 = new Color(image.getRGB(x + 1, y - 1));
186+
Color p8 = new Color(image.getRGB(x + 1, y));
187+
Color p9 = new Color(image.getRGB(x + 1, y + 1));
188+
int alpha_avg = (int)(p1.getAlpha() + p2.getAlpha() + p3.getAlpha() + p4.getAlpha() + p5.getAlpha() + p6.getAlpha() + p7.getAlpha() + p8.getAlpha() + p9.getAlpha()) / 9;
189+
int red_avg = (int)(p1.getRed() + p2.getRed() + p3.getRed() + p4.getRed() + p5.getRed() + p6.getRed() + p7.getRed() + p8.getRed() + p9.getRed()) / 9;
190+
int green_avg = (int)(p1.getGreen() + p2.getGreen() + p3.getGreen() + p4.getGreen() + p5.getGreen() + p6.getGreen() + p7.getGreen() + p8.getGreen() + p9.getGreen()) / 9;
191+
int blue_avg = (int)(p1.getBlue() + p2.getBlue() + p3.getBlue() + p4.getBlue() + p5.getBlue() + p6.getBlue() + p7.getBlue() + p8.getBlue() + p9.getBlue()) / 9;
192+
193+
int p = (alpha_avg<<24) | (red_avg<<16) | (green_avg<<8) | blue_avg;
194+
195+
image.setRGB(x, y, p);
196+
}
197+
}
198+
return image;
199+
}
200+
public static BufferedImage sobel_filter(BufferedImage image){
201+
BufferedImage Gx, Gy;
202+
float[] x1 = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
203+
float[] y1 = {-1,-2,-1,0,0,0,1,2,1};
204+
//2x2 matrix, with those two float arrays.
205+
Kernel MatrixA = new Kernel(3, 3, x1);
206+
Kernel MatrixB = new Kernel(3, 3, y1);
207+
ConvolveOp convolve1 = new ConvolveOp(MatrixA);
208+
ConvolveOp convolve2 = new ConvolveOp(MatrixB);
209+
Gx = convolve1.filter(image, null);
210+
Gy = convolve2.filter(image, null);
211+
for (int x=0; x<image.getWidth(); x++) {
212+
for (int y=0; y<image.getHeight(); y++) {
213+
int derp = Gx.getRGB(x,y);
214+
int herp = Gy.getRGB(x,y);
215+
double result = Math.sqrt(Math.pow(derp, 2.0) + Math.pow(herp, 2.0));
216+
if(result < 20726564.99) {
217+
image.setRGB(x,y,Color.white.getRGB());
218+
} else {
219+
image.setRGB(x,y,Color.black.getRGB());
220+
}
221+
}
222+
}
223+
return image;
224+
}
225+
public static BufferedImage scale_2_binary(BufferedImage image){
226+
int height = image.getHeight();
227+
int width = image.getWidth();
228+
float threshold = 0;
229+
for(int y = 0; y < height; y++){
230+
for(int x = 0; x < width; x++){
231+
int p = image.getRGB(x,y);
232+
int a = new Color(p).getAlpha();
233+
int r = new Color(p).getRed();
234+
int g = new Color(p).getGreen();
235+
int b = new Color(p).getBlue();
236+
// int rgb = 0xFFFF * r + 0xFF * g + b;
237+
float luminance = (r * 0.2126f + g * 0.7152f + b * 0.0722f) / 255;
238+
threshold += luminance;
239+
240+
}
241+
}
242+
threshold = threshold / (height * width);
243+
System.out.println(threshold);
244+
for(int y = 0; y < height; y++){
245+
for(int x = 0; x < width; x++){
246+
int p = image.getRGB(x,y);
247+
int a = new Color(p).getAlpha();
248+
int r = new Color(p).getRed();
249+
int g = new Color(p).getGreen();
250+
int b = new Color(p).getBlue();
251+
float luminance = (r * 0.2126f + g * 0.7152f + b * 0.0722f) / 255;
252+
if (luminance > threshold){
253+
image.setRGB(x, y, Color.white.getRGB());
254+
255+
}else{
256+
image.setRGB(x, y, Color.black.getRGB());
257+
}
258+
}
259+
}
260+
261+
return image;
262+
}
263+
}

ImageProcess/src/ImageHandler.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.awt.image.BufferedImage;
2+
import java.io.File;
3+
import java.io.IOException;
4+
5+
import javax.imageio.ImageIO;
6+
7+
public class ImageHandler {
8+
private String path;
9+
public ImageHandler(String path){
10+
this.path = path;
11+
}
12+
public BufferedImage loadImage(){
13+
BufferedImage image = null;
14+
try {
15+
16+
image = ImageIO.read(new File(path));
17+
18+
} catch (IOException e) {
19+
System.out.println(e.getMessage());
20+
}
21+
return image;
22+
}
23+
public void outputImage(BufferedImage image , String filename){
24+
25+
26+
try {
27+
ImageIO.write(image, "jpg", new File(
28+
"C:\\Users\\CodingXiang\\Desktop\\image\\" + filename + ".png"));
29+
30+
} catch (IOException e) {
31+
System.out.println(e.getMessage());
32+
}
33+
}
34+
}

ImageProcess/src/Main.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.awt.Color;
2+
import java.awt.image.BufferedImage;
3+
import java.awt.image.DataBufferByte;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import javax.imageio.ImageIO;
7+
import java.lang.Math.*;
8+
import java.util.Arrays;
9+
10+
public class Main {
11+
12+
public static void main(String[] args) {
13+
// original image file path
14+
String image_path = "C:\\Users\\CodingXiang\\Desktop\\image\\origin_image.png";
15+
ImageHandler image_handler = new ImageHandler(image_path);
16+
17+
// convert original image to gray
18+
BufferedImage origin_image = image_handler.loadImage();
19+
image_handler.outputImage(Filter.toGray(origin_image), "gray");
20+
BufferedImage gray = Filter.toGray(origin_image);
21+
image_handler.outputImage(Filter.toNegative(gray), "negative");
22+
23+
// set gamma equal 1
24+
BufferedImage origin_image1 = image_handler.loadImage();
25+
BufferedImage gamma_equal_1 = Filter.toGray(origin_image1);
26+
image_handler.outputImage(Filter.setGamma(gamma_equal_1, 1), "gamma=1");
27+
28+
// set gamma smaller than 1
29+
BufferedImage origin_image2 = image_handler.loadImage();
30+
BufferedImage gamma_smaller_than_1 = Filter.toGray(origin_image2);
31+
image_handler.outputImage(Filter.setGamma(gamma_smaller_than_1, 0.5), "gamma_smaller_than_1");
32+
33+
// set gamma bigger than 1
34+
BufferedImage origin_image3 = image_handler.loadImage();
35+
BufferedImage gamma_bigger_than_1 = Filter.toGray(origin_image3);
36+
image_handler.outputImage(Filter.setGamma(gamma_bigger_than_1, 2), "gamma_bigger_than_1");
37+
38+
// put salt and pepper noise on gray image
39+
image_handler.outputImage(Filter.saltpepper(gamma_smaller_than_1), "SaltAndPepper");
40+
41+
// use 3 X 3 median filter
42+
image_handler.outputImage(Filter.median_filter(gamma_smaller_than_1), "Median_Filter");
43+
44+
// use 3 X 3 mean filter
45+
BufferedImage origin_image4 = image_handler.loadImage();
46+
BufferedImage gamma_smaller_than_1_clone = Filter.toGray(origin_image4);
47+
Filter.saltpepper(gamma_smaller_than_1_clone);
48+
image_handler.outputImage(Filter.mean_filter(gamma_smaller_than_1_clone), "Mean_Filter");
49+
50+
// usb Sobel Filter
51+
image_handler.outputImage(Filter.sobel_filter(gamma_equal_1), "Sobel_Filter");
52+
53+
// usb threshold to Binarization
54+
BufferedImage origin_image5 = image_handler.loadImage();
55+
BufferedImage gamma_equal_1_clone = Filter.toGray(origin_image5);
56+
Filter.setGamma(gamma_equal_1_clone, 1);
57+
image_handler.outputImage(Filter.scale_2_binary(gamma_equal_1_clone), "binary");;
58+
}
59+
}

origin_image.png

463 KB
Loading

0 commit comments

Comments
 (0)