-
Notifications
You must be signed in to change notification settings - Fork 0
/
testImage.cpp
313 lines (289 loc) · 9.58 KB
/
testImage.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*Program to emulate image processing part of
* E101 library on PC
* Either Linux or mingw
* Use at your own risk.
* Feel free to modify */
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#define CAMERA_WIDTH 320 //Control Resolution from Camera
#define CAMERA_HEIGHT 240 //Control Resolution from Camera
unsigned char pixels_buf[CAMERA_WIDTH*CAMERA_HEIGHT*4];
double DEG2RAD = M_PI/180.0;
double convThreshold = 65.0;
int radiusRange = 5;
int degStep = 9;
int voteThr = 10;
// returns color component (color==0 -red,color==1-green,color==2-blue
// color == 3 - luminocity
// for pixel located at (row,column)
unsigned char get_pixel( int row,int col, int color) {
// calculate address in 1D array of pixels
int address = CAMERA_WIDTH*row*3 + col*3;
if ((row < 0 ) || (row > CAMERA_HEIGHT) )
{
printf("row is out of range\n");
return -1;
}
if ( (col< 0) || (col > CAMERA_WIDTH))
{
printf("column is out of range\n");
return -1;
}
if (color==0)
{
return (pixels_buf[address]);
}
if (color==1)
{
return (pixels_buf[address + 1]);
}
if (color==2)
{
return (pixels_buf[address + 2]);
}
if (color==3)
{
unsigned char y = (pixels_buf[address] + pixels_buf[address+1] +pixels_buf[address+2])/3;
return y;
}
printf("Color encoding wrong: 0-red, 1-green,2-blue,3 - luminosity\n");
return -2; //error
}
int set_pixel(int row, int col, char red, char green,char blue) {
int address = CAMERA_WIDTH*row*3 + col*3;
if ((address < 0) || (address>CAMERA_WIDTH*CAMERA_HEIGHT*4))
{
printf("SetPixel(): wrong x,y coordinates\n");
return -1;
}
pixels_buf[address] = red;
pixels_buf[address+1] = green;
pixels_buf[address+2] = blue;
return 0;
}
int ReadPPM(const char *filename) {
//char buff[16];
FILE *fp=fopen(filename, "rb");
if (!fp) {
printf("Unable to open file '%s'\n", filename);
return -1;
}
// read the header
char ch;
if ( fscanf(fp,"P%c\n",&ch) != 1 || ch != '6')
{
printf("file is wrong format\n");
return -2;
}
// skip comments
ch = getc(fp);
while(ch == '#')
{
do {
ch = getc(fp);
} while (ch != '\n');
ch = getc(fp);
}
if (!isdigit(ch)) printf("Wrong header\n");
ungetc(ch,fp);
//read width,height and max color value
int width, height, maxval;
int res = fscanf(fp,"%d%d%d\n",&width,&height,&maxval);
printf("Open file: width=%d height=%d\n",width,height);
int size = width*height*3;
int num =fread((void*) pixels_buf, 1,size,fp);
if (num!=size) {
printf("can not allocate image data memory: file=%s num=%d size=%d\n",
filename,num,size);
return -3;
}
fclose(fp);
return 0;
}
int SavePPM(char fn[5]) {
//save image into ppm file
FILE *fp;
char fname[9];
sprintf(fname,"%s",fn);
fp = fopen(fname,"wb");
if ( !fp){
printf("Unable to open the file\n");
return -1;
}
// write file header
fprintf(fp,"P6\n %d %d %d\n",CAMERA_WIDTH , CAMERA_HEIGHT,255);
int ind = 0;
int row = 0;
int col = 0;
char red;
char green;
char blue;
for ( row = 0 ; row < CAMERA_HEIGHT; row++){
for ( col = 0 ; col < CAMERA_WIDTH; col++){
red = pixels_buf[ind];
green = pixels_buf[ind+1];
blue = pixels_buf[ind+2];
fprintf(fp,"%c%c%c",red,green,blue);
ind = ind + 3;
}
}
fflush(fp);
fclose(fp);
return 0;
}
int main()
{
// enter image file name
char file_name[7];
printf(" Enter input image file name(with extension:\n");
scanf("%s",file_name);
//printf(" You enter:%s\n",file_name);
// read image file
if (ReadPPM(file_name) != 0){
printf(" Can not open file\n");
return -1;
};
/* OUR CODE STARTS HERE: CONVOLUTION */
char edges[CAMERA_HEIGHT][CAMERA_WIDTH]; // array stores edge detected values
int diamCount = 0;
double diameter = 0;
for (int row = 0; row<CAMERA_HEIGHT; row++) {
diamCount = 0;
for (int col = 0; col<CAMERA_WIDTH; col++) {
int red = get_pixel(row, col, 0);
int grn = get_pixel(row, col, 1);
// sun diameter detection
if ((float)grn/(float)red < 0.4) {
diamCount++;
} else {
if (diamCount > diameter) diameter=diamCount;
diamCount = 0;
}
int setting = 2; // convolve blueness vals
if (row>0 && col>0 && row<CAMERA_HEIGHT-2 && col<CAMERA_WIDTH-2) { // convolve using Sobel kernels
// vertical edge detect
double sobelX = -get_pixel(row-1,col-1,setting) + get_pixel(row-1,col+1,setting) -
2.0*(get_pixel(row, col-1,setting)) + 2.0*(get_pixel(row, col+1,setting)) -
get_pixel(row+1,col-1,setting) + get_pixel(row+1,col+1,setting);
// horizontal edge detect
double sobelY = -get_pixel(row-1,col-1,setting) - 2.0*get_pixel(row-1,col,setting) - (get_pixel(row-1, col+1,setting)) +
(get_pixel(row+1, col-1,setting)) + 2.0*(get_pixel(row+1,col,setting)) + get_pixel(row+1,col+1,setting);
edges[row][col] = 0;
if (fabs(sobelX)+fabs(sobelY) > convThreshold) edges[row][col] = 1;
} else {
edges[row][col] = 0;
}
}
}
int radius = diameter*0.51;
if (radius > 50) radiusRange = 8;
else radiusRange = 5;
printf("radius: %d\n",radius);
/* ACCUMULATION/VOTING */
int votes[320][240];
for (int y=0; y<CAMERA_HEIGHT; y++) { // clear array
for (int x=0; x<CAMERA_WIDTH; x++) {
votes[x][y] = 0;
// fill in gaps where we're confident there's an edge
if ((edges[y-1][x] == 1 && edges[y+1][x] == 1) || (edges [y][x-1] == 1 && edges [y][x+1] == 1)) {
edges[y][x] = 1;
}
}
}
for (int y=0; y<CAMERA_HEIGHT; y++) {
for (int x=0; x<CAMERA_WIDTH; x++) {
unsigned char red = get_pixel(y, x, 0);
unsigned char green = get_pixel(y, x, 1);
if (edges[y][x] == 1 && (float)green/(float)red < 0.4) { // if edge-detected and red THEN vote
for (int r=radius-radiusRange; r<radius+radiusRange; r++) {
for (int deg=0; deg<360; deg+=degStep) {
int cx = (int) (x - (r * cos(deg*DEG2RAD)));
int cy = (int) (y + (r * sin(deg*DEG2RAD)));
if (cx >= CAMERA_WIDTH || cx < 0 || cy >= CAMERA_HEIGHT || cy < 0) {
continue; // don't look outside camera bounds
}
votes[cx][cy] += 1;
}
}
}
}
}
/* TALLY THE VOTES */
int maxedX = 0;
int maxedY = 0;
int maxedVote = 0;
for (int y=1; y<CAMERA_HEIGHT-1; y++) {
for (int x = 1; x < CAMERA_WIDTH - 1; x++) {
bool isLeftCorner = false;
bool isRightCorner = false;
int squareX = x-radius+3; // ignore shapes with a top left square corner
int squareY = y-radius+3;
if (squareX > 0 && squareY > 0) {
int red = get_pixel(squareY, squareX, 0);
int grn = get_pixel(squareY, squareX, 1);
if ((float)grn/(float)red < 0.4 && edges[squareY][squareX] == 1) isLeftCorner = true;
}
squareX = x+radius-3; // move to bottom right corner
squareY = y+radius-3;
if (squareX < 240 && squareY < 240) {
int red = get_pixel(squareY, squareX, 0);
int grn = get_pixel(squareY, squareX, 1);
if ((float)grn/(float)red < 0.4 && edges[squareY][squareX] == 1) isRightCorner = true;
}
if (isLeftCorner && isRightCorner) continue;
if (votes[x][y] > maxedVote) {
maxedVote = votes[x][y];
maxedX = x;
maxedY = y;
}
}
}
printf("x: %d y: %d votes: %d\n", maxedX, maxedY, maxedVote);
// count how many red pixels in middle
diamCount = 0;
diameter = 0;
for (int r=0; r<CAMERA_HEIGHT; r++) {
int red = get_pixel(r, maxedX, 0);
int grn = get_pixel(r, maxedX, 1);
if ((float) grn / (float) red < 0.4) {
diamCount++;
} else {
if (diamCount > diameter) diameter=diamCount;
diamCount = 0;
}
}
// set convolutional result only after getting pixel vals
for (int y=0; y<CAMERA_HEIGHT; y++) {
for (int x=0; x<CAMERA_WIDTH; x++) {
if (edges[y][x] == 1) set_pixel(y,x,255,255,255);
else set_pixel(y,x,0,0,0);
}
}
// mark voted centre
for (int i = -2; i<2; i++) {
for (int j = -2; j<2; j++) {
set_pixel(maxedY+i, maxedX+j, 255,0,0);
}
}
if (edges[maxedY][maxedX] == 1) {
printf("Half circle\n");
} else if (maxedY>CAMERA_HEIGHT-radius/2 || maxedY<radius/2) {
printf("Out of bounds\n");
} else if (maxedVote<voteThr) {
printf("Not enough votes\n");
} else if (abs(diameter/2-radius) > 5) {
printf("No middle red line\n");
} else {
printf("Sun found\n");
}
/* save to ppm */
printf(" Enter output image file name(with extension:\n");
scanf("%s",file_name);
if (SavePPM(file_name) != 0){
printf(" Can not save file\n");
return -1;
};
return 0;
}