-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeanAlphaMedian.java
133 lines (114 loc) · 3.05 KB
/
MeanAlphaMedian.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* File: ReadWriteImage.java
*
* Description:
* Read and write image.
*
*/
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Main {
public static void main(String args[]) throws IOException {
Scanner x = new Scanner(System.in);
BufferedImage image = null;
File f = null;
// read image file
try {
f = new File("4.png");
image = ImageIO.read(f);
} catch (IOException e) {
System.out.println("Error: " + e);
}
int[][] im = img2matrix(image);
//write image
try {
f = new File("4median7.jpg");
ImageIO.write(matrix2img(im, image), "JPG", f);
} catch (IOException e) {
System.out.println("Error: " + e);
}
}// main() ends here
public static int[][] img2matrix(BufferedImage bi) {
int p, a, r, g, b;
int[][] C = new int[bi.getHeight()][bi.getWidth()];
for (int i = 0; i < bi.getHeight(); i++) {
for (int j = 0; j < bi.getWidth(); j++) {
p = bi.getRGB(j, i);
// a = (p>>24)&0xff;
r = (p >> 16) & 0xff;
g = (p >> 8) & 0xff;
b = p & 0xff;
C[i][j] = (r + g + b) / 3;
}
}
return C;
}
public static BufferedImage matrix2img(int[][] im, BufferedImage bi) {
for (int i = 0; i < im.length; i++) {
for (int j = 0; j < im[i].length; j++) {
// bi.setRGB(j, i,((((bi.getRGB(j, i)>>24)&0xff)<<24) | (im[i][j]<<16) |
// im[i][j]<<8) | im[i][j]);
bi.setRGB(j, i, im[i][j] << 16 | im[i][j] << 8 | im[i][j]);
}
}
return bi;
}
public static void average(int[][] im, BufferedImage bi, int N, int[] a) {
int count = 0;
for (int i = N / 2 ; i < im.length - N / 2; i++) {
for (int j = N / 2; j < im[i].length - N / 2; j++) {
int sum = 0;
for (int k = i - N / 2; k <= i + N / 2; k++) {
for (int k2 = j - N / 2; k2 <= j + N / 2; k2++) {
sum += im[k][k2];
count++;
}
}
im[i][j] = sum / (N * N);
}
}
}
public static void median(int[][] im, BufferedImage bi, int N, int a[]) {
int c = 0;
for (int i = N / 2; i < im.length - N / 2; i++) {
for (int j = N / 2; j < im[i].length - N / 2; j++) {
c=0;
for (int k = i - N / 2; k <= i + N / 2; k++) {
for (int k2 = j - N / 2; k2 <= j + N / 2; k2++) {
a[c] = im[k][k2];
c++;
}
}
Arrays.sort(a);
im[i][j] = a[N*N/ 2];
}
}
}
public static void alphatrimmed(int[][] im, BufferedImage bi, int N, int a[]) {
int c = 0;
for (int i = N / 2; i < im.length - N / 2; i++) {
for (int j = N / 2; j < im[i].length - N / 2; j++) {
c=0;
for (int k = i - N / 2; k <= i + N / 2; k++) {
for (int k2 = j - N / 2; k2 <= j + N / 2; k2++) {
a[c] = im[k][k2];
c++;
}
}
Arrays.sort(a);
int av=0;
for (int m = 0; m < a.length; m++) {
if(m!=0 && m!=a.length-1) {
av+=a[m];
}
}
av=av/(a.length-2);
im[i][j]=av;
}
}
}
}// class ends here