OpenCV找圆

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
void LeastSquaresCircleFitting(vector<Point2d> Points, Point2d &Centroid, double& dRadius);

int main()

{
	Mat src_gray, src_binary, src_blur, detected_edges;
	Mat src = imread("月亮.bmp", 1);
	cvtColor(src, src_gray, CV_BGR2GRAY);
	medianBlur(src_gray, src_blur, 21);
	threshold(src_blur, src_binary, 50, 255, 0);
	imwrite("src_binary.jpg", src_binary);
	Canny(src_binary, detected_edges, 50, 150, 3);
	imwrite("筋片detected_edges.jpg", detected_edges);
	//----------------------------------------------------------------//
	vector<Point2d> m_Points;	
	//扫描取点,从左到右,逐行扫描 
	for (int i = 100; i <1900; ++i)
	{
		uchar* data = detected_edges.ptr<uchar>(i);
		for (int j = 1570; j < 3000; ++j)
		{
			if (data[j] == 255)
			{
				m_Points.push_back(Point(j, i));
				break;
			}
		}
	}

	//扫描取点,从右到左,逐行扫描 
	for (int i = 100; i < 1900; ++i)
	{
		uchar* data = detected_edges.ptr<uchar>(i);
		for (int j = 1570; j > 50; --j)
		{
			if (data[j] == 255)
			{
				m_Points.push_back(Point(j, i));
				break;
			}
		}
	}

	//扫描后的点保存在图像scan_dst中
	Mat scan_dst = Mat::zeros(cv::Size(src_gray.cols, src_gray.rows), src_gray.type());
	vector<Point2d>::iterator iter1;
	vector<Point2d>::iterator end1 = m_Points.end();
	for (iter1 = m_Points.begin(); iter1 != end1; ++iter1)
	{
		scan_dst.at<uchar>((*iter1).x, (*iter1).y) = 255;
	}
	//显示扫描后的图
	//namedWindow("Result1", 0); imshow("Result1", scan_dst);
	imwrite("scan_dst.jpg", scan_dst);


	//	//求半径
	double m_dRadius_DianPian = 0;
	Point2d m_Centroid_DianPian;
	LeastSquaresCircleFitting(m_Points, m_Centroid_DianPian, m_dRadius_DianPian);
	/////最小二乘拟合圆
	cout << "最小二乘拟合结果 " << endl << " " << endl << endl;
	cout << "垫片直径 = " << endl << " " << m_dRadius_DianPian * 2 * 2.46 << endl << endl;
	cout << "m_Centroid.x = " << endl << " " << m_Centroid_DianPian.x << endl << endl;
	cout << "m_Centroid.y = " << endl << " " << m_Centroid_DianPian.y << endl << endl;

	//**********在原图画拟合圆

	//***垫片图上画圆
	circle(src, Point2d(m_Centroid_DianPian.x, m_Centroid_DianPian.y), m_dRadius_DianPian, Scalar(255, 0, 0), 10, 8, 0);
	//在原图画一个圆圈点
	cv::Point2d point1;//特征点,用以画在图像中  
	point1.x = m_Centroid_DianPian.x;//特征点在图像中横坐标  
	point1.y = m_Centroid_DianPian.y;//特征点在图像中纵坐标  
	cv::circle(src, point1, 4, cv::Scalar(255, 0, 0), 10, 8, 0);
	//*****//
	//图片的缩小与放大
	Mat scale_dst = Mat::zeros(1032, 1544, CV_8UC3); //转化为1544*1032大小的
	resize(src, scale_dst, scale_dst.size());
	namedWindow("src", 0); imshow("src", scale_dst);
	waitKey(0);
	return 0;
}

