当前位置:   article > 正文

Opencv实践项目之实时物体的测量——c++_c sharp语言实现视频中物体长度测量的例子

c sharp语言实现视频中物体长度测量的例子

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

Mat matrix;
Point2f center;
Mat frame;
float scale = 0.25;
bool flag = false;
  • 1
  • 2
  • 3
  • 4
  • 5

主函数

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

参照物识别

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/765077
推荐阅读
相关标签
  

闽ICP备14008679号