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 pathtask2.h
403 lines (354 loc) · 9.35 KB
/
task2.h
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#pragma once
//kmeans
#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<cmath>
#include"task3.h"
//#include"source.cpp"
using namespace cv;
using namespace std;
class point {
int x;
int y;
int pxl;
public:
point()
{
x = 0;
y = 0;
pxl = 0;
}
point(int x1, int y1, int pixel)
{
x = x1;
y = y1;
pxl = pixel;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
int getPxl()
{
return pxl;
}
void setPxl(int P)
{
pxl = P;
}
void setX(int X)
{
x = X;
}
void setY(int Y)
{
y = Y;
}
bool operator==(const point other)
{
if (x == other.x && y == other.y)
return true;
else
return false;
}
bool operator!=(const point other)
{
if (x == other.x && y == other.y)
return false;
else
return true;
}
int EuclideanDistance(point P)
{
int x1 = P.x;
int y1 = P.y;
return (int)sqrt(pow(x1 - x, 2) + pow(y1 - y, 2) * 1.0);
}
/*point operator<(point other)
{
EuclideanDistance(x, y, other.x, other.y);
}*/
};
int EuclideanDistance(point P1, point P2)
{
int x1 = P1.getX();
int x2 = P2.getX();
int y1 = P1.getY();
int y2 = P2.getY();
return (int)sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);
}
//int smaller(int a, int b)
//{
// if (a < b)
// return a;
// else
// return b;
//}
point calculateMeans(int** arr, int rows, int cols, int times)
{
int centroid = 0;
int size = rows * cols;
size = size / 2;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
centroid += arr[i][j];
//if (arr[i][j] == 0)
//size--;
}
}
centroid = centroid / size;
/*point centroid;
int x1 = 0;
int y1 = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
x1 += i;
y1 += j;
}
}
x1 = x1 / rows;
y1 = y1 / cols;
centroid.setX(x1);
centroid.setY(y1);
return centroid;*/
point centre;
int xx = 0;
int yy = 0;
int** lesion = new int* [rows];
for (int i = 0; i < rows; i++)
{
lesion[i] = new int[cols];
}
//int res = arr[0][0];
int count = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (arr[i][j] != 0)
{
xx = xx + i;
yy = yy + j;
count++;
}
else {
xx = xx + 0;
rows--;
yy = yy + 0;
cols--;
}
}
}
xx = xx/size;
yy = yy/size;
centre.setX(xx);
centre.setY(yy);
centre.setPxl(centroid);
return centre;
}
void kMeans(Mat img)
{
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++)
{
for (int k = 0; k < img.channels(); k++) {
ImageArray[i][j] = (int)img.at<Vec3b>(i, j)[k];
}
//x=(int)img.at<uchar>(i, j);
// cout << "Value of pixel" << "(" << i << "," << j << ")" << "=" << x << endl;
////showing the values in console window//
}
}
//K=2, clusters are 2
int** cluster1 = new int* [height];
for (int i = 0; i < height; i++)
{
cluster1[i] = new int[width];
}
int** cluster2 = new int* [height];
for (int i = 0; i < height; i++)
{
cluster2[i] = new int[width];
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
cluster1[i][j] = 0;
cluster2[i][j] = 0;
}
}
// picking two random cluster points
point p1(0, 0, ImageArray[0][0]);
point p2(15, 20, ImageArray[15][20]);
int s = 0;
int z = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
point every(i, j, ImageArray[i][j]);
/* int dist1 = ImageArray[0][0] - ImageArray[i][j];
int dist2 = ImageArray[15][20] - ImageArray[i][j];*/
int dist1 = every.EuclideanDistance(p1);
int dist2 = every.EuclideanDistance(p2);
if (dist1 < dist2)
{ cluster1[i][j] = every.getPxl();//Imagearray[i][j];
s++;
}
else {
cluster2[i][j] = every.getPxl();
z++;
}
}
}
point prevc1(0, 0, ImageArray[0][0]);
point prevc2(15, 20, ImageArray[15][20]);
point c1 = calculateMeans(cluster1, height, width, s);
point c2 = calculateMeans(cluster2, height, width, z);
//int times = 0;
/* int sum1 = 0;
int sum2 = 0;
int xx1 = 0;
int yy1 = 0;
int xx2 = 0;
int yy2 = 0;*/
int count1 = 0;
int count2 = 0;
while (c1!= prevc1&& c2 != prevc2) {
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
point every(i, j, ImageArray[i][j]);/*
int dist1 = c1.getPxl() - ImageArray[i][j];
int dist2 = c2.getPxl() - ImageArray[i][j];*/
int dist1 = every.EuclideanDistance(c1);
int dist2 = every.EuclideanDistance(c2);
if (dist1 < dist2)
{
cluster1[i][j] = every.getPxl();
count1++;
/*sum1 = cluster1[i][j] + sum1;
xx1 = i + xx1;
yy1 = j + yy1;*/
}//Imagearray[i][j];
else
{
cluster2[i][j] = every.getPxl();
count2++;
/*sum2 = sum2 + cluster2[i][j];
xx2 = i-xx1 + xx2;
yy2 = j-yy1 + yy2;
}*/
}
/* xx2 = xx2 / height;
xx1 = xx1 / height;
yy2 = yy2 / width;
yy2 = yy2 / width;*/
}
}
prevc1 = c1;
prevc2 = c2;
//c1 = point(xx1, yy1, sum1);
//c2 = point(xx2, yy2, sum2);
c1 = calculateMeans(cluster1, height, width, count1);
c2 = calculateMeans(cluster2, height, width, count2);
count1 = 0;
count2 = 0;
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (ImageArray[i][j] == cluster1[i][j])
ImageArray[i][j] = 255;
if (ImageArray[i][j] == cluster2[i][j])
ImageArray[i][j] = 0;
}
}
/*for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
ImageArray[i][j] = cluster1[i][j] || cluster2[i][j];
}
}*/
//for (int i = 0; i < height; i++)
//{
// for (int j = 0; j < width; j++)
// {
// cluster1[i][j] = 0;
// cluster2[i][j] = 255;
// //ImageArray[i][j] = cluster1[i][j]+cluster2[i][j];
// }
//}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
for (int k = 0; k < img.channels(); k++)
{
img.at<Vec3b>(i, j)[k] = ImageArray[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//
}
}
Mat gt;
string image_path = samples::findFile("Ground Truths/IMD063_lesion.bmp");
gt = imread(image_path, IMREAD_GRAYSCALE);
if (gt.empty())
{
cout << "Could not read the image: " << image_path << endl;
}
diceCof(gt, img);
imshow("Display window", img);
int k = waitKey(0); // Wait for a keystroke in the window
if (k == 's')
{
imwrite("Original Images/IMD002.bmp", img);
}
}
//int main()
//{
// Mat img;
// string image_path = samples::findFile("Original Images/IMD002.bmp");
// img = imread(image_path, IMREAD_COLOR);
// if (img.empty())
// {
// cout << "Could not read the image: " << image_path << endl;
// return 1;
// }
//
//
//
// kMeans(img);
//
// return 0;
//}