How can I get the position and draw rectangle using opencv?

前端 未结 2 1096
梦如初夏
梦如初夏 2020-11-28 16:59

I want to get a position when move and click mouse in picturebox. I want to create rectangle in the image window when and where a mouse is clicked.

<

相关标签:
2条回答
  • 2020-11-28 17:30

    So you have a problem unrelated to your question.

    However, you can achieve your goal using only OpenCV highgui facilites:

    #include <opencv2\opencv.hpp>
    #include <iostream>
    
    using namespace std;
    using namespace cv;
    
    vector<Rect> rects;
    bool bDraw;
    Rect r;
    Point base;
    
    Mat3b img;
    Mat3b layer;
    Mat3b working;
    
    
    void CallBackFunc(int event, int x, int y, int flags, void* userdata)
    {
        if ( event == EVENT_LBUTTONDOWN )
        {
            cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;    
    
            // Init your rect
            base.x = x;
            base.y = y;
            r.x = x;
            r.y = y;
            r.width = 0;
            r.height = 0;
            bDraw = true;
        }        
        else if ( event == EVENT_MOUSEMOVE )
        {
            cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;
    
            // If drawing, update rect width and height
            if(!bDraw) return;
    
            int dx = abs(r.x - x);
            int dy = abs(r.y - y);
    
            if(x < base.x) {
                r.x = x;
                r.width = abs(x - base.x);
            } else {
                r.width = dx;
            }
    
            if(y < base.y) {
                r.y = y;
                r.height = abs(y - base.y);
            } else {
                r.height = dy;
            }
    
            // Refresh
            working = layer.clone();
            rectangle(working, r, Scalar(0,255,0));
            imshow("My Window", working);
        }
        else if ( event == EVENT_LBUTTONUP)
        {
            cout << "Left button released" << endl;
    
            // Save rect, draw it on layer
            rects.push_back(r);
            rectangle(layer, r, Scalar(0,255,255));
    
            r = Rect(); 
            bDraw = false;
    
            // Refresh
            working = layer.clone();
            rectangle(working, r, Scalar(0,255,0));
            imshow("My Window", working);
        }
    }
    
    int main(int argc, char** argv)
    {
        bool bDraw = false;
        bool isDragging = false;
    
        // Read image from file 
        img = imread("path_to_image");
    
        // initialize your temp images
        layer = img.clone();
        working = img.clone();
    
        //if fail to read the image
        if( img.empty() ) 
        { 
            cout << "Error loading the image" << endl;
            return -1; 
        }
    
        //Create a window
        namedWindow("My Window", 1);
    
        //set the callback function for any mouse event
        setMouseCallback("My Window", CallBackFunc, NULL);
    
        //show the image
        imshow("My Window", working);
    
        // Wait until user presses 'q'
        while((waitKey(1) & 0xFF) != 'q');
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-28 17:36

    In tracking module of opencv-contrib, there is a nice feature of selectROI.

    #include <opencv2/opencv.hpp>
    // selectROI is part of tracking API
    #include <opencv2/tracking.hpp>
    
    using namespace std;
    using namespace cv;
    
    
    int main (int argc, char **arv)
    {
        // Read image
        Mat im = imread("image.jpg");
    
        // Select ROI
        Rect2d r = selectROI(im, false); // false -> for creating rectangle from 
                                         //          top-left to bottom-right
    
        // Crop image
        Mat imCrop = im(r);
    
        // Display Cropped Image
        imshow("Image", imCrop);
        waitKey(0);
    
        return 0;
    }
    

    Use the mouse to select the ROI(region of interest) and then press SPACE or ENTER button. Cancel the selection process by pressing c button.

    0 讨论(0)
提交回复
热议问题