Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9052777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:15:51+00:00 2026-06-16T13:15:51+00:00

I’m using opencv library in C++ in my project and i’m having problems using

  • 0

I’m using opencv library in C++ in my project and i’m having problems using the MouseCallback.

I have a class BoardCalibration which has two data members, which I need to use them inside the callback function. You can see this class below:

class BoardCalibration{
private:
    Rect _box;  <-- data members i need to upadte inside the callback function
    bool _drawingBox; <--

public:
    BoardCalibration();
    static void my_mouse_callback(int event, int x, int y, int flags, void* param);
    Rect calibrate(Mat& image);
    void drawBox(IplImage* img);
};

At the calibrate() method im calling the function which recieves the callback my_mouse_callback function. Code:

Rect BoardCalibration::calibrate(Mat& image){
    IplImage * img = new IplImage(image);

    namedWindow("Calibration");

    IplImage *temp = cvCloneImage(img);
    cvSetMouseCallback("Calibration", my_mouse_callback, (void *)img);

    while (1){

        imshow("Calibration", Mat(img));
        cvCopyImage(img,temp);

        if( _drawingBox ){
            drawBox(temp);
        }

        imshow("Calibration", Mat(temp));

        if (waitKey(1)>=0)
            break;
    }

    cout << "calibrated\n";

    delete img;
    return _box;
}

And at implementation of my_mouse_callback is:

static void my_mouse_callback(int event, int x, int y, int flags, void* param){

    IplImage* image = (IplImage*) param;

    switch( event ) {
    case CV_EVENT_MOUSEMOVE: {
        if( _drawingBox ) {
            _box.width  = x-_box.x;
            _box.height = y-_box.y;
        }
                             }
                             break;
    case CV_EVENT_LBUTTONDOWN: {
        _drawingBox = true;
        _box = Rect( x, y, 0, 0 );
                               }
                               break;   
    case CV_EVENT_LBUTTONUP: {
        _drawingBox = false; 
        if( _box.width<0 ) { 
            _box.x+=_box.width;  
            _box.width *=-1; 
        }
        if( _box.height<0 ) { 
            _box.y+=_box.height; 
            _box.height*=-1; 
        }

        //drawBox(image, box);  // keep draw on screen
        // display rectangle coordinates
        cout << "TopLeft: (" << _box.x << "," << _box.y << "), BottomRight: (" 
            << _box.x+_box.width << "," << _box.y+_box.height << ")" << endl;
                             }
                             break;
    }
}

As you can see i’m trying to reach the _box and _drawingBox members here but because it’s static method it does not recognize them.
How can I solve this problem?? I can’t change the prototype of my_mouse_callback otherwise it won’t accepted by cvSetMouseCallback.
I also can’t define those data member outside of the class because its giving me error that they already defined.
Is there anything else I can try???
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:15:52+00:00Added an answer on June 16, 2026 at 1:15 pm

    I don’t know anything about opencv but how about something like this

    struct Helper
    {
        IplImage * pI;
        BoardCalibration * pObj;
    };
    
    
    Rect BoardCalibration::calibrate(Mat& image)
    {
        .... stuff ....
    
        Helper * p = new Helper;
        p->pI = img;
        p->pObj = this;
    
        cvSetMouseCallback("Calibration", my_mouse_callback, (void *)p);
    
        .... stuff ...   
    
    
        delete p;
        delete img;
        return _box;
    
    }
    
    
    static void BoardCalibration::my_mouse_callback(int event, int x, int y, int flags, void* param)
    {
        Helper * p = (Helper *)param;
        IplImage* image = p->pI;
        BoardCalibration * pBC = p->pObj;
    
        switch( event ) 
        {
            case CV_EVENT_MOUSEMOVE: 
            {
                if( pBC->_drawingBox )  // use the pBC pointer
    
                ... stuff ...
    
    
        }
    
        ... stuff ...
    
    }
    

    I don’t know the flow of your code to figure out when exactly the helper object should be deleted. So I have the delete Helper object code near the delete img because if that code was correct, then this would probably be the right place to delete the Helper object also. But you need to check that. You need to delete this objects only when you are sure that the callback would have finished running for that call by then.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which has BIG numbers and small numbers in it. I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I am using jsonparser to parse data and images obtained from json response. When
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.