void LeastSquaresCircleFitting(vector<Point2d> Points, Point2d &Centroid, double& dRadius)
{
	int iNum = (int)Points.size();
	double X1 = 0.0;
	double Y1 = 0.0;
	double X2 = 0.0;
	double Y2 = 0.0;
	double X3 = 0.0;
	double Y3 = 0.0;
	double X1Y1 = 0.0;
	double X1Y2 = 0.0;
	double X2Y1 = 0.0;
	vector<Point2d>::iterator iter;
	vector<Point2d>::iterator end = Points.end();
	for (iter = Points.begin(); iter != end; ++iter)
	{
		X1 = X1 + (*iter).x;
		Y1 = Y1 + (*iter).y;
		X2 = X2 + (*iter).x * (*iter).x;
		Y2 = Y2 + (*iter).y * (*iter).y;
		X3 = X3 + (*iter).x * (*iter).x * (*iter).x;
		Y3 = Y3 + (*iter).y * (*iter).y * (*iter).y;
		X1Y1 = X1Y1 + (*iter).x * (*iter).y;
		X1Y2 = X1Y2 + (*iter).x * (*iter).y * (*iter).y;
		X2Y1 = X2Y1 + (*iter).x * (*iter).x * (*iter).y;
	}
	double C = 0.0;
	double D = 0.0;
	double E = 0.0;
	double G = 0.0;
	double H = 0.0;
	double a = 0.0;
	double b = 0.0;
	double c = 0.0;
	C = iNum * X2 - X1 * X1;
	D = iNum * X1Y1 - X1 * Y1;
	E = iNum * X3 + iNum * X1Y2 - (X2 + Y2) * X1;
	G = iNum * Y2 - Y1 * Y1;
	H = iNum * X2Y1 + iNum * Y3 - (X2 + Y2) * Y1;
	a = (H * D - E * G) / (C * G - D * D);
	b = (H * C - E * D) / (D * D - G * C);
	c = -(a * X1 + b * Y1 + X2 + Y2) / iNum;
	double A = 0.0;
	double B = 0.0;
	double R = 0.0;
	A = a / (-2);
	B = b / (-2);
	R = double(sqrt(a * a + b * b - 4 * c) / 2);
	Centroid.x = A;
	Centroid.y = B;
	dRadius = R;
}

全部评论

相关推荐

10-19 10:28
已编辑
西南石油大学 后端工程师
团孝子已上线feeling:面了很多家公司,能感受到目前只有小公司+外包喜欢问八股。大厂虽然也问八股,但是是从实习、项目中进行提问,并且大厂会问很深,面试官也会对你的回答进行思考➕追问,所以准备大厂面试前一定要备好相关资料。对于算法,我做的是codetop前100+力扣hot100+力扣高频150,面试中实感hot100就足够,基本上只要是hot100就秒答。对于项目和八股,我做的也是烂大街的星球项目,八股则是看小林和问ai,自己也写了很多技术博客和画了很多思维导图,并且自己也尝试用嘴巴说出来,不只停留于纸面。运气也很重要,必须要让面试官/HR看到简历才行,所以建议投递时间是下午两点。tl:第一岗位9.9&nbsp;投递9.10&nbsp;一面(一面评价:最近见过最强的大三,结束五分钟后约二面,都晚上九点了不下班吗)9.11&nbsp;二面(三道算法a出两道,反问评价:经验不够等横向,我实习生要啥经验)9.21挂(实习时间过短+其他原因,想要一年实习的,为什么不招个正职)第二岗位10.10投递10.11约面(主管打电话,说看到我之前投递记录了想要我挂qa职进去干后端,同意)10.14&nbsp;一面(无八股,主动说确实很强,意愿很强)10.16&nbsp;oc其余,友邦,东软,东华,惠择,用友oc已拒京东测开一面挂(投后端被测开捞)腾讯测试已拒(投后端被测开捞)ps:表扬惠择的主管面,没怎么问技术(可能是一面面试官沟通过了),全程一起讲大道理,解答了心中很多疑惑,也告诉我以面试官角度来看怎么选候选人,如果可以下次一定选惠择
HeaoDng:美团好像可以触发一面通
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务