1 | initial version |
again, i'm pretty sure, you won't need this, since you can easily infer the rectangle from the users position, and the position of the FloorMat , it's just plain maths after all, but here we go:
Mat ocv = imread("squares.jpg");
Mat image;
cvtColor(ocv,image,COLOR_BGR2GRAY);
threshold(image,image,20,255,1); // white on black required
vector<vector<Point>> contours;
findContours(image,contours,RETR_CCOMP,CHAIN_APPROX_SIMPLE);
vector<Rect> rects;
for ( int i=0; i<contours.size(); i++)
{
double a = contourArea(contours[i]);
if ( a > 5000 && a < 50000) // filter by size
{
drawContours(ocv,contours,i,Scalar(200,0,0));
Rect r = boundingRect(contours[i]);
rects.push_back(r);
}
}
// for the fun of it, let's sort the rects l->r, t->d:
struct Less
{
bool operator () (const Rect &a, const Rect &b)
{
return (a.x + a.y*10000) < (b.x + b.y*10000);
}
};
std::sort(rects.begin(),rects.end(), Less());
cerr << Mat(rects) << endl;
[6, 3, 89, 70;
96, 3, 99, 70;
196, 3, 87, 70;
6, 74, 89, 91;
96, 74, 99, 91;
196, 74, 87, 91;
6, 166, 89, 77;
96, 166, 99, 77;
196, 166, 87, 77]
2 | No.2 Revision |
again, i'm pretty sure, you won't need this, since you can easily infer the rectangle from the users position, and the position of the FloorMat , it's just plain maths after all, all
Rect floorMat(20,20, 600,600); // let's assume, your mat is at 20,20, and it's 600 wide.
Point user(344,523); // arbitrary user position
user -= floorMat.tl(); // translate to floorMat coords
cerr << user << endl;
int ix = int(user.x) / (floorMat.width / 3); // get x,y square index
int iy = int(user.y) / (floorMat.height/ 3);
cerr << ix << " " << iy << endl;
int rect_id = ix + iy *3 + 1; // +1, since you insist on starting to count at 1
cerr << "user is in square " << rect_id << endl;
[324, 503]
1 2
user is in square 8
but here we go:
Mat ocv = imread("squares.jpg");
Mat image;
cvtColor(ocv,image,COLOR_BGR2GRAY);
threshold(image,image,20,255,1); // white on black required
vector<vector<Point>> contours;
findContours(image,contours,RETR_CCOMP,CHAIN_APPROX_SIMPLE);
vector<Rect> rects;
for ( int i=0; i<contours.size(); i++)
{
double a = contourArea(contours[i]);
if ( a > 5000 && a < 50000) // filter by size
{
drawContours(ocv,contours,i,Scalar(200,0,0));
Rect r = boundingRect(contours[i]);
rects.push_back(r);
}
}
// for the fun of it, let's sort the rects l->r, t->d:
struct Less
{
bool operator () (const Rect &a, const Rect &b)
{
return (a.x + a.y*10000) < (b.x + b.y*10000);
}
};
std::sort(rects.begin(),rects.end(), Less());
cerr << Mat(rects) << endl;
[6, 3, 89, 70;
96, 3, 99, 70;
196, 3, 87, 70;
6, 74, 89, 91;
96, 74, 99, 91;
196, 74, 87, 91;
6, 166, 89, 77;
96, 166, 99, 77;
196, 166, 87, 77]
3 | No.3 Revision |
again, i'm pretty sure, you won't need this, since you can easily infer the rectangle from the users position, and the position of the FloorMat , it's just plain maths after all
Rect floorMat(20,20, 600,600); // let's assume, your mat is at 20,20, and it's 600 wide.
Point user(344,523); // arbitrary user position
user -= floorMat.tl(); // translate to floorMat coords
cerr << user << endl;
int ix = int(user.x) / (floorMat.width / 3); // get x,y square index
int iy = int(user.y) / (floorMat.height/ 3);
cerr << ix << " " << iy << endl;
int rect_id = ix + iy *3 + 1; // +1, since you insist on starting to count at 1
cerr << "user is in square " << rect_id << endl;
[324, 503]
1 2
user is in square 8
but here we go:go, let's try to detect the squares as well:
Mat ocv = imread("squares.jpg");
Mat image;
cvtColor(ocv,image,COLOR_BGR2GRAY);
threshold(image,image,20,255,1); // white on black required
vector<vector<Point>> contours;
findContours(image,contours,RETR_CCOMP,CHAIN_APPROX_SIMPLE);
vector<Rect> rects;
for ( int i=0; i<contours.size(); i++)
{
double a = contourArea(contours[i]);
if ( a > 5000 && a < 50000) // filter by size
{
drawContours(ocv,contours,i,Scalar(200,0,0));
Rect r = boundingRect(contours[i]);
rects.push_back(r);
}
}
// for the fun of it, let's sort the rects l->r, t->d:
struct Less
{
bool operator () (const Rect &a, const Rect &b)
{
return (a.x + a.y*10000) < (b.x + b.y*10000);
}
};
std::sort(rects.begin(),rects.end(), Less());
cerr << Mat(rects) << endl;
[6, 3, 89, 70;
96, 3, 99, 70;
196, 3, 87, 70;
6, 74, 89, 91;
96, 74, 99, 91;
196, 74, 87, 91;
6, 166, 89, 77;
96, 166, 99, 77;
196, 166, 87, 77]