數字圖像處理3:鼠標控件

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;

Mat image;
Point corner1, corner2;
Rect box;
bool ldown = false, lup = false;
void mouse_CallBack(int event, int x, int y, int flags, void *)
{
	if (event == EVENT_LBUTTONDOWN)
	{
		ldown = true;
		corner1.x = x;
		corner1.y = y;
		cout << "corner 1 recorded at" << corner1 << endl;
	}
	if (event == EVENT_LBUTTONUP)
	{
		lup = true;
		corner2.x = x;
		corner2.y = y;
		cout << "corner 2 recorded at" << corner2 << endl;
	}
	if (ldown == true && lup == false)
	{
		Point pt;
		pt.x = x;
		pt.y = y;
		Mat local_img = image.clone();
		rectangle(local_img, corner1, pt, Scalar(225, 0, 255));  //Scalar函數什麼意思呢
		imshow("原圖像", local_img);
	}
	if (ldown == true && lup == true)
	{
		box.width = abs(corner1.x - corner2.x);
		box.height = abs(corner1.y - corner2.y);
		box.x = min(corner1.x, corner2.x);
		box.y = min(corner1.y, corner2.y);
		Mat crop(image, box);
		namedWindow("截取圖像");
		imshow("截取圖像", crop);

		ldown = false;
		lup = false;
	}
}
int main()
{
	image = imread("f:\\圖片\\小包總.jpg");
	namedWindow("原圖像");
	setMouseCallback("原圖像", mouse_CallBack);
	imshow("原圖像",image);
	waitKey(0);
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章