-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
270 lines (220 loc) · 5.96 KB
/
main.cpp
File metadata and controls
270 lines (220 loc) · 5.96 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
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
#include<iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include "Functions.h"
#include "Part3.h"
#include "Part4.h"
using namespace std;
int main()
{
// load 8-bit, 1 channel grayscale TIF image
IplImage* image = cvLoadImage("Array-9.tif", 0);
/**************************************************************************BINARY IMAGE***************************************/
// function returns the threshold for binarizing the image
int thr = compute_threshold(image);
cout<<"####################################################"<<endl<<endl;;
cout<<" computed threshold value is : "<<thr<<endl<<endl;
cout<<"####################################################"<<endl<<endl;
// function returns the binarized image using the threshold passed as parameter
IplImage* img = binarize(image,thr);
uchar* data = (uchar*) img -> imageData;
int width = img->width;
int height = img->height;
int step = img->widthStep;
/********************************************************************************GRID**********************************************/
IplImage* image1 = cvLoadImage("Array-9.tif", 1);
int *cols;
int *rows;
rows = new int[height];
cols = new int[width];
for(int i=0; i < width; i++)
{
for(int j=0; j < height; j++)
{
cols[i] = 0; // initialize array of columns to 0
rows[j] = 0; // initialize array of rows to 0
}
}
int r1,r2,c1,c2,tRows,tCols;
// calculating number of white pixels in image row wise and column wise
for(int i = 0; i < width; i++)
{
for(int j = 0; j < height; j++)
{
if(data[i * step + j] == 255)
{
cols[i] = cols[i] + 1;
rows[j] = rows[j] + 1;
}
}
}
// ignoring rows and columns with very few white pixels
for(int i = 0; i < width; i++)
{
if(cols[i] < 18)
{
cols[i] = 0;
}
}
for(int i = 0; i < height; i++)
{
if(rows[i] < 18)
{
rows[i] = 0;
}
}
// to store the first and last coordinate of image
for(int i = 1; i < width; i++)
{
if(cols[i] != 0)
{
c1 = i;
break;
}
}
for(int i = 1; i < width; i++)
{
if(cols[width - i] != 0)
{
c2 = width - i;
break;
}
}
for(int i = 1; i < height; i++)
{
if(rows[i] != 0)
{
r1 = i;
break;
}
}
for(int i = 1; i < height; i++)
{
if(rows[height - i] != 0)
{
r2 = height - i ;
break;
}
}
// drawing the line using first and last coordinates
cvLine(image1, cvPoint(r1,c1), cvPoint(r1,c2), cvScalar(0,255,0), 1);
cvLine(image1, cvPoint(r1,c1), cvPoint(r2,c1), cvScalar(0,255,0), 1);
cvLine(image1, cvPoint(r2,c1), cvPoint(r2,c2), cvScalar(0,255,0), 1);
cvLine(image1, cvPoint(r1,c2), cvPoint(r2,c2), cvScalar(0,255,0), 1);
tCols = 1;
for(int i = r1; i < r2; i++)
{
if(rows[i] == 0) //if the value of row is zero, that mean there is no circle in it
{
do
{
i = i + 1;
}while(rows[i] == 0);
tCols = tCols + 1;
cvLine(image1, cvPoint(i-1,c1), cvPoint(i-1,c2), cvScalar(0,255,0), 1);
}
}
tRows = 1;
for(int i = c1; i < c2; i++)
{
if(cols[i] == 0)
{
do
{
i = i + 1;
}while(cols[i] == 0);
tRows = tRows + 1;
cvLine(image1, cvPoint(r1,i-1), cvPoint(r2,i-1), cvScalar(0,255,0), 1);
}
}
cout<<"Size Of the Grid"<<endl;
cout<<"Total nmuber of rows : " <<tRows<<endl;
cout<<"Total nmuber of columns : " <<tCols<<endl<<endl;
cout<<"####################################################"<<endl<<endl;
/**********************************************************MORPHOLGICAL OPERATION***************************/
int choice;
IplImage* final;
cout<<" 1. open-close operation for 3x3 window"<<endl;
cout<<" 2. close-open operation for 3x3 window"<<endl;
cout<<" 3. open-close operation for 5x5 window"<<endl;
cout<<" 4. close-open operation for 5x5 window"<<endl<<endl;
cout<<"please choose one of the above options (1-4):";
cin>>choice;
cout<<endl<<endl<<"####################################################"<<endl<<endl;
switch(choice)
{
case 1:
final = open_close3(img);
break;
case 2:
final = close_open3(img);
break;
case 3:
final = open_close5(img);
break;
case 4:
final = close_open5(img);
break;
default:
cout<<endl<<" jason dude this is a wrong choice"<<endl;
system("pause");
exit(0);
};
/************************************************************BLOB********************************/
IplImage* colorimg;
int** blob ;
int choice1;
cout<<endl<<"1. 4-connected window "<<endl<<"2. 8-connected window"<<endl<<"Enter your choice 1/2"<<endl<<endl;;
cin>>choice1;
cout<<endl<<endl<<"####################################################"<<endl<<endl;
if(choice1 == 1)
{
blob = connected_4(final);
}
else if(choice1 == 2)
{
blob = connected_8(final);
}
else
{
cout<<" Wrong choice "<<endl;
system("pause");
exit(0);
}
colorimg = connected_regions(final, blob);
/*********************************************************CONTOUR******************************/
IplImage* contImg;
img = padding3(img);
IplImage* tmpimg = dilation3(img);
tmpimg = erosion3(tmpimg);
contImg = countor(tmpimg);
contImg = unpad3(contImg);
/*********************************************************DISPLAY****************************/
cvNamedWindow("InputImage", CV_WINDOW_AUTOSIZE);
cvShowImage("InputImage", image);
cvNamedWindow("BinaryImage", CV_WINDOW_AUTOSIZE);
cvShowImage("BinaryImage", img);
cvNamedWindow("GridImage");
cvShowImage("GridImage",image1);
cvNamedWindow("final image", CV_WINDOW_AUTOSIZE);
cvShowImage("final image", final);
cvNamedWindow("Blob Colored Image", CV_WINDOW_AUTOSIZE);
cvShowImage("Blob Colored Image",colorimg);
cvNamedWindow("contour image", CV_WINDOW_AUTOSIZE);
cvShowImage("contour image", contImg);
cvWaitKey(0);
cvDestroyWindow("InputImage");
cvDestroyWindow("BinaryImage");
cvDestroyWindow("GridImage");
cvDestroyWindow("final image");
cvDestroyWindow("Blob Colored Image");
cvDestroyWindow("contour image");
cvReleaseImage(&colorimg);
cvReleaseImage(&contImg);
cvReleaseImage(&image);
cvReleaseImage(&final);
cvReleaseImage(&img);
cvReleaseImage(&image1);
return 0;
}