opencv 直线距离测量

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "pch.h"
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<vector>
using namespace cv;
using namespace std;
int main()
{
	
 Mat img2= imread("D:\\f1.bmp",0);
 Rect r1,r2;
 r1.x = 323;
 r1.y = 987;
 r1.width = 30;
 r1.height = 30;
 r2.x = 5355;
 r2.y = 975;
 r2.width = 30;
 r2.height = 30;
Mat d1 = Mat(img2, r1);
Mat d2= Mat(img2, r1);
threshold(d1, d1, 200, 255, THRESH_BINARY);
threshold(d2, d2, 200, 255, THRESH_BINARY);
int a = d1.at<uchar>(2, 0);
int b = d2.at<uchar>(2, 0);
vector<int> l1;
vector<int> l2;
if (a == 0)
{
	for (int i = 0; i < d1.rows; i++)
	{
		for (int j = 0; j < d1.cols; j++)
		{
			if (d1.at<uchar>(i, j) == 255)
			{
				l1.push_back(j);
				break;
			}
				
		}
	}
}

if (b == 0)
{
	for (int i = 0; i < d2.rows; i++)
	{
		for (int j = 0; j < d2.cols; j++)
		{
			if (d2.at<uchar>(i, j) == 255)
			{
				l2.push_back(j);
				break;
			}
				
		}
	}
}




int s1 = 0;
int s2 = 0;
for (int i = 0; i < l1.size(); i++)
{
	s1 = l1[i] + s1;
}
int m1 = s1 / l1.size();
int c1 = r1.x + m1;
for (int i = 0; i < l2.size(); i++)
{
	s2 = l2[i] + s2;
}
int m2 = s2 / l2.size();
int c2 = r2.x + m2;

int c3 = c2 - c1;

 imshow("匹配后的图片", d1);

 waitKey(0);
    return 0;

}

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xutingjie168

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值