-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathero_dil.cpp
More file actions
103 lines (95 loc) · 1.67 KB
/
ero_dil.cpp
File metadata and controls
103 lines (95 loc) · 1.67 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
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
int Isvalid(int x,int y,int rows,int cols)
{
if (x<0||y<0||x>=rows||y>=cols)
{
return 0;
}
return 1;
}
int main ()
{
Mat img=imread("samps/erosion.png",0);
Mat a=img.clone();
int ctrw=0,ctrb=0,bg=0,obj=255;
//for finding obj color
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
if (img.at<uchar>(i,j)==0)
{
ctrb++;
}
else
{
ctrw++;
}
}
}
if (ctrw>ctrb)
{
bg=255;
obj=0;
}
//
int n=1; //no. of cycles
//createTrackbar("no. of cycles","image",&n,7);
for (int i = 0; i < n; i++)
{
//Erosion
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
if (a.at<uchar>(i,j)==obj)
{
for (int k = -1; k < 1; i++)
{
for (int l = -1; l < 1; j++)
{
if (Isvalid(i-k,j-l,img.rows,img.cols))
{
if (img.at<uchar>(i-k,j-l)==bg)
{
a.at<uchar>(i,j)==bg;
}
}
}
}
}
}
}
/*Dilation
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
if (a.at<uchar>(i,j)==bg)
{
for (int k = -1; k < 1; i++)
{
for (int l = -1; l < 1; j++)
{
if (Isvalid(i-k,j-l,img.rows,img.cols))
{
if (img.at<uchar>(i-k,j-l)==obj)
{
a.at<uchar>(i,j)==obj;
}
}
}
}
}
}
}*/
}
namedWindow("image",WINDOW_NORMAL);
imshow("image",a);
waitKey(0);
}