-
Notifications
You must be signed in to change notification settings - Fork 1
/
Segmentation.cpp
131 lines (107 loc) · 3.41 KB
/
Segmentation.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
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <stack>
#include <string>
#include "Segmentation.h"
using namespace std;
Segmentation::Segmentation(){
}
Segmentation::~Segmentation(){
}
Segmentation::Segmentation(Image& img) : Image(img){
Histogram4& h = *(new Histogram4(img));
float medianThreshold = h.task6(img);
cout << medianThreshold;
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
int brightness = (img.get_Color_x_y(i,j).get_red() + img.get_Color_x_y(i,j).get_blue() + img.get_Color_x_y(i,j).get_green())/3;
if(brightness < medianThreshold){
img.setColor(i,j,0,0,0);
}
else{
img.setColor(i,j,255,255,255);
}
}
}
}
void Segmentation::findConnectedComponents(Image& img){
int height = img.get_height();
int width = img.get_width();
int connectedComponent[height][width];
int count = 1;
vector<vector<int> > groups;
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
connectedComponent[i][j]=0;
}
}
for(int i=0;i<height;i++){
int left=0,up=0;
for(int j=0;j<width;j++){
if(img.get_Color_x_y(i,j).get_red() == 0){
if(i>0){
up = connectedComponent[i-1][j];
if(up!=0)
connectedComponent[i][j] = up;
}
if(j>0){
left = connectedComponent[i][j-1];
if(left!=0){
if(connectedComponent[i][j]== 0){
connectedComponent[i][j] = left;
}
else if(left!=up){
mergeGroups(groups, up, left);
}
}
}
if(connectedComponent[i][j]==0){
connectedComponent[i][j] = count;
vector<int> temp;
temp.push_back(count);
groups.push_back(temp);
count++;
}
}
}
}
int colorArray[9][3] = {
{255,0,0},{0,255,0},{0,0,255},{0,255,255},{255,0,255},{255,255,0},{120,120,0},{0,120,120},{120,0,120}
};
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
int connectedComponentNumber = connectedComponent[i][j];
img.setColor(i,j,0,0,0);
for(int m=0;m<groups.size();m++){
for(int n=0;n<groups[m].size();n++){
if(connectedComponentNumber == groups[m][n]){
// Color c(colorArray[m%9][0],colorArray[m%9][1],colorArray[m%9][2]);
// img.getPix()[i][j].setCol(c);
img.setColor(i,j,colorArray[m%9][0],colorArray[m%9][1],colorArray[m%9][2]);
}
}
}
}
}
}
void Segmentation::mergeGroups(vector<vector<int> > &groups, int grp1, int grp2){
int grp1Pos = -1,grp2Pos = -1;
for(int i=0;i<groups.size();i++){ // find out in which collection of connected groups do the new groups belong
for(int j=0;j<groups[i].size();j++){
if(groups[i][j]==grp1){
grp1Pos = i;
}
else if(groups[i][j]== grp2){
grp2Pos = i;
}
}
}
if(grp1Pos!=grp2Pos){ // if both the groups are not same then merge the groups
for(int i=0;i<groups[grp2Pos].size();i++){ // copy values of group2 to group1
groups[grp1Pos].push_back(groups[grp2Pos][i]);
}
groups.erase(groups.begin()+grp2Pos); // delete group2
}
}