-
Notifications
You must be signed in to change notification settings - Fork 7
/
Morphological Operations: Opening.cpp
108 lines (95 loc) · 3.64 KB
/
Morphological Operations: Opening.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
/*
RAMESH CHANDRA
*/
#include<bits/stdc++.h>
using namespace std;
void printImage(vector<string>&image){
for(int i=0;i<image.size();i++)
cout<<image[i]<<endl;
}
void dilatedPixel(pair<int,int> imagePixel,vector<string>&dilatedImage,vector<string>&mask,pair<int,int>centerOfMask){
int N = dilatedImage.size();
int M = dilatedImage[0].length();
int deltaI = centerOfMask.first;
int deltaJ = centerOfMask.second;
for(int i=imagePixel.first-deltaI,mask_i=0;mask_i<mask.size();mask_i++,i++){
for(int j=imagePixel.second-deltaJ,mask_j=0;mask_j<mask[mask_i].length();mask_j++,j++){
if(mask[mask_i][mask_j]=='0') //no operation required....as mask contain 0
continue;
if(i<0 || i>= N || j<0 || j>=M) //out of image
continue;
if(dilatedImage[i][j]=='0') //pixel contain atleast one zero where mask contain 1
dilatedImage[i][j]='1';
}
}
}
vector<string> dilation(vector<string>&image,vector<string>&mask,pair<int,int>centerOfMask){
vector<string> dilatedImage = image;
for(int i=0;i<image.size();i++){
for(int j=0;j<image[i].length();j++){
if(image[i][j]=='0') //don't need to any thing
continue;
//check for this pixel
dilatedPixel({i,j},dilatedImage,mask,centerOfMask);
}
}
return dilatedImage;
}
int erodedPixel(pair<int,int> imagePixel,vector<string>&image,vector<string>&mask,pair<int,int>centerOfMask){
int N = image.size();
int M = image[0].length();
int deltaI = centerOfMask.first;
int deltaJ = centerOfMask.second;
for(int i=imagePixel.first-deltaI,mask_i=0;mask_i<mask.size();mask_i++,i++){
for(int j=imagePixel.second-deltaJ,mask_j=0;mask_j<mask[mask_i].length();mask_j++,j++){
if(mask[mask_i][mask_j]=='0') //no operation required....as mask contain 0
continue;
if(i<0 || i>= N || j<0 || j>=M) //out of image
return 0;
if(image[i][j]=='0') //pixel contain atleast one zero where mask contain 1
return 0;
}
}
return 1;
}
vector<string> erosion(vector<string>&image,vector<string>&mask,pair<int,int>centerOfMask){
vector<string> erodedImage = image;
for(int i=0;i<image.size();i++){
for(int j=0;j<image[i].length();j++){
if(image[i][j]=='0') //don't need to any thing
continue;
//check for this pixel
erodedImage[i][j] = char(erodedPixel({i,j},image,mask,centerOfMask)+'0');
}
}
return erodedImage;
}
int main() {
vector<string> image = {
"0000000000",
"0111111100",
"0000111100",
"0000111100",
"0001111100",
"0000111100",
"0001100000",
"0000000000",
"0000000000"
};
vector<string> mask = {
"111",
"111",
"111"
};
//printImage(image);
vector<string> erodedImage = erosion(image,mask,{1,1});
//printImage(erodedImage);
vector<string> dilatedImage = dilation(erodedImage,mask,{1,1});
//printImage(dilatedImage);
int count_one = 0;
for(int i=0;i<dilatedImage.size();i++)
for(int j=0;j<dilatedImage[i].length();j++)
count_one+=(dilatedImage[i][j]=='1');
cout<<count_one<<endl;
return 0;
}