opencv通过鼠标获取训练图像

来源:互联网 发布:我的世界网络错误 编辑:程序博客网 时间:2024/06/11 14:03

环境:Window10+VS2017+opencv3.2 

再次说明。本代码只能用来学习,交流学习经验,禁止用于商业用途,否则必定追究责任。

如果有疑问:欢迎加qq:1558500569

使用鼠标达成训练图像的获取,并且移动矩形框,放大或者缩小矩形框,并且保存为训练的图像。目前仅仅只是矩形,后期添加人圆。


代码如下:

Mat FormerImage; //最原始的图片
Mat win_image;
Mat BackupImage; //用来保存上一个图像
bool trainBool = false; //用来判断是否处在train
bool SaveBool = false; //用来保存
bool DisSpotBool = false; //用来判断圆
bool UserRepeatImage = false; //不能重复使用训练图像
Rect rect_img; //用来训练选中的矩形
Point theChange;//用来保存移动的坐标
Point theChangePaintSpot;//画出斑点


/*****************************************获取训练按钮的信息*************************************/
void fillRectButton(Mat& Image, Point firstPoint, int num, Scalar color, string Text)
{
Point points[1][20];
points[0][0] = firstPoint;
points[0][1] = Point(firstPoint.x + num, firstPoint.y);
points[0][2] = Point(firstPoint.x + num, firstPoint.y + 40);
points[0][3] = Point(firstPoint.x, firstPoint.y + 40);
const Point* pt[1] = { points[0] };
int npt[1] = { 4 };
polylines(Image, pt, npt, 1, 1, color);
fillPoly(Image, pt, npt, 1, color, 8);
cv::putText(Image, Text, Point(firstPoint.x + num / 8, firstPoint.y + 20), FONT_HERSHEY_COMPLEX, 0.5, cv::Scalar(0, 0, 0), 0.5, 1, 0);


}
/**********************鼠标操作获取训练图像*********************/
//int x,int y,代表鼠标位于窗口的(x,y)坐标位置,窗口左上角默认为原点,向右为x轴,向下为y轴
// static声明静态局部变量,值在函数调用结束后不消失而保留原值,
//即其占用的存储单元不释放,在下次该函数调用时,该变量保留上一次函数调用结束时的值


void Train_OnMouse(int event, int x, int y, int flags, void* ustc)
{
if (event == CV_EVENT_LBUTTONDOWN) //左键按下
{
theChangePaintSpot.x = x;
theChangePaintSpot.y = y;
if ((x >= 0) && (x <= 60) && (y <= 40) && (y >= 0)) //点击训练按钮(60,40)
{
if (!trainBool)
{
fillRectButton(win_image, Point(0, 0), 60, Scalar(0, 255, 255), "train"); //获取训练按钮的信息 变色已经选中到
trainBool = true;
if (!UserRepeatImage)
{
BackupImage = win_image.clone();
rectangle(win_image, Point(0, 40), Point(62, 80), Scalar(0, 0, 255), 2, 8, 0);//在临时图像上实时显示鼠标拖动时形成的矩形画一个小矩形 
rect_img = Rect(0, 40, 62, 40);
}
}
else
{
fillRectButton(win_image, Point(0, 0), 60, Scalar(0, 0, 255), "train"); //获取训练按钮的信息
trainBool = false;
UserRepeatImage = true;
}
}


if ((x >= 62) && (x <= 122) && (y <= 40) && (y >= 0))
{
if (!SaveBool)
{
fillRectButton(win_image, Point(62, 0), 60, Scalar(0, 255, 255), "Save"); //获取训练按钮的信息 变色已经选中到
Mat Image = win_image(rect_img);
imshow("模板的图片", Image);
SaveBool = true;
}
else
{
fillRectButton(win_image, Point(62, 0), 60, Scalar(0, 0, 255), "Save"); //获取训练按钮的信息
SaveBool = false;
}
}


if ((x >= 124) && (x <= 204) && (y <= 40) && (y >= 0)) //点击训练按钮(60,40)
{
if (!DisSpotBool)
{
fillRectButton(win_image, Point(124, 0), 80, Scalar(0, 255, 255), "DisSpot"); //获取训练按钮的信息 变色已经选中到
DisSpotBool = true;
}
else
{
fillRectButton(win_image, Point(124, 0), 80, Scalar(0, 0, 255), "DisSpot"); //获取训练按钮的信息
DisSpotBool = false;
}
}


}


if (event == CV_EVENT_MOUSEMOVE)  //鼠标移动
{


if (trainBool)
{
if ((x >= rect_img.x) && (x <= rect_img.x + rect_img.width) && (y >= rect_img.y) && (y <= rect_img.y + rect_img.height) & trainBool)
{
line(win_image, Point(rect_img.x + rect_img.width / 2 - 4, rect_img.y + rect_img.height / 2), Point(rect_img.x + rect_img.width / 2 + 4, rect_img.y + rect_img.height / 2), Scalar(0, 255, 255), 1, 4, 0);
line(win_image, Point(rect_img.x + rect_img.width / 2, rect_img.y + rect_img.height / 2 - 4), Point(rect_img.x + rect_img.width / 2, rect_img.y + rect_img.height / 2 + 4), Scalar(0, 255, 255), 1, 4, 0);
circle(win_image, Point(rect_img.x + rect_img.width, rect_img.y + rect_img.height / 2), 8, Scalar(0, 0, 255), 1.5, 1, 0);

}
}


}


if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_LBUTTONDOWN))   //左键按下,鼠标移动时
{
if (trainBool)
{
if ((x >= rect_img.x) && (x <= rect_img.x + rect_img.width) && (y >= rect_img.y) && (y <= rect_img.y + rect_img.height) & trainBool)
{
theChange.x = x - (rect_img.x + rect_img.width / 2);
theChange.y = y - (rect_img.y + rect_img.height / 2);
}
}
if (DisSpotBool)
{
line(win_image, theChangePaintSpot, Point(x, y), Scalar(154, 157, 252), 4, 4, 0);
theChangePaintSpot = Point(x, y);
}
}


