- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLBGAlgorithm.java
More file actions
Latest commit
85 lines (71 loc) · 2.72 KB
/
Copy pathLBGAlgorithm.java
File metadata and controls
85 lines (71 loc) · 2.72 KB
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
importjava.awt.*;
importjava.util.ArrayList;
importstaticjava.lang.System.exit;
publicclassLBGAlgorithm {
privateintQuantizationLevels;
privateintvectorDim[];
LBGAlgorithm(intQuantizationLevels, int[] vectorDim)
{
this.QuantizationLevels = QuantizationLevels;
this.vectorDim = vectorDim;
}
publicArrayList<ImageVector> LBG(int[][] img)
{
if (img.length%vectorDim[0] != 0 || img[0].length%vectorDim[1] != 0) // cant split to these vector dimensions
{
System.out.println("DIMENSIONS WRONG");
exit(1);
}
ArrayList<ImageVector> allVectors = ImageHandler.getAllVectors(img, vectorDim[0], vectorDim[1]);
ArrayList<Cluster> initialClusters = getInitialClusters(allVectors);
ArrayList<ImageVector> codeBook = refineClusters(initialClusters, allVectors);
returncodeBook;
}
privateArrayList<ImageVector> refineClusters(ArrayList<Cluster> clusters, ArrayList<ImageVector> allVectors)
{
for (intk = 0; k < 50; ++k)
{
for (Clusterc : clusters)
c.clearVectors();
for (ImageVectorv : allVectors)
{
intpick = 0;
doubleminDist = clusters.get(0).getRepVector().getDistance(v);
for (inti = 1; i < clusters.size(); ++i)
{
doubledist = clusters.get(i).getRepVector().getDistance(v);
if (dist < minDist)
{
pick = i;
minDist = dist;
}
}
clusters.get(pick).addVector(v);
}
for (Clusterc : clusters)
c.calcRep();
}
ArrayList<ImageVector> codeBook = newArrayList<>();
for (Clusterc : clusters)
codeBook.add(c.getRepVector());
returncodeBook;
}
privateArrayList<Cluster> getInitialClusters(ArrayList<ImageVector> allVectors)
{
ArrayList<Cluster> allClusters = newArrayList<Cluster>();
allClusters.add(newCluster(allVectors));
while (allClusters.size() < QuantizationLevels)
{
ArrayList<Cluster> newAllClusters = newArrayList<Cluster>();
for (Clusterc : allClusters)
{
ClusterSplittersplitter = newClusterSplitter(c);
splitter.split();
newAllClusters.add(splitter.getLowChild());
newAllClusters.add(splitter.getHighChild());
}
allClusters = newAllClusters;
}
returnallClusters;
}
}