Ask Your Question
0

Detecting multiple squares within a square and extracting their coordinates

asked 2015-02-15 04:23:41 -0500

Sweptica gravatar image

updated 2015-02-15 05:41:20 -0500

I would like to be able to detect individual squares inside the a block and extract their coordinates,

Essentially i'm trying to create a DDR (dance dance revolution) game using opencv and kinect. To detect where the user's are stepping inside the block.

I know there are many edge detection algorithms but i'm not sure where to start.

Here is a picture of the block, I want to detect where the user is stepping (1,2,3..etc) or more specifically the boundary of each square. For example, how does my program detect if the user is within the boundary of square 1 or 2 etc

image description

edit retag flag offensive close merge delete

Comments

i guess, you don't even have to detect squares explicitly, more the position of the user.

berak gravatar imageberak ( 2015-02-15 04:34:32 -0500 )edit

The users position i can easily get from the skeletal frame of the kinect, the issue is how will i check what are the boundaries of the square. For example, after I place this mat on the floor how will my program detect if the user is in the boundary of square 1, 2 or 3 etc. . .

Sweptica gravatar imageSweptica ( 2015-02-15 05:36:50 -0500 )edit

if you know the position, you already know , which square the user is on.

berak gravatar imageberak ( 2015-02-15 05:46:29 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
5

answered 2015-02-15 06:24:30 -0500

berak gravatar image

updated 2015-02-16 03:11:03 -0500

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, 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]

image description image description

edit flag offensive delete link more

Comments

Berak before you posted the updates to your existing answer, i'm getting an error over your previous code Unhandled exception at 0x000007fefdc3940d in Opencv_Test.exe: Microsoft C++ exception: cv::Exception at memory location 0x002af520..

I have tried to debug as much as I can, but i can't seem to catch what's causing the exception

Sweptica gravatar imageSweptica ( 2015-02-16 09:41:47 -0500 )edit

well, first you will have to find out where it fails.

either use a debugger (that's the pro version) , or put a lot of cerr << "here"; statements (amateur).

also, please check, if you strictly link to opencv debug libs with a debug build, and release ones for the release build.

berak gravatar imageberak ( 2015-02-16 09:53:19 -0500 )edit

I'm getting an assertion fail error, on the line double a = contourArea(contours[i]); "vector subscript out of range"

Sweptica gravatar imageSweptica ( 2015-02-17 00:44:03 -0500 )edit

ah, so you probably changed the code a bit, used a different floormat img and such ?

if so, either edit the question, and append your code, or maybe even it's time for a new question ?

berak gravatar imageberak ( 2015-02-17 00:48:03 -0500 )edit

Berak, the code i have used is the exact one you've provided above without the floorMat or user detection part. (i'm using vs2010)

Sweptica gravatar imageSweptica ( 2015-02-17 01:08:11 -0500 )edit
Login/Signup to Answer

Question Tools

3 followers

Stats

Asked: 2015-02-15 04:23:41 -0500

Seen: 2,650 times

Last updated: Feb 16 '15