วันเสาร์ที่ 7 ตุลาคม พ.ศ. 2560

C++ opencv drawing Box

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

#include<iostream>
#include<conio.h>       

using namespace std;
using namespace cv;

void my_mouse_calback(int event, int x, int y, int flags, void*data);

Rect Box;
Mat drawing;

bool drawing_box = false;

void draw_box(Mat& img, Rect box)
{
rectangle(img, box.tl(), box.br(), Scalar(255, 0, 0));
}

int main(int argc,char** argv)
{
int Bright = 1, Cont = 47;
Mat drawing, drawing2;
drawing = imread("D:\\3.jpg"); drawing.copyTo(drawing2);
Box = Rect(-1, -1, 0, 0);

namedWindow("Exam", CV_WINDOW_AUTOSIZE);


setMouseCallback("Exam", my_mouse_calback, (void*)&drawing);
while(1)
{
drawing.copyTo(drawing2);
if (drawing_box)draw_box(drawing2, Box);
imshow("Exam", drawing2);
if (waitKey(30) == 27)
break;
}
return 0;
}

void my_mouse_calback(int event, int x, int y, int flags, void*data)
{
Mat& drawing = *(Mat*)data;
int w, h, r;
if (event == EVENT_MOUSEMOVE)
{
if (drawing_box)
{
Box.width = x - Box.x;
Box.height = y - Box.y;
}
}

if (event == EVENT_LBUTTONDOWN)
{
drawing_box = true;
Box = Rect(x, y, 0, 0);
}
if (event == EVENT_LBUTTONUP)
{
drawing_box = false;

if (Box.width < 0)
{
Box.x += Box.width;
Box.width *= -1;
}
if (Box.height < 0)
{
Box.x = Box.height;
Box.width *= -1;
}
draw_box(drawing, Box);
}
}


ไม่มีความคิดเห็น:

แสดงความคิดเห็น