-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaverage.cpp
95 lines (61 loc) · 1.72 KB
/
average.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
#include <iostream>
#include <vector>
#include <cmath>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
Mat& Average(Mat& I, int th);
int main( int argc, char* argv[]) {
if (argc < 3) {
cout << "Not enough parameters" << endl;
return -1;
}
Mat I, O;
I = imread("./processedStd/"+string(argv[2]), IMREAD_GRAYSCALE);
int th = stoi(argv[1]);
if (!I.data) {
cout << "The image: " << "/processedStd/"+string(argv[2]) << " could not be loaded." << endl;
return -1;
}
O = Average(I, th);
// cout << "writing: " << "/processedAvg/"+string(argv[2]) << "\n";
imwrite("./processedAvg/"+string(argv[2]), O);
return 0;
}
/*
Iterates over binary image (0 or 255)
stops at any 255 pixel and begins search for neighbors
if number of neighbors is below a threhsold will set pixel to 0
*/
Mat& Average(Mat& I, int th) {
// accept only char type matrices
CV_Assert(I.depth() == CV_8U);
int channels = I.channels();
int nRows = I.rows;
int nCols = I.cols;
long i,j;
long n,m;
int searchRadius = 10;
for( i = 0; i < nRows; ++i) {
for ( j = 0; j < nCols; ++j) {
int byte = (int)I.at<uchar>(i,j);
if(byte == 255) {
int count = 0;
for( n = -searchRadius; n < searchRadius; n++) {
for( m = -searchRadius; m < searchRadius; m++) {
if(i+n > 0 && i+n < nRows && j+m > 0 && j+m < nCols-1000) {
byte = (int)I.at<uchar>(i+n,j+m);
if(byte == 255) count++;
}
}
}
if(count < th) {
I.at<uchar>(i,j) = 0;
}
}
}
}
return I;
}