-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageprocessor.pde
141 lines (119 loc) · 5.34 KB
/
imageprocessor.pde
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
class ImageProcessor // only for processing the image, no data stored here
{
ImageProcessor()
{
}
CellGrid gridifyImage(PImage image, int cellSize)
{
image.loadPixels();
int gridWidth = int(image.width / cellSize);
int gridHeight = int(image.height / cellSize);
ArrayList<Cell> cells = new ArrayList<Cell>();
for(int y = 0; y < gridHeight; ++y)
{
int yCellStart = y * cellSize;
int yCellEnd = yCellStart + cellSize;
for(int x = 0; x < gridWidth; ++x)
{
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
int xCellStart = x * cellSize;
int xCellEnd = xCellStart + cellSize;
for(int j = yCellStart; j < yCellEnd; ++j)
{
for(int i = xCellStart; i < xCellEnd; ++i)
{
color temp = image.pixels[j * image.width + i];
redValue += red(temp);
greenValue += green(temp);
blueValue += blue(temp);
}
}
redValue = redValue / (cellSize * cellSize);
greenValue = greenValue / (cellSize * cellSize);
blueValue = blueValue / (cellSize * cellSize);
cells.add(new Cell(color(redValue, greenValue, blueValue)));
}
}
return gatherData(new CellGrid(cells, gridWidth, gridHeight, cellSize));
}
CellGrid gatherData(CellGrid grid)
{
int gridWidth = grid.mGridWidth;
int gridHeight = grid.mGridHeight;
for(int y = 0; y < gridHeight; ++y)
{
for(int x = 0; x < gridWidth; ++x)
{
//row col avg
Cell toSet = grid.getCell(x, y);
color toSetColor = toSet.mColor;
int redAccumulator = 0;
int greenAccumulator = 0;
int blueAccumulator = 0;
for(int i = 0; i < gridWidth; i++)
{
color cellColor = grid.getCell(i, y).mColor;
redAccumulator += red(cellColor);
blueAccumulator += green(cellColor);
greenAccumulator += blue(cellColor);
}
for(int i = 0; i < gridHeight; i++)
{
color cellColor = grid.getCell(x, i).mColor;
redAccumulator += red(cellColor);
blueAccumulator += green(cellColor);
greenAccumulator += blue(cellColor);
}
int amount = gridWidth + gridHeight;
color rowColumnAverageColor = color(redAccumulator / amount, greenAccumulator / amount, blueAccumulator / amount);
//neighbor diff
int neighborRedAccumulator = 0;
int neighborGreenAccumulator = 0;
int neighborBlueAccumulator = 0;
int neighborCount = 0;
if(x > 0)
{
color cellColor = grid.getCell(x - 1, y).mColor;
neighborRedAccumulator += red(cellColor);
neighborGreenAccumulator += green(cellColor);
neighborBlueAccumulator += blue(cellColor);
neighborCount++;
}
else if(x < gridWidth - 1)
{
color cellColor = grid.getCell(x + 1, y).mColor;
neighborRedAccumulator += red(cellColor);
neighborGreenAccumulator += green(cellColor);
neighborBlueAccumulator += blue(cellColor);
neighborCount++;
}
else if(y > 0)
{
color cellColor = grid.getCell(x, y - 1).mColor;
neighborRedAccumulator += red(cellColor);
neighborGreenAccumulator += green(cellColor);
neighborBlueAccumulator += blue(cellColor);
neighborCount++;
}
else if(y < gridHeight - 1)
{
color cellColor = grid.getCell(x, y + 1).mColor;
neighborRedAccumulator += red(cellColor);
neighborGreenAccumulator += green(cellColor);
neighborBlueAccumulator += blue(cellColor);
neighborCount++;
}
color neighborColorAverage = color(neighborRedAccumulator / neighborCount, neighborGreenAccumulator / neighborCount, neighborBlueAccumulator / neighborCount);
int redDifference = (int)abs(red(neighborColorAverage) - red(toSetColor));
int greenDifference = (int)abs(green(neighborColorAverage) - green(toSetColor));
int blueDifference = (int)abs(blue(neighborColorAverage) - blue(toSetColor));
int totalDifference = redDifference + greenDifference + blueDifference;
int neighborDifference = (int)((totalDifference / (3.0f)));
toSet.setExtraData(rowColumnAverageColor, neighborDifference);
}
}
return grid;
}
}