(二)添加鼠标截图操作


#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <stdio.h>

#define filePath "C:/Users/Administrator/Desktop/三维重建/图片/%d.PNG"

using namespace cv;
using namespace std;

Mat src, srcCopy, src_hsv, roiImg, imgThresh, edge,drawing;
Rect select;
bool select_flag = false;
string pic_name;

/*******************************【色域】********************************/
int h_min = 0;
int h_max = 10;

int s_min = 43;
int s_max = 255;

int v_min = 0;
int v_max = 255;
/***********************************************************************/

/*******************************【识别矩形大小阈值】********************************/
int Thresh = 50;
int MaxThresh = 255;
/***********************************************************************/

vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
vector<Rect> boundRect(contours.size());

void on_threshold(int, void*);

//画矩形框并截图(按S键截图并保存,按Q键退出)
void S_on_Mouse(int event, int x, int y, int flags, void*param)
{
	Point p1, p2;
	switch (event)
	{
	case EVENT_LBUTTONDOWN:
	{
							  select.x = x;
							  select.y = y;
							  select_flag = true;
	}
		break;
	case EVENT_MOUSEMOVE:
	{
							if (select_flag)
							{
								src.copyTo(srcCopy);
								p1 = Point(select.x, select.y);
								p2 = Point(x, y);
								rectangle(srcCopy, p1, p2, Scalar(0, 255, 0), 2);
								imshow("img", srcCopy);
							}
	}
		break;
	case EVENT_LBUTTONUP:
	{
							//显示框处的ROI
							Rect roi = Rect(Point(select.x, select.y), Point(x, y));
							if (roi.width && roi.height)
							{
								roiImg = src(roi);
								imshow("roi", roiImg);
								imwrite("D://1.jpg", roiImg);
							}
							select_flag = false;
	}
		break;
	}
}

void on_threshold(int, void*)
{
	vector<Mat> hsvSplit;
	split(roiImg, hsvSplit);
	equalizeHist(hsvSplit[2], hsvSplit[2]);
	merge(hsvSplit, roiImg);
	inRange(roiImg, Scalar(h_min, s_min, v_min), Scalar(h_max, s_max, v_max), imgThresh);
	//imshow("thresh", imgThresh);

	Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
	morphologyEx(imgThresh, imgThresh, MORPH_OPEN, element);
	morphologyEx(imgThresh, imgThresh, MORPH_CLOSE, element);
	Canny(imgThresh, edge, 3, 9, 3);

	//使用Threshold检测边缘
	//threshold(edge,edge,50,MaxThresh,THRESH_BINARY);

	findContours(edge, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

	vector<Rect> boundRect(contours.size());

	drawing = Mat::zeros(edge.size(), CV_8UC3);
	for (int i = 0; i < contours.size(); i++)
	{
		boundRect[i] = boundingRect(Mat(contours[i]));
		Scalar color(0, 255, 0);

		drawContours(drawing, contours, i, color, 1, 8, hierarchy);
		rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 0, 255), 2, 8);

		//写入文件
		ofstream file("坐标.txt", ios::app);
		cout << "*********************************************************" << endl;
		cout << pic_name << ":" << endl;
		//cout << "P1 = " << boundRect[i].tl() << "px" << endl;
		//cout << "P2 = " << boundRect[i].br() << "px" << endl;
		cout << "P1 = " << boundRect[i].tl()*0.265 << "mm" << endl;
		cout << "P2 = " << boundRect[i].br()*0.265 << "mm" << endl;

		file << pic_name << ":" << endl;
		//file << "P1 = " << boundRect[i].tl() << "px" << endl;
		//file << "P2 = " << boundRect[i].br() << "px" << endl;
		file << "P1 = " << boundRect[i].tl()*0.265 << "mm" << endl;
		file << "P2 = " << boundRect[i].br()*0.265 << "mm" << endl;
		cout << "*********************************************************" << endl;

		file.close();
		cout << "坐标已写入.txt文档" << endl;

		//储存识别之后的图像在当前目录下
		//imwrite(to_string(i+1)+".png", drawing);
	}
	imshow("output", drawing);
}

int main(int argc, char** argv)
{
	vector<Mat> images;
	cout << "\n按“s”键截图(英文输入法)\n\n按“q”键进行图像识别" << endl;
	for (int i = 1; i < 11; i++)
	{
		string name = format(filePath, i);
		src = imread(name);
		if (!src.data)
		{
			cout << "Finshed !\n";
			return -1;
		}
		images.push_back(src);

		/***************************************【图片预处理】****************************************/
		srcCopy = src.clone();
		select.x = select.y = 0;

		//namedWindow("input", WINDOW_AUTOSIZE);	
		//namedWindow(win, WINDOW_AUTOSIZE);
		//cvtColor(src, src_hsv, COLOR_BGR2HSV);
		imshow("img", srcCopy);

		/***************************************【截图】****************************************/
		
		while (1)
		{
			int key = waitKey(10);
			switch (key)
			{
			case's':
				setMouseCallback("img", S_on_Mouse, 0);
				break;
			}
			if (key == 27 || key == 'q')
				break;
		}
		/***************************************【颜色识别】****************************************/
		//createTrackbar("min_H", win, &h_min, 180, on_threshold);
		//createTrackbar("max_H", win, &h_max, 180, on_threshold);
		//createTrackbar("min_S", win, &s_min, s_max, on_threshold);
		//createTrackbar("max_S", win, &s_max, s_max, on_threshold);
		//createTrackbar("min_V", win, &v_min, v_max, on_threshold);
		//createTrackbar("max_V", win, &v_max, v_max, on_threshold);
		
		cvtColor(roiImg, roiImg, COLOR_BGR2HSV);
		on_threshold(0, 0);


		waitKey(0);
		
	}
	return 0;
}

在这里插入图片描述
1.切换为英文输入法,按"s"在这里插入图片描述
2. roi截图在这里插入图片描述
3.按“q”键
在这里插入图片描述


非科班出身的我代码写的很凌乱,过一段时间不看我自己都不认识;
因为总感觉电脑会在某一天突然退休(被迫害妄想症?),所以先在网上存个档;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值