霍夫变换提取圆心坐标,并拟合直线

<span style="font-family:Microsoft YaHei;font-size:14px;">#include <cmath>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

const int kvalue = 15;//双边滤波邻域大小

int main()
{
	Mat src_color = imread("1.png");//读取原彩色图
	imshow("原图-彩色", src_color);

	//声明一个三通道图像,像素值全为0,用来将霍夫变换检测出的圆画在上面
	Mat dst(src_color.size(), src_color.type());
	dst = Scalar::all(0);

	Mat src_gray;//彩色图像转化成灰度图
	cvtColor(src_color, src_gray, COLOR_BGR2GRAY);
	imshow("原图-灰度", src_gray);
	imwrite("src_gray.png", src_gray);

	Mat bf;//对灰度图像进行双边滤波
	bilateralFilter(src_gray, bf, kvalue, kvalue*2, kvalue/2);
	imshow("灰度双边滤波处理", bf);
	imwrite("src_bf.png", bf);

	vector<Vec3f> circles;//声明一个向量,保存检测出的圆的圆心坐标和半径
	HoughCircles(bf, circles, CV_HOUGH_GRADIENT, 1.5, 20, 130, 38, 10, 50);//霍夫变换检测圆

	std::vector<int> v;//保存圆心的横坐标减纵坐标的绝对值,用于区分两排灯
	cout << "x=\ty=\tr=" << endl;

	for(size_t i = 0; i < circles.size(); i++)//把霍夫变换检测出的圆画出来
	{
		Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
		int radius = cvRound(circles[i][2]);

		circle( dst, center, 1, Scalar(0, 255, 0), -1, 8, 0 );//画出圆心,颜色为绿色
		circle( dst, center, radius, Scalar(0, 0, 255), 1, 8, 0 );//画出圆的轮廓,颜色为红色

		v.push_back(abs(center.x-center.y));//存储圆心的横坐标减纵坐标的绝对值,用于区分两排灯

		cout << center.x << "\t" << center.y << "\t" << radius << endl;//在控制台输出圆心坐标和半径
	}

	sort(v.begin(), v.end());//从小到大排序

	std::vector<Point> points1, points2;//声明点向量,分别存储两排灯的圆心坐标

	for (size_t i = 0; i < circles.size(); i++)//用来区分两排灯
	{
		Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
		if (abs(center.x-center.y) < v[12])
			points1.push_back(Point(center.x, center.y));//存储从左上到右下那排灯的圆心坐标
		else
			points2.push_back(Point(center.x, center.y));//存储从左下到右上那排灯的圆心坐标
	}

	cv::Vec4f line1, line2;//拟合直线
	fitLine(Mat(points1), line1, CV_DIST_L2, 0, 0.01, 0.01);
	fitLine(Mat(points2), line2, CV_DIST_L2, 0, 0.01, 0.01);

	int x01 = (int)line1[2];
	int y01 = (int)line1[3];
	int x11 = (int)(x01 + 300*line1[0]);
	int y11 = (int)(y01 + 300*line1[1]);
	int x21 = (int)(x01 - 300*line1[0]);
	int y21 = (int)(y01 - 300*line1[1]);	

	int x02 = (int)line2[2];
	int y02 = (int)line2[3];
	int x12 = (int)(x02 + 300*line2[0]);
	int y12 = (int)(y02 + 300*line2[1]);
	int x22 = (int)(x02 - 300*line2[0]);
	int y22 = (int)(y02 - 300*line2[1]);	

	cv::line(dst, Point(x11, y11), Point(x21, y21), Scalar(255, 255, 255), 1);//画出直线
	cv::line(dst, Point(x12, y12), Point(x22, y22), Scalar(255, 255, 255), 1);

	imshow("特征提取", dst);
	imwrite("dst.png", dst);

	waitKey();
}</span>

原图:

效果图:

时间: 2024-08-05 21:35:29

霍夫变换提取圆心坐标,并拟合直线的相关文章

