#include
#include
#include
#include
Using the namespace CV;
using namespace std;
CV: : Mat org, DST, img, TMP;
Void on_mouse (int event, int x, int y, int flags, void * ustc)//the event code mouse events, x, y the mouse coordinates, flags drag and keyboard symbol
{
The static Point pre_pt=CV: : Point (1, 1);//the initial coordinate
The static Point cur_pt=CV: : Point (1, 1);//real-time coordinate
Char \ [16].
If (event==CV_EVENT_LBUTTONDOWN)//left key press, read the initial coordinate, and on the image point draw round
{
Org. CopyTo (img);//copies original image to the img
Sprintf (temp, "(% d, % d)", x, y);
Pre_pt=Point (x, y);
PutText (img, temp, pre_pt FONT_HERSHEY_SIMPLEX, 0.5, Scalar,0,0,255 (0), 1, 8).//displayed on the window coordinates
Circle (img, pre_pt, 2, Scalar (255,0,0,0), CV_FILLED, CV_AA, 0).//draw round
Imshow (" img ", img);
}
Else if (event==CV_EVENT_MOUSEMOVE & amp; & ! (flags & amp; CV_EVENT_FLAG_LBUTTON))//left without press the mouse handlers
{
Img. CopyTo (TMP); TMP//copies img to temporary image, used to display real-time coordinate
Sprintf (temp, "(% d, % d)", x, y);
Cur_pt=Point (x, y);
PutText (TMP, temp, cur_pt FONT_HERSHEY_SIMPLEX, 0.5, Scalar,0,0,255 (0));//only real-time display
the coordinates of the mouseImshow (" img ", TMP);
}
Else if (event==CV_EVENT_MOUSEMOVE & amp; & (flags & amp; CV_EVENT_FLAG_LBUTTON))//left key presses, mouse movements, in the image on the draw rectangle
{
Img. CopyTo (TMP);
Sprintf (temp, "(% d, % d)", x, y);
Cur_pt=Point (x, y);
PutText (TMP, temp, cur_pt FONT_HERSHEY_SIMPLEX, 0.5, Scalar,0,0,255 (0));
A rectangle (TMP, pre_pt cur_pt, Scalar (0, 0255), 1,8,0);//on the temporary image real-time display forms when the mouse drag rectangle
Imshow (" img ", TMP);
}
Else if (event==CV_EVENT_LBUTTONUP)//left to loosen, on the image to draw rectangles of
{
Org. CopyTo (img);
Sprintf (temp, "(% d, % d)", x, y);
Cur_pt=Point (x, y);
PutText (img, temp, cur_pt FONT_HERSHEY_SIMPLEX, 0.5, Scalar,0,0,255 (0));
Circle (img, pre_pt, 2, Scalar (255,0,0,0), CV_FILLED, CV_AA, 0).
A rectangle (img, pre_pt cur_pt, Scalar (0, 0255), 1,8,0);//according to initial point and end point, the rectangle on the img on
Imshow (" img ", img);
Img. CopyTo (TMP);
Surrounded by rectangular image//and save to the DST
Int width=abs (pre_pt. X-ray cur_pt. X);
Int height=abs (pre_pt. Y - cur_pt. Y);
If (width==0 | | height==0)
{
Printf (" width==0 | | height==0 ");
return;
}
DST=org (the Rect (min (cur_pt. X, pre_pt. X), min (cur_pt. J y, pre_pt. Y), width, height));
CV: : resize (DST, DST, Size (28, 28));
CvtColor (DST, DST, CV_BGR2GRAY);
Threshold (DST, DST, 170, 255, CV_THRESH_BINARY);
Imwrite ("/media/sda/Ubuntu 14.0/myMNIST MNIST_recognize/temp3. PNG ", DST);//note will be changed to your processing results store address
NamedWindow (" DST ");
Imshow (" DST ", DST);
waitKey(0);
}
}
Int main ()
{
Org=imread ("/media/sda/Ubuntu 14.0/myMNIST MNIST_recognize/num3. JPG ");//read picture address
Org. CopyTo (img);
Org. CopyTo (TMP);
NamedWindow (" img ");//define a img window
SetMouseCallback (" img ", on_mouse, 0);//callback function called
Imshow (" img ", img);
CV: : waitKey (0);
}
CodePudding user response:
At the beginning of the code to join the header file # include & lt; Opencv2 highgui/highgui_c. H>