Opencv实践项目之实时物体的测量——c++

在这里插入图片描述
全局变量

Mat matrix;
Point2f center;
Mat frame;
float scale = 0.25;
bool flag = false;

主函数

int main()
{
	frame = imread("celiang.jpg");
	resize(frame, frame, Size(0,0), scale, scale, INTER_LINEAR);
	imshow("original", frame);
	int thres[] = { 50,255 };
	Mat out;
	Point2f a4;
	getcontours(frame, thres,out,a4);
	//A4纸的长度为210,宽度为297
	double a4_width = 210;
	double a4_height = 297;
	int scale = 3;
	Point2f mart;
	int th[] = { 50,255 };
	getcontours_2(out, th,mart);

	if (flag)
	{
		int wid = round(mart.x / a4.x * a4_width);
		int hei = round(mart.y / a4.y * a4_height);
		string w = to_string(wid)+"cm";
		
		putText(out, w, Point(center.x, center.y - 50), FONT_HERSHEY_SCRIPT_COMPLEX, 1, Scalar(0, 0, 255), 1);
		string h = to_string(hei)+"cm";
		putText(out, h, Point(center.x - 70, center.y + hei ), FONT_HERSHEY_SCRIPT_COMPLEX, 1, Scalar(0, 0, 255), 1);
	}
	imshow("test", out);
	waitKey(0);
	return 0;
}

参照物识别