poj 3384 Feng Shui 半平面交的应用 求最多覆盖凸多边形的面积的两个圆 的圆心坐标

题目来源: http://poj.org/problem?id=3384 分析: 用半平面交将多边形的每条边一起向"内"推进R,得到新的多边形(半平面交),然后求多边形的最远两点. 代码如下: const double EPS = 1e-10; const int Max_N = 105 ; struct Point{ double x,y; Point(){} Point(double x, double y):x(x),y(y){} Point operator - (Point

halcon之最小二乘拟合直线

如果不了解最小二乘算法 请先阅读: Least squares的算法细节原理https://en.wikipedia.org/wiki/Least_squares 通常在halcon中拟合直线会用houghline或者 fitline.本文提供一种新的选择,用halcon的矩阵操作实现最小二乘拟合直线 首先随机生成一组数据 Mx:=[100:10:500] tuple_length(Mx,len) tuple_gen_const(len,5,r) Ma:=2 Mb:=40 tuple_rand(

c语言求平面上2个坐标点的直线距离、求俩坐标直线距离作为半径的圆的面积、递归、菲波那次数列、explode

1 #include <stdio.h> 2 #include <math.h> 3 #include <string.h> 4 5 char explode( char * str , char symbol ); 6 7 8 double distance ( int x1 , int y1 , int x2 , int y2 ); // 求平面上2个坐标点的直线距离 9 double circle_area( double radius ); // 求圆面积. r

已知圆上三个点坐标,求圆半径 r 和 圆心坐标

问题: 已知圆上三个点坐标分别为(x1,y1).(x2,y2).(x3,y3) 求圆半径R和圆心坐标(X,Y) X,Y,R为未知数,x1,y1,x2,y2,x3,y3为常数 则由圆公式:(x1-X)²+(y1-Y)²=R²      (1)式(x2-X)²+(y2-Y)²=R²      (2)式(x3-X)²+(y3-Y)²=R²      (3)式(1)-(2),就是左边减左边,右边减右边,得到x1²-2Xx1+X²+(y1²-2Yy1+Y²)-(x2²-2Xx2+X²)-(y2²-2Yy2

Halcon拟合直线

read_image(Image,'C:/Users/研发/Pictures/Saved Pictures/qq.bmp') dev_set_draw ('margin') get_image_size(Image, Width, Height) Row:=[479,479,479,479,479] Col:=[450,455,460,465,470] RowAfter:=[] ColAfter:=[] RowSelect:=[] ColSelect:=[] for Index:=0 to 4

[MXNet逐梦之旅]练习一&#183;使用MXNet拟合直线手动实现

[MXNet逐梦之旅]练习一·使用MXNet拟合直线手动实现 code #%% from matplotlib import pyplot as plt from mxnet import autograd, nd import random #%% num_inputs = 1 num_examples = 100 true_w = 1.56 true_b = 1.24 features = nd.arange(0,10,0.1).reshape((-1, 1)) labels = true_

python matplotlib拟合直线

import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif'] = ['SimHei'] def linear_regression(x, y): N = len(x) sumx = sum(x) sumy = sum(y) sumx2 = sum(x ** 2) sumxy = sum(x * y) A

分析一则halcon抓边拟合直线的小案例

例图: 完整算法: 1 read_image (Image, 'C:/Users/Administrator/Desktop/1.png') 2 threshold (Image, Regions, 0, 112) 3 4 skeleton(Regions,TriangleSkeleton) 5 6 gen_contours_skeleton_xld(TriangleSkeleton,TriangleContours,1,'filter') 7 8 segment_contours_xld(Tr

(八)最小二乘法 拟合直线

1 #coding=utf-8 2 from numpy import * 3 import numpy as np 4 import matplotlib.pyplot as plt 5 6 plt.close() 7 fig=plt.figure() 8 plt.grid(True) 9 plt.axis([0,1,0,5]) 10 #plt.title("") 11 # 每个小的方括号是一列 12 point=[[0,0.9],[0.2,1.9],[0.4,2.8],[0.6,3