-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColorQuantization.cs
More file actions
186 lines (146 loc) · 6.93 KB
/
ColorQuantization.cs
File metadata and controls
186 lines (146 loc) · 6.93 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Linq;
using static ImageQuantization.MST;
namespace ImageQuantization
{
class ColorQuantization
{
public static List<int> distinctColorsList;
public static uint noise = 0;
public static int k;
public static RGBPixel[,] ColorQuantize(RGBPixel[,] ImageMatrix, int number_of_clusters)
{
distinctColorsList = GetDistinctColorsList(ImageMatrix);
int[] parent = Prim(distinctColorsList); //O(D^2)
ConstructEdges(distinctColorsList, parent); //O(D)
if (Config.BOUNS)
EdgesOfclustersB(number_of_clusters);
else
ClusterEdges(number_of_clusters); //O(DlogD)
Forest forest = new Forest(MST.edges); //O(D-K)
List<List<RGBPixel>> clusters = Forest.GetClusters(distinctColorsList.Count);
if (Config.BOUNS)
clusters = Truecluster(clusters, number_of_clusters);
k = clusters.Count;
Dictionary<int, short> clusterIndices = PopulateClusterIndicies(clusters); //O(D)
List<RGBPixel> colorPallette = GetColorPallette(clusters); //O(D)
ReduceImageColors(ImageMatrix, colorPallette, clusterIndices);
int countColorsBefore = distinctColorsList.Count;
int countColorsAfter = colorPallette.Count;
Console.WriteLine("number of CLUSTERS: " + clusters.Count);
Console.WriteLine("Reduced number of colors in image from " + countColorsBefore + " to " + countColorsAfter);
Console.WriteLine("Noise: " + noise.ToString());
return ImageMatrix;
}
private static Dictionary<int, short> PopulateClusterIndicies(List<List<RGBPixel>> clusters) //O(D)
{
Console.WriteLine("START PopulateClusterIndicies at: " + (MainForm.stopWatch.Elapsed).ToString());
Dictionary<int, short> clusterIndices = new Dictionary<int, short>();
for(int i = 0; i< clusters.Count; i++)
//this will loop over all nodes in all clusters,
//since number of nodes is D then this whole loop takes
//O(D)
{
var cluster = clusters[i];
for (int j = 0; j < cluster.Count; j++)
{
clusterIndices.Add(RGBPixel.Hash(cluster[j]), (short)i);
}
}
Console.WriteLine("finished PopulateClusterIndicies at " + (MainForm.stopWatch.Elapsed).ToString());
return clusterIndices;
}
public static List<RGBPixel> GetColorPallette(List<List<RGBPixel>> clusters) //O(D)
{
// for every member of cluster sum all values and get the mean for the sum
List<RGBPixel> colorPallete = new List<RGBPixel>();
for (int clusterIndex = 0; clusterIndex < clusters.Count; clusterIndex++)
//this will loop over all nodes in all clusters,
//since number of nodes is D then this whole loop takes
//O(D)
{
int sumRed = 0, sumGreen = 0, sumBlue = 0;
int numberOfColorsInCluster = clusters[clusterIndex].Count;
foreach (RGBPixel pixel in clusters[clusterIndex])
{
sumRed += pixel.red;
sumBlue += pixel.blue;
sumGreen += pixel.green;
}
sumRed = (int)Math.Ceiling((double)sumRed / numberOfColorsInCluster);
sumGreen = (int)Math.Ceiling((double)sumGreen / numberOfColorsInCluster);
sumBlue = (int)Math.Ceiling((double)sumBlue / numberOfColorsInCluster);
byte red = Convert.ToByte(sumRed);
byte green = Convert.ToByte(sumGreen);
byte blue = Convert.ToByte(sumBlue);
RGBPixel representitaveColor = new RGBPixel(red, green, blue);
colorPallete.Add(representitaveColor);
}
return colorPallete;
}
private static List<int> GetDistinctColorsList(RGBPixel[,] ImageMatrix) //O(N^2)
{
HashSet<int> distinctColors = new HashSet<int>();
foreach (RGBPixel pixel in ImageMatrix)
distinctColors.Add(RGBPixel.Hash(pixel)); // O(N^2) if C# handles resizing well
List<int> colorsList = distinctColors.ToList();
Console.WriteLine("finished GetDistinctColorsList at " + (MainForm.stopWatch.Elapsed).ToString());
Console.WriteLine("Number of distinct colors is " + distinctColors.Count);
return colorsList;
}
public static int GetWeight(int a, int b) //O(1)
{
RGBPixel aPixel = RGBPixel.UnHash(a);
RGBPixel bPixel = RGBPixel.UnHash(b);
int differenceRed = (aPixel.red - bPixel.red) * (aPixel.red - bPixel.red);
int differenceGreen = (aPixel.green - bPixel.green) * (aPixel.green - bPixel.green);
int differenceBlue = (aPixel.blue - bPixel.blue) * (aPixel.blue - bPixel.blue);
return differenceRed + differenceGreen + differenceBlue;
}
public static double GetDistance(int weight) //O(1)
{
return Math.Sqrt(weight);
}
private static void ReduceImageColors(RGBPixel[,] ImageMatrix, List<RGBPixel> ColorPallette, Dictionary<int, short> clusterIndices)
{
int rows = ImageOperations.GetHeight(ImageMatrix);
int columns = ImageOperations.GetWidth(ImageMatrix);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
RGBPixel currentColor = ImageMatrix[i, j];
int currentColorClusterIndex = clusterIndices[RGBPixel.Hash(currentColor)];
RGBPixel newColor = ColorPallette[currentColorClusterIndex];
ImageMatrix[i, j] = newColor;
}
}
Console.WriteLine("finished ReduceImageColors at " + (MainForm.stopWatch.Elapsed).ToString());
}
public static List<List<RGBPixel>> Truecluster(List<List<RGBPixel>> cluster, int num_of_clusters)
{
if (cluster.Count < num_of_clusters)
{
List<RGBPixel> ColorPallette = GetColorPallette(cluster);
List<RGBPixel> colorsrep = new List<RGBPixel>();
foreach (var color in ColorPallette)
{
colorsrep.Add(color);
}
for (int i = 0; i < num_of_clusters - cluster.Count; i++)
{
cluster.Add(colorsrep);
}
}
return cluster;
}
internal static void Reset() //O(1)
{
distinctColorsList = null;
noise = 0;
MST.edges = null;
MST.sum = 0;
}
}
}