文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

C++ OpenCV实现物体尺寸测量示例详解

2024-04-02 19:55

关注

前言

本文将使用OpenCV C++ 进行物体尺寸测量。具体来说就是先定位到待测物体的位置,然后测量物体的宽高。

一、图像透视矫正

原图如图所示。本案例的需求是测量图片中两张卡片的尺寸。首先,我们得定位到两张卡片的位置。第一步,我们首先得将白色A4纸切割出来,这样方便定位到两张卡片所在位置。这里用到的算法是图像透视矫正,具体可以参考OpenCV C++案例实战四《图像透视矫正》

//图像矫正
void getWarp(Mat src, Mat &Warp)
{
	Mat gray;
	cvtColor(src, gray, COLOR_BGR2GRAY);

	Mat thresh;
	threshold(gray, thresh, 0, 255, THRESH_BINARY | THRESH_OTSU);

	Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
	Mat open;
	morphologyEx(thresh, open, MORPH_OPEN, kernel);

	vector<vector<Point>>contours;
	findContours(open, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
	vector<vector<Point>>conPoly(contours.size());
	vector<Point>srcPts;

	//找到最大轮廓
	int MaxIndex = 0;
	double Area = 0;
	for (int i = 0; i < contours.size(); i++)
	{
		double area = contourArea(contours[i]);
		if (area > Area)
		{
			Area = area;
			MaxIndex = i;
		}
	}

	//获取矩形四个角点
	double peri = arcLength(contours[MaxIndex], true);
	approxPolyDP(contours[MaxIndex], conPoly[MaxIndex], 0.02*peri, true);

	srcPts = { conPoly[MaxIndex][0],conPoly[MaxIndex][1],conPoly[MaxIndex][2],conPoly[MaxIndex][3] };

	int T_L, B_L, B_R, T_R;
	int width = src.cols / 2;
	int height = src.rows / 2;
	for (int i = 0; i < srcPts.size(); i++)
	{
		if (srcPts[i].x < width && srcPts[i].y < height)
		{
			T_L = i;
		}
		if (srcPts[i].x < width && srcPts[i].y > height)
		{
			B_L = i;
		}
		if (srcPts[i].x > width && srcPts[i].y > height)
		{
			B_R = i;
		}
		if (srcPts[i].x > width && srcPts[i].y < height)
		{
			T_R = i;
		}
	}

	double UpWidth = EuDis(srcPts[T_L], srcPts[T_R]);
	double DownWidth = EuDis(srcPts[B_L], srcPts[B_R]);
	double MaxWidth = max(UpWidth, DownWidth);

	double UpHeight = EuDis(srcPts[T_L], srcPts[B_L]);
	double DownHeight = EuDis(srcPts[T_R], srcPts[B_R]);
	double MaxHeight = max(UpHeight, DownHeight);

	//透视变换进行图像矫正
	Point2f SrcAffinePts[4] = { Point2f(srcPts[T_L]),Point2f(srcPts[T_R]) ,Point2f(srcPts[B_R]) ,Point2f(srcPts[B_L]) };
	Point2f DstAffinePts[4] = { Point2f(0,0),Point2f(MaxWidth,0),Point2f(MaxWidth,MaxHeight),Point2f(0,MaxHeight) };

	Mat M = getPerspectiveTransform(SrcAffinePts, DstAffinePts);
	warpPerspective(src, Warp, M, Point(MaxWidth, MaxHeight));

}

效果如图所示。接下来,我们需要定位两张卡片所在位置,寻找特征。

二、物体定位

//获取物体坐标
void FindPts(Mat &Warp, vector<vector<Point>>&TargetPts)
{

	Mat gray;
	cvtColor(Warp, gray, COLOR_BGR2GRAY);

	Mat thresh;
	threshold(gray, thresh, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);

	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
	Mat open;
	morphologyEx(thresh, open, MORPH_OPEN, kernel);

	vector<vector<Point>>contours;
	findContours(open, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
	vector<vector<Point>>conPoly(contours.size());
	//定位卡片四个角点
	for (int i = 0; i < contours.size(); i++)
	{
		double area = contourArea(contours[i]);

		if (area > 1000)
		{
			double peri = arcLength(contours[i], true);

			approxPolyDP(contours[i], conPoly[i], 0.02*peri, true);

			vector<Point>temp;
			temp = { conPoly[i][0],conPoly[i][1], conPoly[i][2], conPoly[i][3] };

			TargetPts.push_back(temp);
		}
	}
}

如图所示。通过上面代码段,我们已经定位出卡片的四个角点。接下来,只需根据角点位置就可以计算卡片的宽高了。

三、尺寸测量

//计算距离
void DrawAndCompute(Mat &Warp, vector<vector<Point>>&TargetPts)
{
	for (int i = 0; i < TargetPts.size(); i++)
	{
		for (int j = 0; j < TargetPts[i].size(); j++)
		{
			//尺寸测量
			Point PtA = Point(TargetPts[i][j]);
			Point PtB = Point(TargetPts[i][(j + 1) % TargetPts[i].size()]);
			double dis = round(EuDis(PtA, PtB) * 100) / 100;

			//效果显示
			circle(Warp, TargetPts[i][j], 5, Scalar(0, 255, 0), -1);
			line(Warp, PtA, PtB, Scalar(0, 0, 255), 2);
			char text[20];
			sprintf_s(text, "%.2f", dis);
			Point point = Point((PtA.x + PtB.x) / 2, (PtA.y + PtB.y) / 2);
			putText(Warp, text, point, FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 0, 255), 2);
		}
	}
}

四、效果显示

五、源码

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;

//欧式距离
double EuDis(Point pt1, Point pt2)
{
	return sqrt((pt2.x - pt1.x)*(pt2.x - pt1.x) + (pt2.y - pt1.y)*(pt2.y - pt1.y));
}

//图像矫正
void getWarp(Mat src, Mat &Warp)
{
	Mat gray;
	cvtColor(src, gray, COLOR_BGR2GRAY);

	Mat thresh;
	threshold(gray, thresh, 0, 255, THRESH_BINARY | THRESH_OTSU);

	Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
	Mat open;
	morphologyEx(thresh, open, MORPH_OPEN, kernel);

	vector<vector<Point>>contours;
	findContours(open, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
	vector<vector<Point>>conPoly(contours.size());
	vector<Point>srcPts;

	//找到最大轮廓
	int MaxIndex = 0;
	double Area = 0;
	for (int i = 0; i < contours.size(); i++)
	{
		double area = contourArea(contours[i]);
		if (area > Area)
		{
			Area = area;
			MaxIndex = i;
		}
	}

	//获取矩形四个角点
	double peri = arcLength(contours[MaxIndex], true);
	approxPolyDP(contours[MaxIndex], conPoly[MaxIndex], 0.02*peri, true);

	srcPts = { conPoly[MaxIndex][0],conPoly[MaxIndex][1],conPoly[MaxIndex][2],conPoly[MaxIndex][3] };

	int T_L, B_L, B_R, T_R;
	int width = src.cols / 2;
	int height = src.rows / 2;
	for (int i = 0; i < srcPts.size(); i++)
	{
		if (srcPts[i].x < width && srcPts[i].y < height)
		{
			T_L = i;
		}
		if (srcPts[i].x < width && srcPts[i].y > height)
		{
			B_L = i;
		}
		if (srcPts[i].x > width && srcPts[i].y > height)
		{
			B_R = i;
		}
		if (srcPts[i].x > width && srcPts[i].y < height)
		{
			T_R = i;
		}
	}

	double UpWidth = EuDis(srcPts[T_L], srcPts[T_R]);
	double DownWidth = EuDis(srcPts[B_L], srcPts[B_R]);
	double MaxWidth = max(UpWidth, DownWidth);

	double UpHeight = EuDis(srcPts[T_L], srcPts[B_L]);
	double DownHeight = EuDis(srcPts[T_R], srcPts[B_R]);
	double MaxHeight = max(UpHeight, DownHeight);

	//透视变换进行图像矫正
	Point2f SrcAffinePts[4] = { Point2f(srcPts[T_L]),Point2f(srcPts[T_R]) ,Point2f(srcPts[B_R]) ,Point2f(srcPts[B_L]) };
	Point2f DstAffinePts[4] = { Point2f(0,0),Point2f(MaxWidth,0),Point2f(MaxWidth,MaxHeight),Point2f(0,MaxHeight) };

	Mat M = getPerspectiveTransform(SrcAffinePts, DstAffinePts);
	warpPerspective(src, Warp, M, Point(MaxWidth, MaxHeight));

}

//获取物体坐标
void FindPts(Mat &Warp, vector<vector<Point>>&TargetPts)
{

	Mat gray;
	cvtColor(Warp, gray, COLOR_BGR2GRAY);

	Mat thresh;
	threshold(gray, thresh, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);

	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
	Mat open;
	morphologyEx(thresh, open, MORPH_OPEN, kernel);

	vector<vector<Point>>contours;
	findContours(open, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
	vector<vector<Point>>conPoly(contours.size());
	//定位卡片四个角点
	for (int i = 0; i < contours.size(); i++)
	{
		double area = contourArea(contours[i]);

		if (area > 1000)
		{
			double peri = arcLength(contours[i], true);

			approxPolyDP(contours[i], conPoly[i], 0.02*peri, true);

			vector<Point>temp;
			temp = { conPoly[i][0],conPoly[i][1], conPoly[i][2], conPoly[i][3] };

			TargetPts.push_back(temp);
		}
	}
}

//计算距离
void DrawAndCompute(Mat &Warp, vector<vector<Point>>&TargetPts)
{
	for (int i = 0; i < TargetPts.size(); i++)
	{
		for (int j = 0; j < TargetPts[i].size(); j++)
		{
			//尺寸测量
			Point PtA = Point(TargetPts[i][j]);
			Point PtB = Point(TargetPts[i][(j + 1) % TargetPts[i].size()]);
			double dis = round(EuDis(PtA, PtB) * 100) / 100;

			//效果显示
			circle(Warp, TargetPts[i][j], 5, Scalar(0, 255, 0), -1);
			line(Warp, PtA, PtB, Scalar(0, 0, 255), 2);
			char text[20];
			sprintf_s(text, "%.2f", dis);
			Point point = Point((PtA.x + PtB.x) / 2, (PtA.y + PtB.y) / 2);
			putText(Warp, text, point, FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 0, 255), 2);
		}
	}
}

int main()
{
	Mat src = imread("src.jpg");
	if (src.empty())
	{
		cout << "No Image!" << endl;
		system("pause");
		return -1;
	}

	Mat Warp;
	getWarp(src, Warp);

	vector<vector<Point>>TargetPts;	
	FindPts(Warp, TargetPts);

	DrawAndCompute(Warp, TargetPts);

	imshow("Warp", Warp);
	waitKey(0);
	destroyAllWindows();
	system("pause");
	return 0;
}

总结

本文使用OpenCV C++ 进行物体尺寸测量,关键步骤有以下几点。

1、图像透视矫正。方便定位物体所在位置。

2、物体定位。定位所需物体位置,获取特征。

3、根据已知特征进行计算。

以上就是C++ OpenCV实现物体尺寸测量示例详解的详细内容,更多关于C++ OpenCV物体尺寸测量的资料请关注编程网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