1 | initial version |
selectROIS blocks video so May be you could try this
struct ParamRect {
int rctEnCours;
Rect r;
Mat img;
Mat result;
int evt = 0;
String nomFenetre;
};
void DefRectangle(int event, int x, int y, int flags, void *userData)
{
ParamRect *pgc = (ParamRect*)userData;
if (pgc->img.empty())
return;
pgc->evt = event;
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;
}
Mat img = pgc->img.clone();
rectangle(img, pgc->r, Scalar(0, 255, 0), 2);
imshow(pgc->nomFenetre, img);
}
int main(int argc, char** argv)
{
ParamRect pgc;
pgc.rctEnCours = 0;
pgc.nomFenetre = "Video";
namedWindow(pgc.nomFenetre);
setMouseCallback(pgc.nomFenetre, DefRectangle, &pgc);
VideoCapture vv(CAP_MSMF);
if (!vv.isOpened())
{
exit(0);
}
int code = 0;
Mat img;
namedWindow(pgc.nomFenetre);
std::vector< Rect > r;
while (code != 27)
{
vv >> pgc.img;
code = waitKey(10);
if (pgc.evt == 0)
{
if (pgc.r.area() > 0)
{
Mat img = pgc.img.clone();
rectangle(img, pgc.r, Scalar(0, 255, 0), 2);
imshow(pgc.nomFenetre, img);
}
else
imshow(pgc.nomFenetre, pgc.img);
}
else
pgc.evt = 0;
}
exit(0);
2 | No.2 Revision |
selectROIS blocks video so May be you could try this
struct ParamRect {
int rctEnCours;
Rect r;
Mat img;
Mat result;
int evt = 0;
String nomFenetre;
};
void DefRectangle(int event, int x, int y, int flags, void *userData)
{
ParamRect *pgc = (ParamRect*)userData;
if (pgc->img.empty())
return;
pgc->evt = event;
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;
}
Mat img = pgc->img.clone();
rectangle(img, pgc->r, Scalar(0, 255, 0), 2);
imshow(pgc->nomFenetre, img);
}
int main(int argc, char** argv)
{
ParamRect pgc;
pgc.rctEnCours = 0;
pgc.nomFenetre = "Video";
namedWindow(pgc.nomFenetre);
setMouseCallback(pgc.nomFenetre, DefRectangle, &pgc);
VideoCapture vv(CAP_MSMF);
if (!vv.isOpened())
{
exit(0);
}
int code = 0;
Mat img;
namedWindow(pgc.nomFenetre);
std::vector< Rect > r;
while (code != 27)
{
vv >> pgc.img;
code = waitKey(10);
if (pgc.evt == 0)
{
if (pgc.r.area() > 0)
{
Mat img = pgc.img.clone();
rectangle(img, pgc.r, Scalar(0, 255, 0), 2);
imshow(pgc.nomFenetre, img);
}
else
imshow(pgc.nomFenetre, pgc.img);
}
else
pgc.evt = 0;
}
exit(0);
It is for one rect. it is not difficult to use keyboart to get multiple rect
3 | No.3 Revision |
selectROIS blocks video so May be you could try this
struct ParamRect {
int rctEnCours;
Rect vector<Rect> r;
int index = 0;
Mat img;
Mat result;
int evt = 0;
String nomFenetre;
};
void DefRectangle(int event, int x, int y, int flags, void *userData)
{
ParamRect *pgc = (ParamRect*)userData;
if (pgc->img.empty())
return;
pgc->evt = event;
if ((flags&EVENT_FLAG_LBUTTON) != 0 && (flags & EVENT_FLAG_CTRLKEY) == 0)
{
if (pgc->rctEnCours == 0)
{
pgc->r.x pgc->r[pgc->index].x = x;
pgc->r.y pgc->r[pgc->index].y = y;
pgc->r.width pgc->r[pgc->index].width = 0;
pgc->r.height pgc->r[pgc->index].height = 0;
pgc->rctEnCours = 1;
}
else if (pgc->rctEnCours == 1)
{
Point tl = pgc->r.tl(), pgc->r[pgc->index].tl(), br = pgc->r.br();
pgc->r[pgc->index].br();
if (x != pgc->r.x)
pgc->r[pgc->index].x)
{
if (x < pgc->r.x)
{
pgc->r.x pgc->r[pgc->index].x)
{
pgc->r[pgc->index].x = x;
pgc->r.width pgc->r[pgc->index].width = br.x - x - 1;
}
else
pgc->r.width pgc->r[pgc->index].width = x - tl.x - 1;
}
if (y != pgc->r.y)
pgc->r[pgc->index].y)
{
if (y < pgc->r.y)
{
pgc->r.y pgc->r[pgc->index].y)
{
pgc->r[pgc->index].y = y;
pgc->r.height pgc->r[pgc->index].height = br.y - y - 1;
}
else
pgc->r.height pgc->r[pgc->index].height = y - tl.y - 1;
}
if (pgc->r.br().x (pgc->r[pgc->index].br().x > pgc->img.size().width)
{
pgc->r.width pgc->r[pgc->index].width = pgc->img.size().width - pgc->r.x;
}
if (pgc->r.br().y pgc->r[pgc->index].x;
}
if (pgc->r[pgc->index].br().y > pgc->img.size().height)
{
pgc->r.height pgc->r[pgc->index].height = pgc->img.size().height - pgc->r.y;
pgc->r[pgc->index].y;
}
}
}
else
if (event == EVENT_LBUTTONUP && pgc->rctEnCours == 1)
{
pgc->rctEnCours = 0;
}
Mat img = pgc->img.clone();
rectangle(img, pgc->r, Scalar(0, 255, pgc->r[pgc->index], Scalar(pgc->index * 64, 255 - pgc->index * 64, 0), 2);
imshow(pgc->nomFenetre, img);
}
int main(int argc, char** argv)
{
ParamRect pgc;
pgc.rctEnCours = 0;
pgc.nomFenetre = "Video";
pgc.r = vector<Rect>(3);
namedWindow(pgc.nomFenetre);
setMouseCallback(pgc.nomFenetre, DefRectangle, &pgc);
VideoCapture vv(CAP_MSMF);
if (!vv.isOpened())
{
exit(0);
}
int code = 0;
Mat img;
namedWindow(pgc.nomFenetre);
std::vector< Rect > r;
while (code != 27)
{
vv >> pgc.img;
code = waitKey(10);
if (code >= 48 && code <= 51)
pgc.index = code - 48;
if (pgc.evt == 0)
{
if (pgc.r.area() Mat img = pgc.img.clone();
for (int i = 0; i < pgc.r.size();i++)
{
if (pgc.r[i].area() > 0)
{
Mat img = pgc.img.clone();
{
rectangle(img, pgc.r, Scalar(0, 255, pgc.r[i], Scalar(i*64, 255-i*64, 0), 2);
}
}
imshow(pgc.nomFenetre, img);
}
else
imshow(pgc.nomFenetre, pgc.img);
}
else
pgc.evt = 0;
}
exit(0);
It is for one rect. it is not difficult to use keyboart to get multiple rect