Ask Your Question
0

Change the location of a fixed rectangle in the image by moving the mouse

asked 2017-08-01 08:57:25 -0500

louis89 gravatar image

updated 2017-08-01 11:47:48 -0500

I am using OpenCV for a C++ application.I have a fixed rectangle in my image.I want when I click on the mouse on the rectangle,than The rectangle position changes to where the mouse is released.I'm beginner in opencv and c++.do you have any idea to do this?this is my code:

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

using namespace cv;
using namespace std;

int main()
{
// Open the video file
VideoCapture capture("blowcontrast1.avi");

if (!capture.isOpened())
    return 1;

bool stop(false);
Mat frame; //current video frame
namedWindow("Extracted Frame");
int delay = 1;

cout << capture.get(CV_CAP_PROP_FPS);
capture.set(CV_CAP_PROP_POS_AVI_RATIO, 0.5);

while (!stop)
{
    if (!capture.read(frame))
        break;
            Rect rectangle(200 , 150, 40, 40);
            rectangle(frame, rectangle, Scalar(255, 255, 255));
    imshow("Extracted Frame", frame);

    if (waitKey(delay) >= 0)
        stop = true;
}

waitKey();
}
edit retag flag offensive close merge delete

Comments

take a look at this

sturkmen gravatar imagesturkmen ( 2017-08-01 10:36:01 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-08-01 10:12:48 -0500

LBerger gravatar image

updated 2017-08-02 09:27:08 -0500

You have to use a mouse callback function. There is an example in grabcut sample.

You can try this :

#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;


struct ParamRect {
    int rctEnCours;
    Rect r;
    Mat img;
    Mat result;
    String nomFenetre;
};



void DefRectangle(int event, int x, int y, int flags, void *userData)
{
    ParamRect *pgc = (ParamRect*)userData;
    if (pgc->img.empty())
        return;
/* UNUSED 
    if ((flags&EVENT_FLAG_LBUTTON)!=0 && (flags & EVENT_FLAG_CTRLKEY)==0)
    {
        if (pgc->rctEnCours == 0)
        {
            pgc->r.x = x;
            pgc->r.y = y;
            pgc->r.width = 0;
            pgc->r.height = 0;
            pgc->rctEnCours = 1;
        }
        else if (pgc->rctEnCours == 1)
        {
            Point tl = pgc->r.tl(), br = pgc->r.br();
            if (x != pgc->r.x)
            {
                if (x < pgc->r.x)
                {
                    pgc->r.x = x;
                    pgc->r.width = br.x - x - 1;
                }
                else
                    pgc->r.width = x - tl.x - 1;

            }
            if (y != pgc->r.y)
            {
                if (y < pgc->r.y)
                {
                    pgc->r.y = y;
                    pgc->r.height = br.y - y - 1;
                }
                else
                    pgc->r.height = y - tl.y - 1;

            }
            if (pgc->r.br().x > pgc->img.size().width)
            {
                pgc->r.width = pgc->img.size().width - pgc->r.x;
            }
            if (pgc->r.br().y > pgc->img.size().height)
            {
                pgc->r.height = pgc->img.size().height - pgc->r.y;
            }
        }
    }
    else */
    if (event == EVENT_LBUTTONUP && pgc->rctEnCours == 1)
    {
        pgc->rctEnCours = 0;
    }
    if ((flags&EVENT_FLAG_LBUTTON) != 0 && (flags & EVENT_FLAG_CTRLKEY) != 0)
    {
        pgc->r.x = x;
        pgc->r.y = y;
    }
    Mat img = pgc->img.clone();
    rectangle(img, pgc->r, Scalar(0, 255, 0), 2);
    imshow(pgc->nomFenetre, img );
    waitKey(10);
}


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

using namespace cv;
using namespace std;

int main()
{
    // Open the video file
    VideoCapture capture("g:/lib/opencv/samples/data/Megamind.avi");

    if (!capture.isOpened())
        return 1;

    bool stop(false);
    ParamRect pgc;
    pgc.rctEnCours = 0;
    Mat frame; //current video frame
    pgc.img=frame;
    pgc.nomFenetre = "Extracted Frame";
    namedWindow(pgc.nomFenetre);
    int delay = 10;


    setMouseCallback(pgc.nomFenetre, DefRectangle, &pgc);

    cout << capture.get(CV_CAP_PROP_FPS);
//    capture.set(CV_CAP_PROP_POS_AVI_RATIO, 0.5); not a good idea
    pgc.r= Rect(200, 150, 40, 40);

    while (!stop)
    {
        capture>>pgc.img;
        if (waitKey(delay) >= 0 || pgc.img.empty())
            stop = true;

    }

    waitKey();
}

use ctrl key and left button to move rectangle . I didn't check if rect is outside of image

edit flag offensive delete link more

Comments

thanks a lot .this Works very well in opencv2.4.13,but when I use this code in opencv3.0.0 then The computer hugs and does not do anything.Why is this happening?

louis89 gravatar imagelouis89 ( 2017-08-02 10:08:27 -0500 )edit

Don't use 3.0 in 3.3-rc it works. Do you use windows or linux ?

LBerger gravatar imageLBerger ( 2017-08-02 10:14:11 -0500 )edit

I use windows and VS2015.In my code I use connected component.for this reason I use opencv3.0.0

louis89 gravatar imagelouis89 ( 2017-08-02 10:19:53 -0500 )edit

update to 3.3-rc and can you see video frame? (-> check opencv_ffmpeg.dll)

LBerger gravatar imageLBerger ( 2017-08-02 10:21:49 -0500 )edit

ok.I try with opencv3.3 and I'll tell you the result

louis89 gravatar imagelouis89 ( 2017-08-02 10:24:53 -0500 )edit

I use opencv3.3_rc and Unfortunately the computer hangs and dose not anything again and I had to restart the computer.I'm really confused and really do not know what to do to solve this problem

louis89 gravatar imagelouis89 ( 2017-08-02 13:38:43 -0500 )edit

restart computer !! No, problem is not my program. Have you build yourself opencv on this computer ?

LBerger gravatar imageLBerger ( 2017-08-02 13:52:17 -0500 )edit

yes.your program works very well in opencv2.4.13 but when I use this program in opencv3.0.0 or opencv3.3_rc I'm having trouble hanging up the computer

louis89 gravatar imagelouis89 ( 2017-08-02 14:08:55 -0500 )edit

Have you build yourself opencv on this computer ? what is error message ?

LBerger gravatar imageLBerger ( 2017-08-02 15:00:14 -0500 )edit

yes.I do this.I use cmake for build opencv3.3_rc and Without receiving any errors I succeeded to build it in visual studio 2015.I use this version of opencv for another program(for example connected component) but when I use your code in this version of opencv,my program stops without getting any errors

louis89 gravatar imagelouis89 ( 2017-08-02 16:01:05 -0500 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2017-08-01 08:57:25 -0500

Seen: 681 times

Last updated: Aug 02 '17