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

前端 未结 2 1098
梦如初夏
梦如初夏 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:36

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

    #include 
    // selectROI is part of tracking API
    #include 
    
    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.

提交回复
热议问题