Skip to content

Commit ae04f5b

Browse files
author
Milind L
committed
This makes getting board work regardless of the initial random numbering of gotis.
1 parent e2cc2e3 commit ae04f5b

File tree

2 files changed

+86
-5
lines changed

2 files changed

+86
-5
lines changed

getting_board.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdio.h>
55
#include <stdlib.h>
66
#include <sstream>
7+
#include "sort_board_as_per_location.h"
78
using namespace std;
89
using namespace cv;
910

@@ -105,13 +106,19 @@ RotatedRect *label(VideoCapture cam) {
105106
//Start main loop:
106107
while(key!=27) {
107108
//Read and threshold image. Clean up.
109+
108110
cam.read(src);
109111
cvtColor(src,hsv, CV_BGR2HSV);
110-
inRange(hsv, Scalar(0, sL, vL), Scalar(179, sH,vH),holded);
111-
inRange(hsv, Scalar(0, sL1, vL1), Scalar(179, sH1,vH1),holded1);
112+
inRange(hsv, Scalar(0, sL, vL), Scalar(60, sH,vH),holded);
113+
//threshold(holded, holded, 15, 255, THRESH_BINARY);
114+
inRange(hsv, Scalar(70, sL1, vL1), Scalar(179, sH1,vH1),holded1);
115+
//threshold(holded1, holded1, 12, 255, THRESH_BINARY);
112116
medianBlur(holded, holded, 15);
113117
medianBlur(holded1, holded1, 15);
114-
118+
//imshow("holded_for_white", holded);
119+
//imshow("holded_for_black", holded1);
120+
waitKey(33);
121+
//destroyAllWindows();
115122
//Fiond contours for black and white , find moments
116123
vector<vector<Point> > contours;
117124
vector<Vec4i> hierarchy;
@@ -151,6 +158,7 @@ RotatedRect *label(VideoCapture cam) {
151158
if(flag==1) continue;
152159
if(checkContour(contours[i])==false) continue;
153160
RotatedRect r1 = minAreaRect( Mat(contours[i]));
161+
waitKey(1);
154162
/* start drawing functions */
155163
Point2f r[4]; r1.points(r);
156164
for( int j = 0; j < 4; j++ ){
@@ -163,6 +171,7 @@ RotatedRect *label(VideoCapture cam) {
163171
s = out.str();
164172
putText(drawing, s, mc[i], 1,1, Scalar(255,255,255));
165173
rects[k] = r1;
174+
166175
/* end drawing functions */
167176
k++;
168177
if(k==32) { //k=32 implies we are finished with the chess blocks
@@ -171,15 +180,19 @@ RotatedRect *label(VideoCapture cam) {
171180
}
172181

173182
}
174-
183+
sort_array_y(rects, 32);
184+
for(int m=0; m!=8; m++) {
185+
sort_array_x(rects+(4*m), 4);
186+
}
187+
print_array(rects, 32);
175188
k=0; //reset white/black counter for black
176189
for( int i = 0; i< contours1.size(); i++ )
177190
{
178191

179192
if(flag2==1) continue;
180193
if(checkContour(contours1[i])==false) continue;
181194
RotatedRect r1 = minAreaRect( Mat(contours1[i]));
182-
195+
waitKey(1);
183196
/* start drawing functions */
184197
Point2f r[4]; r1.points(r);
185198
for( int j = 0; j < 4; j++ ){
@@ -199,6 +212,11 @@ RotatedRect *label(VideoCapture cam) {
199212
drawing.copyTo(final2);
200213
}
201214
}
215+
sort_array_y(rects+32, 32);
216+
for(int m=0; m!=8; m++) {
217+
sort_array_x(32+rects+(4*m), 4);
218+
}
219+
print_array(rects+32, 32);
202220

203221
imshow("COM", src+drawing); //Display current drawn imgs
204222
key=waitKey(30);

sort_board_as_per_location.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* in this routine I need to sort the board in positional order. I will be supplied an array of rotated rects
2+
* I will need to first sort as per y and then sort each sub array of 8 in terms of x */
3+
4+
#include "opencv2/highgui/highgui.hpp"
5+
#include "opencv2/imgproc/imgproc.hpp"
6+
#include <iostream>
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <sstream>
10+
using namespace std;
11+
using namespace cv;
12+
//RotatedRectName.center.x;
13+
void swap(RotatedRect *a, RotatedRect *b) {
14+
RotatedRect temp = *a;
15+
*a = *b;
16+
*b=temp;
17+
}
18+
19+
void sort_array_y(RotatedRect *arr, int N) { //sort as per y
20+
int i, j;
21+
for(i=0; i!=N; i++) {
22+
for(j=0; j!=N; j++) {
23+
if(arr[j].center.y>arr[i].center.y) swap(arr[j], arr[i]);
24+
}
25+
}
26+
}
27+
28+
void sort_array_x(RotatedRect *arr, int N) { //sort as per x
29+
int i, j;
30+
for(i=0; i!=N; i++) {
31+
for(j=0; j!=N; j++) {
32+
if(arr[j].center.x>arr[i].center.x) swap(arr[j], arr[i]);
33+
}
34+
}
35+
}
36+
37+
//Test function
38+
void print_array(RotatedRect *arr, int N) {
39+
for(int i=0; i!=N; i++) {
40+
cout<<"X,Y coordinate of point number "<<i<<" is "<<arr[i].center.x<<" , "<<arr[i].center.y<<endl;
41+
}
42+
}
43+
/*
44+
int main() {
45+
RotatedRect r1[] = {RotatedRect(Point2f(1,2), Size2f(1,2), 90),
46+
RotatedRect(Point2f(2,5), Size2f(1,2), 90),
47+
RotatedRect(Point2f(6,9), Size2f(1,2), 90),
48+
RotatedRect(Point2f(7,0), Size2f(1,2), 90)};
49+
//Treat as 3*2 array
50+
print_array(r1, 4);
51+
cout<<"Printed the normal shit"<<endl;
52+
sort_array_y(r1, 4);
53+
print_array(r1, 4);
54+
cout<<"Printed the sorted sorted shit"<<endl;
55+
for(int m=0; m!=2; m++) {
56+
sort_array_x(r1+m*2, 2);
57+
}
58+
print_array(r1, 4);
59+
cout<<"Printed the y sorted shit"<<endl;
60+
return 0;
61+
}
62+
63+
*/

0 commit comments

Comments
 (0)