This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDs-assignment1.cpp
341 lines (304 loc) · 9.08 KB
/
Ds-assignment1.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/imgcodecs.hpp>
#include <iostream>
#include <stdio.h>
#include"source.cpp"
using namespace cv;
using namespace std;
class Equivalent
{
int L1;
int L2;
public:
Equivalent()
{
L1 = 0;
L2 = 0;
}
Equivalent(int label1, int label2)
{
L1 = label1;
L2 = label2;
}
int getLabel1()
{
return L1;
}
int getLabel2()
{
return L2;
}
};
int smaller(int a, int b)
{
if (a < b)
return a;
else
return b;
}
int larger(int a, int b)
{
if (a > b)
return a;
else
return b;
}
int* convertTo1D(int** arr, int cols, int rows)
{
int index = 0;
int n = rows * cols;
int* oneD = new int[n];
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
oneD[index] = arr[i][j];
index++;
}
}
return oneD;
}
int** convertTo2D(int* arr, int rows, int cols)
{
int index = 0;
int n = rows * cols;
int** twoD = new int* [rows];
for (int i = 0; i < rows; i++)
{
twoD[i] = new int[cols];
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
twoD[i][j] = arr[index];
index++;
}
}
return twoD;
}
void bubbleSort(int* &arr, int size)
{
int pass = 1;
bool exchanges;
do {
exchanges = false;
for (int i = 0; i < size - pass; i++)
if (arr[i] > arr[i + 1]) {
int tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
exchanges = true;
}
pass++;
} while (exchanges);
}
int mostFrequent(int* arr, int n)
{
// code here
int maxcount = 0;
int element_having_max_freq;
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j])
count++;
}
if (count > maxcount) {
maxcount = count;
element_having_max_freq = arr[i];
}
}
return element_having_max_freq;
}
int** ConnectedComponentsLabelling(int** Image, int rows, int cols)
{
Equivalent* EQ = new Equivalent[cols];
int L = 0; //labelIndex
int** LabelArray = new int* [cols]; //array for storing labels
for (int i = 0; i < cols; i++)
{
LabelArray[i] = new int[rows];
}
for (int i = 0; i < cols; i++) //label for first pixel i.e. [0][0] index
{
for (int j = 0; j < rows; j++)
{
if (Image[0][0] > 0)
{
L++;
LabelArray[0][0] = L;
}
}
}
try {
//column-wise if labels are same then assign the previous label
for (int i = 1; i < cols-1; i++) //0th row and any column excluding 0th column
{
for (int j = 0; j < rows; j++) //label pixels in row j = 0
{
if (Image[i][0] > 0)
{
if (Image[i][0] == Image[i - 1][0])
{
LabelArray[i][0] = LabelArray[i - 1][0];
}
if (Image[i][0] != Image[i - 1][0])
{
L++;
LabelArray[i][0] = L;
}
}
}
}
}
catch (...)
{
cout << "Default Exception";
}
//row-wise if labels are same then assign the previous label
for (int i = 0; i < cols; i++) //label pixels in col i=0
{
for (int j = 1; j < rows-1; j++) //0th column and any row excluding 0th row
{
if (Image[0][j] > 0 && Image[0][j] == Image[0][j-1])
{
LabelArray[0][j] = LabelArray[0][j-1];
}
if (Image[0][j] > 0 && Image[0][j] != Image[0][j-1])
{
L++;
LabelArray[0][j] = L;
}
}
}
for (int i = 1; i < cols-1; i++) //label pixels in col i=0
{
for (int j = 1; j < rows - 1; j++) //0th column and any row excluding 0th row
{
if (Image[i][j] > 0 ) //if its a valid pixel
{
//if left pixel has same label but above pixel doesn't, then assign same label as left pixel
if (Image[i][j] == Image[i - 1][j] && Image[i][j] != Image[i][j - 1])
{
LabelArray[i][j] = LabelArray[i - 1][j];
}
//if top pixel has same label but left pixel doesn't, then assign same label as of top pixel
if (Image[i][j] != Image[i - 1][j] && Image[i][j] == Image[i][j - 1])
{
LabelArray[i][j] = LabelArray[i][j-1];
}
//if both top and left pixels are not equal to current pixel then give new label
if (Image[i][j] != Image[i - 1][j] && Image[i][j] != Image[i][j - 1])
{
L++;
LabelArray[i][j] = L;
}
//if top and left pixels are same, labels of top and left are also same then set same label (eg top label)
if (Image[i][j] == Image[i - 1][j] && Image[i][j] == Image[i][j - 1] && LabelArray[i-1][j]==LabelArray[i][j-1])
{
LabelArray[i][j] = LabelArray[i][j-1];
}
//if top and left pixels are same, but have different labels then assign smaller label
if (Image[i][j] == Image[i - 1][j] && Image[i][j] == Image[i][j - 1] && LabelArray[i - 1][j] != LabelArray[i][j - 1])
{
LabelArray[i][j] = smaller(LabelArray[i - 1][j], LabelArray[i][j - 1]);
//record equivalency by creating object of equivalence class inside array
EQ[i]= Equivalent(LabelArray[i - 1][j], LabelArray[i][j - 1]);
}
}
/*if (Image[0][j] > 0 && Image[0][j] != Image[0][j - 1])
{
L++;
LabelArray[0][j] = L;
}*/
}
}
//2nd pass
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
if (LabelArray[i][j] == EQ[i].getLabel1() || LabelArray[i][j] == EQ[i].getLabel2())
{
LabelArray[i][j] = smaller(EQ[i].getLabel1(), EQ[i].getLabel2());
}
}
}
//after assigning labels we consider the most number of labels as lesion
int* temp = convertTo1D(LabelArray, cols, rows);
int freqLabel = mostFrequent(temp, cols * rows);
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
if (LabelArray[i][j] == freqLabel)
{
Image[i][j] = 255;
}
else
Image[i][j] = 0;
}
}
return Image;
}
int main()
{
Mat img;
string image_path = samples::findFile("Segmented Outputs/mIMD002.bmp");
img = imread(image_path, IMREAD_COLOR);
if (img.empty())
{
cout << "Could not read the image: " << image_path << endl;
return 1;
}
//taking an image matrix//
int width = img.cols;
int height = img.rows;
int** ImageArray = new int* [height];
for (int i = 0; i < height; i++)
{
ImageArray[i] = new int[width];
}
//int x;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
ImageArray[i][j]=(int)img.at<uchar>(i, j);
//x=(int)img.at<uchar>(i, j);
//if (x == 255)
//{
// cout << "Value of pixel" << "(" << i << "," << j << ")" << "=" << x << endl;
//}//showing the values in console window//
}
}
int** ModImage = ConnectedComponentsLabelling(ImageArray, height, width);
/*Mat Image;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
Image.at<uchar>(i, j) = ModImage[i][j] ;
}
}*/
/*Mat A(height,width, CV_64F, ModImage);
memcpy(A.data, ModImage, height * width * sizeof(int));
imwrite("Segmented Outputs/my1.bmp", A);
imshow("Display window", A);*/
imshow("Display window", img);
int k = waitKey(0); // Wait for a keystroke in the window
if (k == 's')
{
imwrite("Segmented Outputs/mIMD002.bmp", img);
}
//int k = waitKey(0); // Wait for a keystroke in the window
//if (k == 's')
//{
// imwrite("Segmented Outputs/mIMD002.bmp", img);
//}
return 0;
}