-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
238 lines (190 loc) · 7.77 KB
/
main.cpp
File metadata and controls
238 lines (190 loc) · 7.77 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <cv.h>
#include <highgui.h>
using namespace cv;
Vec3b Red( 0, 0, 255 );
struct Location{
int X, Y, size;
Location( unsigned int a, unsigned int b, unsigned int c ) : X( a ), Y( b ), size( c ) {}
const bool operator==(const Location& other) const {
return X == other.X && Y == other.Y;
}
};
const int VectorSize( const std::vector<int>& data ){
int retVal = 0;
for( int i = 0; i < data.size(); i++ ) retVal += data[i];
return retVal;
}
const bool isFinder( const int size, const std::vector<int>& data ){
const int partSize = size / 7;
const int tolerance = partSize / 2;
return std::abs( partSize - data[0] ) < tolerance && std::abs( partSize - data[1] ) < tolerance &&
std::abs( 3 * partSize - data[2] ) < 2 * tolerance && //3 * 3 *
std::abs( partSize - data[3] ) < tolerance && std::abs( partSize - data[4] ) < tolerance;
}
const std::vector<Location> FindMarksHorizontal( Mat image, const int maxfeature ){
std::vector<Location> retVal;
for( unsigned int i = 0; i < image.rows; i++ ){
for( unsigned int j = 0; j < image.cols; j++ ){
if( image.at<unsigned char>(i, j) < 128 ) {
int current = 0;
std::vector<int> data( 5, 0 );
for( unsigned int k = j - 1; k < image.cols; k++ ){
unsigned char value = image.at<unsigned char>(i, k);
if( current % 2 == 0 ) {
if( value < 128 ) {
data[current]++;
if(data[current] > maxfeature){
j = max(j, k);
break;
}
}else{
current++;
}
}else{
if( value > 128 ) {
data[current]++;
if(data[current] > maxfeature){
j = max(j, k);
break;
}
}else{
current++;
}
}
if( current == 5 ){
const int size = VectorSize( data );
if( isFinder( size, data ) ){
retVal.push_back( Location( i, j + ( size / 2.0 ), size ) );
//j = max(j, k);
j += data[2] + data[3] + data[4];
}
//j += 1;//data[0];// + data[1];
break;
}
}
}
}
}
return retVal;
}
const std::vector<Location> FindMarksVertical( Mat image, const int maxfeature ){
std::vector<Location> retVal;
for( unsigned int i = 0; i < image.cols; i++ ){
for( unsigned int j = 0; j < image.rows; j++ ){
if( image.at<unsigned char>(j, i) < 128 ) {
int current = 0;
std::vector<int> data( 5, 0 );
for( unsigned int k = j - 1; k < image.rows; k++ ){
unsigned char value = image.at<unsigned char>(k, i);
if( current % 2 == 0 ) {
if( value < 128 ) {
data[current]++;
if(data[current] > maxfeature){
j = max(j, k);
break;
}
}else{
current++;
}
}else{
if( value > 128 ) {
data[current]++;
if(data[current] > maxfeature){
j = max(j, k);
break;
}
}else{
current++;
}
}
if( current == 5 ){
const int size = VectorSize( data );
if( isFinder( size, data ) ){
retVal.push_back( Location( j + ( size / 2.0 ), i , size) );
//j = max(j, k);
j += data[2] + data[3] + data[4];
}
//j += 1;//data[0];// + data[1];
break;
}
}
}
}
}
return retVal;
}
void DrawCross( Mat image, const int row, const int column, const int size ){
image.at<Vec3b>(row, column) = Red;
// Draw Horizontal Line
for( int i = 0; i < size; i ++){
image.at<Vec3b>(row-i, column) = Red;
image.at<Vec3b>(row+i, column) = Red;
}
// Draw Vertical Line
for( int i = 0; i < size; i ++){
image.at<Vec3b>(row, column-i) = Red;
image.at<Vec3b>(row, column+i) = Red;
}
}
int main( int argc, char **argv ) {
// Confirm we have enough parameters
if( argc != 2 ) {
std::cerr << "ComputerVision2 <imagefile>" << std::endl;
return -1;
}
// Read Image
Mat image = imread( argv[1], 1 );
const int cols = image.cols;
const int rows = image.rows;
float scale = 1.0;
if( std::abs(cols - 1950) > 200){
scale = (float)cols / 1950;
const float inv_scale = 1.0 / scale;
const int newrows = (int)(rows * inv_scale + 0.5);
resize(image, image, Size(1950, newrows), 0, 0);
}
//std::cout << "Scale " << scale << std::endl;
//pixels per feature
const int halfpixelsperfeature = max(((image.rows / 21 / 7) + (image.cols / 28 / 7)) / 4, 1);
const int maxfeature = halfpixelsperfeature * 2 * 3 * 2;
Mat imblur;
blur( image, imblur, Size( halfpixelsperfeature/2, halfpixelsperfeature/2 ), Point(-1,-1) );
// Convert to Grayscale
Mat gray;
cvtColor(imblur, gray, CV_BGR2GRAY);
// Threshold Image
Mat temp;
adaptiveThreshold( gray, temp, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 127, 10 );//127, ADAPTIVE_THRESH_MEAN_C, ADAPTIVE_THRESH_GAUSSIAN_C
// Test horizontally and vertically
const std::vector<Location> Horizontal = FindMarksHorizontal( temp, maxfeature );
const std::vector<Location> Vertical = FindMarksVertical( temp, maxfeature );
// Find Centers
std::vector<Location> locs;
for( int i = 0; i < Horizontal.size(); i++ ){
for( int j = 0; j < Vertical.size(); j++ ){
if( Horizontal[i] == Vertical[j] ){
const int locsize = locs.size();
bool newpoint = true;
for( int k=0; k<locsize; k++){
const Location loc = locs[k];
if(std::abs(loc.X - Horizontal[i].X) < 5 && std::abs(loc.Y - Horizontal[i].Y) < 5){
newpoint = false;
break;
}
}
if(newpoint){
std::cout << "Point: " << (int)(Horizontal[i].X * scale + 0.5) << ", " << (int)(Horizontal[i].Y * scale + 0.5) << ", " << (int)(((Horizontal[i].size + Vertical[j].size) / 2) * scale + 0.5)<< std::endl;
DrawCross( image, Horizontal[i].X, Horizontal[i].Y, 64 );
locs.push_back(Horizontal[i]);
}
}
}
}
// Draw to screen
//cv::namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
//cv::imshow( "Display Image", image );
//while( cv::waitKey( 1 ) != 'q' ) {}
cv::imwrite( "result.jpg", image );
cv::imwrite( "grey.jpg", temp );
return 0;
}