-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_objects.cpp
161 lines (110 loc) · 3.51 KB
/
get_objects.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
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
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
Mat process_division_circle(Mat image){
/*assume 1-channel input
*
*/
Mat smooth, div;
Mat rectKernel = getStructuringElement(MORPH_RECT, Size(5,5));
Mat circKernel = getStructuringElement(MORPH_ELLIPSE, Size(5,5));
morphologyEx(image, smooth, MORPH_DILATE, rectKernel);
medianBlur(smooth, smooth, 5);
morphologyEx(smooth, smooth, MORPH_DILATE, circKernel);
divide(image, smooth, div, 255.0);
return div;
}
Rect circle_to_square(int x, int y, int r){ //fun fact: openCV has a Rect_ class for rectangles
/*create a rectangle from the given
* circle ands its center points
* radius size for the square is
* gonna be like 60 or 70 */
int cornerX = x - 50;
int cornerY = y - 50;
Point corner(cornerX, cornerY);
Size squareDim(100,100);
Rect ret(corner, squareDim);
return ret;
}
vector<Rect> get_objects(Mat image){
/*take grayscale image and return
* predicted bounding boxes in the valid area
*
* output: a vector containing the circles
* */
vector<Vec3f> circles;
vector<Rect> boxes;
int boxRadius = 50; //parameter might need tuning
Mat div = process_division_circle(image);
//get our circles
HoughCircles(div, circles, CV_HOUGH_GRADIENT, 1, 150, 255, 45, 0, 60);
for (int i = 0; i < circles.size(); i++){
//check for any points out of bounds
if (circles[i][0] < 100 || circles[i][0] > 900){
circles.erase(circles.begin() + i);
}
else if (circles[i][1] < 100 || circles[i][1] > 900){
circles.erase(circles.begin() + i);
}
//normal case
else{
Rect toPush = circle_to_square(cvRound(circles[i][0]), cvRound(circles[i][1]), boxRadius);
boxes.push_back(toPush);
}
}
return boxes;
}
vector<int> everyOtherEven(int n){
vector<int> ret;
for (int i = 2; i < n; i+= 4){
ret.push_back(i);
}
return ret;
}
bool compareRects(Rect leftRect, Rect rightRect){
if (leftRect.y == rightRect.y){ return leftRect.x < rightRect.x;}
return (leftRect.y < rightRect.y);
}
int main(){
string filename = "data/images/Test21_1.tif";
Mat image = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
if (!image.data){
cout << "ur image sux" << endl;
return -1;
}
Mat div = process_division_circle(image);
vector<Vec3f> circles;
HoughCircles(div, circles, CV_HOUGH_GRADIENT, 1, 150, 255, 45, 0, 60);
vector<Rect> rectangles = get_objects(image);
sort(rectangles.begin(), rectangles.end(), compareRects);
vector<int> swap_indices = everyOtherEven(rectangles.size());
for( int i = 0; i < rectangles.size(); i++ )
{
//TODO THIS EDGE CASE FOR THE RECTANGLES
//if( find(swap_indices.begin(), swap_indices.end(), i) != swap_indices.end() )
if(i % 2 == 0){
if (rectangles[i].x > rectangles[i+1].x){
//cout << "swap attempt" << endl;
Rect temp = rectangles[i];
rectangles[i] = rectangles[i+1];
rectangles[i+1] = temp;
}
}
//cout << "x: "<< rectangles[i].x << " y: " << rectangles[i].y << endl;
rectangle(image, rectangles[i], Scalar(0,0,255), 1, 8, 0 );
//present a circle
Point center = Point(circles[i][0], circles[i][1]);
int radius = 20;
circle(image, center, radius, Scalar(0,0,255), 3, LINE_AA);
}
namedWindow("Display window", WINDOW_AUTOSIZE);
imshow("Display window", image);
waitKey(0);
return 0;
}