Ask Your Question
1

I'm trying to draw a rectangle on my mouse click coordinates?

asked 2017-09-17 04:32:46 -0500

Splintersfury gravatar image

I am very new to opencv. So basically it compiles and runs well but I am unable to see the rectangle on my image. Can someone help me with this? Thank you so much :)

----------
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
Point point1, point2;


void onmouse(int event, int x, int y, int flags, void* param)
{
    if(event==CV_EVENT_LBUTTONDOWN)
    {
            Mat image = imread("mozart.jpg");
        point1 = Point(x, y);
        point2 = Point(x + 20, y + 20);
        rectangle(image, point1, point2,CV_RGB(0,255,0),1);
        std::cout << "(" << x << ", " << y << ")" << "\n";

    }
}

int main( int argc, char** argv )
{


Mat image, dst;

/// Read image ( same size, same type )
image = imread("mozart.jpg");


namedWindow("My window", 1);
imshow("My window",image);

setMouseCallback("My window", onmouse, &image);

waitKey();
return 0;
}


----------
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-09-17 06:01:39 -0500

take a look at this code

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

#include <iostream>

using namespace std;
using namespace cv;

bool selectObject = false;
Rect selection;
Point origin;
Mat image, canvas;
String win_name = "Cropping Demo";
vector<Rect> selections;

static bool showSelections()
{
    for (size_t i = 0; i< selections.size(); i++)
    {
        rectangle(canvas, selections[i], Scalar(0, 255, 0), 2);
    }
    imshow(win_name, canvas);
    return true;
}

static void onMouse(int event, int x, int y, int, void*)
{
    switch (event)
    {
    case CV_EVENT_LBUTTONDOWN:
        origin = Point(x, y);
        selectObject = true;
        break;
    case CV_EVENT_LBUTTONUP:
    {
        selectObject = false;
        selections.push_back(selection);
        showSelections();
        break;
    }
    }

    if (selectObject)
    {
        selection.x = MIN(x, origin.x);
        selection.y = MIN(y, origin.y);
        selection.width = std::abs(x - origin.x) + 1;
        selection.height = std::abs(y - origin.y) + 1;
        selection &= Rect(0, 0, image.cols, image.rows);

        if (selection.width > 0 && selection.height > 0)
        {
            Mat canvascopy;
            canvas.copyTo(canvascopy);
            Mat roi = canvascopy(selection);
            bitwise_not(roi, roi);
            imshow(win_name, canvascopy);
        }
    }
}


int main(int argc, const char** argv)
{
    String file_name = argv[1];
    image = imread(file_name);
    image.copyTo(canvas);

    namedWindow(win_name);
    setMouseCallback(win_name, onMouse);

    imshow(win_name, image);

    while (true)
    {
        int key = waitKey(0);

        if (key == 27)
            break;

        if (key == 's') // crops and saves selections
        {
            for (size_t i = 0; i < selections.size(); i++)
            {
                String save_file_name = file_name + format("%02d.jpg", i);
                imwrite(save_file_name, image(selections[i]));
            }
        }

        if ( (key == 'd') & (selections.size() > 0) ) // delete last selection
        {
            selections.erase(selections.end() -1 );
            image.copyTo(canvas);
            showSelections();
        }
    }
    return 0;
}
edit flag offensive delete link more

Comments

Oh wow i got it now, thank you so much for this reference man! Sorry, I want to upvote but I cant cause i'm new :(

Splintersfury gravatar imageSplintersfury ( 2017-09-20 19:42:03 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-09-17 04:32:46 -0500

Seen: 1,676 times

Last updated: Sep 17 '17