-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask1.h
More file actions
161 lines (125 loc) · 3.39 KB
/
Copy pathTask1.h
File metadata and controls
161 lines (125 loc) · 3.39 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
#pragma once
#include <iostream>
#include "EasyBMP.h"
using namespace std;
void allocate(int**& array, int cols, int rows)
{
array = new int* [rows];
for (int i = 0; i < rows; i++)
array[i] = new int[cols]{0};
}
void ComponentReLabel(int **&label,int width,int height,int search, int write) {
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
if (label[x][y] == search) {
label[x][y] = write;
}
}
}
}
RGBApixel makepixel(int alpha, int red, int green, int blue) {
RGBApixel make;
make.Alpha = alpha;
make.Red = red;
make.Green = green;
make.Blue = blue;
return make;
}
BMP Task1(BMP &Image) {
cout << "Working on Task1....\n";
int height = Image.TellWidth();
int width = Image.TellHeight();
RGBApixel Pixel;
RGBApixel Black;
Black.Blue = 0;
Black.Green = 0;
Black.Red = 0;
Black.Alpha = 0;
int** img;
allocate(img, width, height);
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
Pixel = Image.GetPixel(x, y);
img[x][y] = (Pixel.Red + Pixel.Blue + Pixel.Green) / 3;
}
}
int counter=0;
int** label;
allocate(label, width, height);
for (int x = 1; x < height-1; x++) {
for (int y = 1; y < width-1; y++) {
if (img[x][y] == 0) {
label[x][y] = 0;
}
else {
if (img[x][y - 1] == 0 && img[x - 1][y - 1] == 0 && img[x - 1][y] == 0 && img[x - 1][y + 1]==0) {
counter++;
label[x][y] = counter;
}
else if (img[x][y - 1] != 0) {
label[x][y] = label[x][y - 1];
}
else if (img[x - 1][y - 1] != 0) {
label[x][y] = label[x - 1][y - 1];
}
else if (img[x - 1][y] != 0) {
label[x][y] = label[x - 1][y];
}
else if (img[x - 1][y + 1] != 0) {
label[x][y] = label[x - 1][y + 1];
}
}
}
}
for (int x = 0; x < 100; x++) {
Recheck:
for (int x = 1; x < height; x++) {
for (int y = 1; y < width - 1; y++) {
if (label[x][y] == 0)
continue;
else if (label[x][y - 1] != 0 && label[x][y - 1] != label[x][y]) {
ComponentReLabel(label, width, height, label[x][y], label[x][y - 1]);
goto Recheck;
}
else if (label[x - 1][y - 1] != 0 && label[x - 1][y - 1] != label[x][y]) {
ComponentReLabel(label, width, height, label[x][y], label[x - 1][y - 1]);
goto Recheck;
}
else if (label[x - 1][y] != 0 && label[x - 1][y] != label[x][y]) {
ComponentReLabel(label, width, height, label[x][y], label[x - 1][y]);
goto Recheck;
}
else if (label[x - 1][y + 1] != 0 && label[x - 1][y + 1] != label[x][y]) {
ComponentReLabel(label, width, height, label[x][y], label[x - 1][y + 1]);
goto Recheck;
}
}
}
}
int* labels = new int[counter]{0};
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
if (label[x][y]>0)
labels[label[x][y]]++;
}
}
int maxlabel = labels[0];
for (int x = 0; x < counter; x++) {
if (labels[x] > maxlabel)
maxlabel = x;
}
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
Image.SetPixel(x, y, Black);
}
}
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
if (label[x][y] ==maxlabel )
Image.SetPixel(x, y, makepixel(255, 255, 255, 255));
}
}
Image.WriteToFile("Result1.bmp");
cout << "\tResult Image Saved!\n\n";
return Image;
}