if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_RBUTTONDOWN))   //右键按下,鼠标移动时
{
if (trainBool)
{
if ((x >= rect_img.x) && (x <= rect_img.x + rect_img.width) && (y >= rect_img.y) && (y <= rect_img.y + rect_img.height) & trainBool)
{
theChange.x = x - (rect_img.x + rect_img.width / 2);
theChange.y = y - (rect_img.y + rect_img.height / 2);
}
}


}


if (event == CV_EVENT_RBUTTONUP) //右键释放
{
if (trainBool)
{
if ((x >= rect_img.x - rect_img.width / 2) && (x <= rect_img.x + rect_img.width*1.5) && (y >= rect_img.y - rect_img.height / 2) && (y <= rect_img.y + rect_img.height*1.5) & trainBool)
{
win_image = BackupImage.clone();
rect_img.width = theChange.x + rect_img.width;
rect_img.height = theChange.y + rect_img.height;
rectangle(win_image, Point(rect_img.x, rect_img.y), Point(rect_img.x + rect_img.width, rect_img.y + rect_img.height), Scalar(0, 0, 255), 2, 8, 0);//在临时图像上实时显示鼠标拖动时形成的矩形 
line(win_image, Point(rect_img.x + rect_img.width / 2 - 4, rect_img.y + rect_img.height / 2), Point(rect_img.x + rect_img.width / 2 + 4, rect_img.y + rect_img.height / 2), Scalar(0, 255, 255), 1, 4, 0);
line(win_image, Point(rect_img.x + rect_img.width / 2, rect_img.y + rect_img.height / 2 - 4), Point(rect_img.x + rect_img.width / 2, rect_img.y + rect_img.height / 2 + 4), Scalar(0, 255, 255), 1, 4, 0);
circle(win_image, Point(rect_img.x + rect_img.width, rect_img.y + rect_img.height / 2), 8, Scalar(0, 0, 255), 1.5, 1, 0);
}
}


}


if (event == CV_EVENT_LBUTTONUP) //左键释放
{
if (trainBool)
{
if ((x >= rect_img.x - rect_img.width / 2) && (x <= rect_img.x + rect_img.width*1.5) && (y >= rect_img.y - rect_img.height / 2) && (y <= rect_img.y + rect_img.height*1.5) & trainBool)
{
win_image = BackupImage.clone();
rectangle(win_image, Point(theChange.x + rect_img.x, theChange.y + rect_img.y), Point(theChange.x + rect_img.x + rect_img.width, theChange.y + rect_img.y + rect_img.height), Scalar(0, 0, 255), 2, 8, 0);//在临时图像上实时显示鼠标拖动时形成的矩形 
rect_img.x = theChange.x + rect_img.x;
rect_img.y = theChange.y + rect_img.y;
}
}


}


imshow("训练操作", win_image);
}


GetTrainRect::GetTrainRect(Mat dealWithMat)
:dealWithMat(dealWithMat)
{
Mat Image(dealWithMat.rows +40, dealWithMat.cols , dealWithMat.type()); //是一个type 不是channels
Mat imageRoI = Image(Rect(0, 40, dealWithMat.cols, dealWithMat.rows));
dealWithMat.copyTo(imageRoI);
fillRectButton(Image, Point(0, 0), 60, Scalar(0, 0, 255), "train"); //获取训练按钮的信息(获取矩形)
fillRectButton(Image, Point(62, 0), 60, Scalar(0, 0, 255), "Save"); //获取训练按钮的信息
fillRectButton(Image, Point(124, 0), 80, Scalar(0, 0, 255), "DisSpot"); //获取训练按钮的信息
fillRectButton(Image, Point(206, 0), 60, Scalar(0, 0, 255), "Run"); //获取训练按钮的信息
fillRectButton(Image, Point(dealWithMat.cols-60, 0), 60, Scalar(0, 0, 255), "Circle"); //获取圆


win_image = Image.clone();
FormerImage = Image.clone();
BackupImage = Image.clone();
namedWindow("训练操作", WINDOW_AUTOSIZE);
imshow("训练操作", Image);
setMouseCallback("训练操作", Train_OnMouse);
waitKey(0);
}


原创粉丝点击