void getcontours(Mat img, const int arr[],Mat& out,Point2f& ob,int minarea=1000*scale)
{
	
	Mat imggray;
	cvtColor(img,imggray, COLOR_BGR2GRAY);
	Mat imggauss;
	GaussianBlur(imggray,imggauss, Size(5, 5), 1);
	Mat imgcanny;
	Canny(imggauss, imgcanny, arr[0], arr[1]);
	Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
	morphologyEx(imgcanny, imgcanny, MORPH_CLOSE, kernel);//闭操作,排除小型黑洞,使物体聚合
	//轮廓查找
	vector<vector<Point>>contours;
	vector<Vec4i>hierarchy;

	findContours(imgcanny, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
	//drawContours(img, contours, -1, Scalar(0, 0, 255), 1, 8,hierarchy);//绘制全部轮廓
	//轮廓筛选
	vector<vector<Point>>finalcontours;
	vector<vector<Point>>contours_poly(contours.size());
	for (int i = 0; i < contours.size(); i++)
	{
		if (contourArea(contours[i]) > minarea)
		{
			//approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
			finalcontours.push_back(contours[i]);
		}
	}
	//drawContours(img, finalcontours, -1, Scalar(0, 0, 255), 1, 8);
	//找到最大轮廓
	double len = INT_MIN;
	int index;
	for (int i = 0; i < finalcontours.size(); i++){
	double l = arcLength(finalcontours[i], true);
		if ( l> len){len = l;}
		index = i;}
	if (finalcontours.size() != 0)
	{
		vector<Point2f>maxcontours(finalcontours[index].size());
		approxPolyDP(Mat(finalcontours[index]), maxcontours, 0.02*arcLength(finalcontours[index], true), true);//可通过修改近似曲线的阈值来
	  //最小外接矩形
		RotatedRect bbox = minAreaRect(maxcontours);//得到长和宽
		ob.x = bbox.size.width;
		ob.y = bbox.size.height;
		//为四个点排序
		map<double, int> reorder;
		reorder.insert(pair<double, int>(maxcontours[0].x + maxcontours[0].y, 0));
		reorder.insert(pair<double, int>(maxcontours[1].x + maxcontours[1].y, 1));
		reorder.insert(pair<double, int>(maxcontours[2].x + maxcontours[2].y, 2));
		reorder.insert(pair<double, int>(maxcontours[3].x + maxcontours[3].y, 3));
		int i = 0;
		Point2f srcimg[4];
		for (map<double, int>::iterator iter = reorder.begin(); iter != reorder.end(); iter++)
		{
			srcimg[i] = maxcontours[iter->second];
			i++;
		}
		const Point2f dstimg[4] = { Point2f(0, 0),Point2f(bbox.size.width, 0) , Point2f(0, bbox.size.height),Point2f(bbox.size.width, bbox.size.height) };
		matrix = getPerspectiveTransform(srcimg, dstimg);
		inv_matrix = matrix.inv();
		Mat imgwarp;
		warpPerspective(img, imgwarp, matrix, Size(bbox.size.width, bbox.size.height));
		out = imgwarp.clone();
		
	}
}

目标识别

void getcontours_2(Mat& img, const int arr[], Point2f& mart,int minarea = 1000*scale)
{
	flag = false;
	Mat imggray;
	cvtColor(img, imggray, COLOR_BGR2GRAY);
	Mat imggauss;
	GaussianBlur(imggray, imggauss, Size(5, 5), 1);
	Mat imgcanny;
	Canny(imggauss, imgcanny, arr[0], arr[1]);
	//膨胀和腐蚀,闭操作,排除小型黑洞,使物体聚合
	//开操作,消除小连接或者毛刺,在纤细处分离物体
	Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
	morphologyEx(imgcanny, imgcanny, MORPH_CLOSE, kernel);
	//轮廓查找
	vector<vector<Point>>contours;
	vector<Vec4i>hierarchy;

	findContours(imgcanny, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
	//drawContours(img, contours, -1, Scalar(0, 0, 255), 1, 8,hierarchy);//绘制全部轮廓
	//轮廓筛选
	vector<vector<Point>>finalcontours;
	vector<vector<Point>>contours_poly(contours.size());
	for (int i = 0; i < contours.size(); i++)
	{
		if (contourArea(contours[i]) > minarea&&contours[i].size()>500*scale)
		{
			//approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
			finalcontours.push_back(contours[i]);
		}
	}
	//drawContours(img, finalcontours, -1, Scalar(0, 0, 255), 1, 8);
	//找到最大轮廓
	double len = INT_MIN;
	int index;
	for (int i = 0; i < finalcontours.size(); i++) {
		double l = arcLength(finalcontours[i], true);
		if (l> len) { len = l; }
		index = i;
	}
	if (finalcontours.size() != 0)
	{
		flag = true;
		vector<Point2f>maxcontours(finalcontours[index].size());
		approxPolyDP(Mat(finalcontours[index]), maxcontours, 0.02*arcLength(finalcontours[index], true), true);//可通过修改近似曲线的阈值来																							   //最小外接矩形
		RotatedRect bbox = minAreaRect(maxcontours);//得到长和宽
		mart.x = bbox.size.width;
		mart.y = bbox.size.height;

		//为四个点排序
		map<double, int> reorder;
		reorder.insert(pair<double, int>(maxcontours[0].x + maxcontours[0].y, 0));
		reorder.insert(pair<double, int>(maxcontours[1].x + maxcontours[1].y, 1));
		reorder.insert(pair<double, int>(maxcontours[2].x + maxcontours[2].y, 2));
		reorder.insert(pair<double, int>(maxcontours[3].x + maxcontours[3].y, 3));
		Point2f srcimg[4];
		int i = 0;
		for (map<double, int>::iterator iter = reorder.begin(); iter != reorder.end(); iter++)
		{
			srcimg[i] = maxcontours[iter->second];
			i++;
		}
		
		Mat dest = (Mat_<double>(3, 3) << srcimg[0].x, srcimg[0].y, 1,
		srcimg[1].x, srcimg[1].y, 1,
		srcimg[2].x, srcimg[2].y, 1);
		Mat original = dest*inv_matrix;
		center = srcimg[0];

		arrowedLine(img, srcimg[0], srcimg[1], Scalar(0, 0, 255), 3, 8, 0);
		arrowedLine(img, srcimg[0], srcimg[2], Scalar(0, 0, 255), 3, 8, 0);
	}
	
}

效果图
在这里插入图片描述

  • 6
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
【资源说明】 课程设计-基于opencv传统图像处理算法实现物体尺寸测量系统c++源码(含详细注释+sln解决方案).zip 课程设计-基于opencv传统图像处理算法实现物体尺寸测量系统c++源码(含详细注释+sln解决方案).zip课程设计-基于opencv传统图像处理算法实现物体尺寸测量系统c++源码(含详细注释+sln解决方案).zip课程设计-基于opencv传统图像处理算法实现物体尺寸测量系统c++源码(含详细注释+sln解决方案).zip 课程设计-基于opencv传统图像处理算法实现物体尺寸测量系统c++源码(含详细注释+sln解决方案).zip 课程设计-基于opencv传统图像处理算法实现物体尺寸测量系统c++源码(含详细注释+sln解决方案).zip 课程设计-基于opencv传统图像处理算法实现物体尺寸测量系统c++源码(含详细注释+sln解决方案).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值