Ask Your Question

Revision history [back]

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;
    int nbIteration;
    String nomFenetre;
};



void DefRectangle(int event, int x, int y, int flags, void *userData)
{
    ParamRect *pgc = (ParamRect*)userData;

    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 - 0.5* pgc->result);
    waitKey(10);
}

const String keys =
"{Aide h usage ? help  |     | Afficher ce message   }"
"{@arg1                |     | chemin complet de l'image couleur (3 canaux)}";

int main(int argc, char* argv[])
{
    CommandLineParser parser(argc, argv, keys);

    if (parser.has("help"))
    {
        parser.printMessage();
        return 0;
    }
    String nomFic = parser.get<String>(0);
    if (nomFic.length() == 0)
    {
        parser.printMessage();
        return 0;
    }
    ParamRect pgc;
    pgc.rctEnCours = 0;
    pgc.img = imread(nomFic, IMREAD_COLOR);
    if (pgc.img.empty())
    {
        cout << "File empty. Check name file.\n";
        return 0;
    }
    pgc.nomFenetre = "MyImage";

    imshow(pgc.nomFenetre, pgc.img);

    setMouseCallback(pgc.nomFenetre, DefRectangle, &pgc);
    int code = 0;
    do
    {
        code = waitKey(30);
    } while (code != 27);
}

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

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;
    int nbIteration;
    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 - 0.5* pgc->result);
);
    waitKey(10);
}

const String keys =
"{Aide h usage ? help  |     | Afficher ce message   }"
"{@arg1                |     | chemin complet de l'image couleur (3 canaux)}";

#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(int argc, char* argv[])
{
    CommandLineParser parser(argc, argv, keys);

    if (parser.has("help"))
    {
        parser.printMessage();
main()
{
    // Open the video file
    VideoCapture capture("g:/lib/opencv/samples/data/Megamind.avi");

    if (!capture.isOpened())
        return 0;
    }
    String nomFic = parser.get<String>(0);
    if (nomFic.length() == 0)
    {
        parser.printMessage();
        return 0;
    }
1;

    bool stop(false);
    ParamRect pgc;
    pgc.rctEnCours = 0;
    pgc.img = imread(nomFic, IMREAD_COLOR);
    if (pgc.img.empty())
    {
        cout << "File empty. Check name file.\n";
        return 0;
    }
Mat frame; //current video frame
    pgc.img=frame;
    pgc.nomFenetre = "MyImage";

    imshow(pgc.nomFenetre, pgc.img);
"Extracted Frame";
    namedWindow(pgc.nomFenetre);
    int delay = 10;


    setMouseCallback(pgc.nomFenetre, DefRectangle, &pgc);
    int code = 0;
    do
    {
        code = waitKey(30);
    } 
    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 (code != 27);
(!stop)
    {
        capture>>pgc.img;
        if (waitKey(delay) >= 0 || pgc.img.empty())
            stop = true;

    }

    waitKey();
}

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