forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompress.java
170 lines (148 loc) · 6.21 KB
/
Compress.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import java.util.Scanner;
import java.util.Vector;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import javax.imageio.ImageIO;
class ReadImg
{
public static int height;
public static int width;
public int getHeight()
{
return height;
}
public int getWidth() {
return width;
}
public int[][] readImage(String path) {
// return matrix of the original image
BufferedImage img;
try {
img = ImageIO.read(new File(path));
height = img.getHeight();
width = img.getWidth();
int[][] imagePixels = new int[height][width];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Color pixel = new Color(img.getRGB(x, y));
int red = pixel.getRed();
imagePixels[y][x] = red;
}
}
return imagePixels;
} catch (IOException e) {
return null;
}
}
}
public class Compress {
public static void main(String[] args) throws IOException {
Compress compress = new Compress();
ReadImg img = new ReadImg();
int[][] matrix;
matrix = img
.readImage("D:\\JavaClg\\compressimg_ca.jpg");//path image
Scanner input = new Scanner(System.in);
System.out.println("Enter Height Vector");
int VectorHeight = input.nextInt();
System.out.println("Enter Width Vector");
int VectorWidth = input.nextInt();
System.out.println("Enter codeBlockSize");
int codeBlockSize = input.nextInt();
Vector<Vector<Integer>> Vectors = new Vector<>();
Vectors = compress.getVectors(matrix, img.getHeight(), img.getWidth(), VectorHeight, VectorWidth);
Vector<Vector<Integer>> QuantizeVector = new Vector<>();
compress.Split(codeBlockSize, Vectors, QuantizeVector);
Vector<Integer> NumberOfCodeBlocks = compress.NearFromCodeBlock(Vectors, QuantizeVector);
FileWriter myWriter = new FileWriter("DataInput.txt");
myWriter.write("VectorWidth is :" + VectorWidth + "\n");
myWriter.write("VectorHegiht is :" + VectorHeight + "\n");
myWriter.write("codeBlockSize is :" + codeBlockSize + "\n");
myWriter.write("hegihtimg is :" + img.getHeight() + "\n");
myWriter.write("Widthimg is :" + img.getWidth() + "\n");
myWriter.close();
FileOutputStream fileOut = new FileOutputStream("NumberOfCodeBlocks.txt");
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(NumberOfCodeBlocks);
objectOut.close();
FileOutputStream fileOut2 = new FileOutputStream("QuantizedCode.txt");
ObjectOutputStream objectOut2 = new ObjectOutputStream(fileOut2);
objectOut2.writeObject(QuantizeVector);
objectOut2.close();
input.close();
}
public Vector<Vector<Integer>> getVectors(int[][] img, int hegiht, int width, int VectorHeight, int VectorWidth) {
Vector<Vector<Integer>> tempVectors = new Vector<>();
for (int i = 0; i < hegiht; i += VectorHeight) {
for (int j = 0; j < width; j += VectorWidth) {
tempVectors.add(new Vector<>());
for (int k = i; k < i + VectorHeight; k++) {
for (int l = j; l < j + VectorWidth; l++) {
if (k < hegiht && l < width)
tempVectors.lastElement().add(img[k][l]);
}
}
}
}
return tempVectors;
}
private Vector<Integer> CalculateAverage(Vector<Vector<Integer>> Vectors) {
int[] SumElements = new int[Vectors.get(0).size()];
for (Vector<Integer> vector : Vectors)
for (int i = 0; i < vector.size(); i++)
SumElements[i] += vector.get(i);
Vector<Integer> AvgVector = new Vector<>();
for (int i = 0; i < SumElements.length; i++)
AvgVector.add(SumElements[i] / Vectors.size());
return AvgVector;
}
private void Split(int CodeBook, Vector<Vector<Integer>> Vectors, Vector<Vector<Integer>> Quantized) {
if (Vectors.size() == 0)
return;
if (CodeBook == 1) {
Quantized.add(CalculateAverage(Vectors));
return;
}
Vector<Vector<Integer>> leftVectors = new Vector<>();
Vector<Vector<Integer>> rightVectors = new Vector<>();
Vector<Integer> AvgVector = CalculateAverage(Vectors);
for (Vector<Integer> vec : Vectors) {
int right = NearstFrom(vec, AvgVector, 1);
int left = NearstFrom(vec, AvgVector, -1);
if (right >= left)
leftVectors.add(vec);
else
rightVectors.add(vec);
}
Split(CodeBook / 2, leftVectors, Quantized);
Split(CodeBook / 2, rightVectors, Quantized);
}
private int NearstFrom(Vector<Integer> vec, Vector<Integer> Qun, int Spliting) {
int distance = 0;
for (int i = 0; i < vec.size(); i++)
distance += Math.pow(vec.get(i) - Qun.get(i) + Spliting, 2);
return distance;
}
private Vector<Integer> NearFromCodeBlock(Vector<Vector<Integer>> Vectors, Vector<Vector<Integer>> Quantized) {
Vector<Integer> NumberofCodeBlock = new Vector<>();
for (Vector<Integer> vector : Vectors) {
int tempDestance = NearstFrom(vector, Quantized.get(0), 0);
int NearFrom = 0;
for (int i = 1; i < Quantized.size(); i++) {
int tempDestance2 = NearstFrom(vector, Quantized.get(i), 0);
if (tempDestance2 < tempDestance) {
tempDestance = tempDestance2;
NearFrom = i;
}
}
NumberofCodeBlock.add(NearFrom);
}
return NumberofCodeBlock;
}
}