Ask Your Question
0

select multiple points with mouse (opencv 2.4.9,vs2010,winforms)

asked 2016-03-04 07:43:37 -0600

kazar13 gravatar image

updated 2016-03-04 07:55:41 -0600

berak gravatar image

Hi,

I'm trying to select multiple points by mouse (with cv::EVENT_LBUTTONDOWN).

So I'm passing the std::vector<cv::point> to the Callback-Function:

//setting the roi manually
std::vector<cv::Point> points;   

   cv::Mat img = cv::imread("f:/open/me.jpg");

   cv::namedWindow("Original");

   cv::setMouseCallback("Original", mouse_call, (void*)&points);

   int X,Y;


   cv::imshow("Original",img);

And in Callback:

static void mouse_call(int event,int x,int y, int, void* param)
{
      if(event==cv::EVENT_LBUTTONDOWN){

        std::vector<cv::Point>* ptPtr = (std::vector<cv::Point>*)param;

            ptPtr->push_back(cv::Point(x,y));


    }

And, when I'm trying to access the vector-elements with:

points[0].x

points[0].y

It's compiling, but at runtime i get an error: "Debug Assertion Failed " "Expression: vector subscript out of range"

Any clue ? Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-03-04 11:35:28 -0600

berak gravatar image

updated 2016-03-04 11:36:15 -0600

you can't assume, there's always something in your vector, so you need to care for the case it's empty:

if (points.size() > 0)
{
    points[0].x
    points[0].y
}
edit flag offensive delete link more

Comments

I've added following statement:

`if (points.size() > 0) //we do have points in vector

{ MessageBox::Show(System::Convert::ToString(points[0].x));

    }

`

After i click several times on image, i don't get any message box returned with coordinate. Am I still doing something wrong ?

kazar13 gravatar imagekazar13 ( 2016-03-05 11:10:29 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-03-04 07:43:37 -0600

Seen: 1,268 times

Last updated: Mar 04 '16