Ask Your Question
1

coordinates of detected target

asked 2013-07-18 02:49:38 -0600

MuathGH gravatar image

updated 2020-10-23 09:33:46 -0600

hel

hello , im trying to recover the coordination of a detected target and print it , im not expert in openCV neither C++ , this is my first code and i really need you're help , here's the code:

 #include "stdafx.h"
#include <highgui.h>
#include <opencv2/video/background_segm.hpp>
#include<opencv2/opencv.hpp>
#include<vector>
#include<opencv2\imgproc\imgproc.hpp>
#include <cv.h>
#include <ostream>
#include<ostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{


    cv::BackgroundSubtractorMOG2 bg;
    bg.bShadowDetection=false;
    bg.nmixtures=3;
    bg.history=100;
    bg.varThreshold=10; 
    cv::Mat frame;
    cv::Mat fore;
    cv::Mat back;


    CvCapture* capture  = cvCaptureFromCAM(0); // capture from video device 


    cvNamedWindow("Orignal", CV_WINDOW_AUTOSIZE); // create a window to display the images
      cvNamedWindow("Processed", CV_WINDOW_AUTOSIZE); // create a window to display the images

    while(1) {

        frame=cvQueryFrame(capture);   // Grabs and returns a frame from a 
        bg.operator()(frame,fore);
        bg.getBackgroundImage(back);
        cv::erode(fore,fore,cv::Mat()); // to clear the noise   
        cv::dilate(fore,fore,cv::Mat()); // to smooth the objecty


        cv::imshow("Orignal",frame);
        cv::imshow("Processed",fore);

       if(cvWaitKey(10)==27) // to exit the loop using ESCP 

        break;
    }
 return 0;
}

so the detected object is in fore matrix , i need to get its X,Y position printed out .. is it possible ?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-07-18 03:35:28 -0600

First of all apply the findContours function onto the foreground image. This will look for contours in your binary image, around the blobs that you have segmented before with the background segmentation algorithm.

Each of these contours is found as a vector of points, which give you the exact location of the blob, in image coordinates. If this information is enough, then you can stop here.

Next step is to find the smallest bounding box for the blob, which will accurately describe the location of the blob, in fewer coordinates than the list of vector points. This can be done by applying the boundingRect function on each of the contours. This will return a rectangle object, which contains a x and y coordinate of the top left corner and a width and heigth parameter.

You could also calculate the mass center, using the moments of the contour as explained in this tutorial.

edit flag offensive delete link more

Comments

ok , i applied the findContours function on the foreground image and boundingRect : cv::findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); cv::boundingRect(contours);

but every time i try to run the program , this happens : Unhandled exception at 0x000007fefd42a49d in test_For_opencv2_3.exe: Microsoft C++ exception: cv::Exception at memory location 0x0019de50..

MuathGH gravatar imageMuathGH ( 2013-07-18 05:04:39 -0600 )edit

This is because you didn't create the contours matrix before adressing it, so basically you are acessing memory that doesn't exist, thus leading to this problem. Add Mat contours; right before it.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-07-18 05:09:44 -0600 )edit

Unhandled exception at 0x000007fefd42a49d in test_For_opencv2_3.exe: Microsoft C++ exception: cv::Exception at memory location 0x002bd810..

same thing , i've created the matrix but still get this error

MuathGH gravatar imageMuathGH ( 2013-07-18 05:14:12 -0600 )edit

Use debugger and check at what location/rule it exactly crashes, I have the idea that something must be going wrong before this.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-07-18 05:56:56 -0600 )edit

Question Tools

Stats

Asked: 2013-07-18 02:49:38 -0600

Seen: 4,671 times

Last updated: Jul 18 '13