1、简介
机器视觉中经常涉及到基础形状的检测,基础形状包括直线、圆和椭圆等,直线的检测是最简单的一种,可以直接提取边缘后采点进行点的直线方程拟合,要确定的参数就两个:斜率和截距。
而圆的检测跟直线类似,只是变量变成了三个参数(圆心的横纵坐标和圆半径),如果直接进行参数拟合,所需的计算量或许会成倍的增加,运算的时间与半径变化所需搜索的范围成正相关。
2、已知半径的圆检测
它的思路是:
1.找出边缘点;
2.以边缘点为圆心在原图像上找到所有圆周上的点,为可能的圆心,每个圆心位置对应的累加器加1;
3.遍历所有边缘点后,找出可能的圆心累计数最大的那个点,为真正的圆心。
这里参考网友Jonh-Hany的代码:
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
#define X_MAX 400
#define Y_MAX 400
#define TO_BE_BLACK 40
//radius of circles is known
int houghTrans_r(IplImage *src, IplImage *dst, IplImage *tmp, float r, int xstep, int ystep)// 100, 1, 1,
{
int width = src->width;
int height = src->height;
int channel = src->nChannels;
int xmax = width%xstep ? width/xstep+1 : width/xstep;
int ymax = height%ystep ? height/ystep+1 : height/ystep;
int i,j,x,y;
int para[X_MAX][Y_MAX] = {0};
//i,j are in the pixel space
//x,y are in the parameter space
for(j=0; j<height; j++)
{
uchar* pin = (uchar*)(src->imageData + j*src->widthStep);
for(i=0; i<width; i++)
{
//pixel is black
if(pin[channel*i] < TO_BE_BLACK)
{
float temp;
//calculate every probable y-cord based on x-cord
for(x=0; x<xmax; x++)
{
temp = r*r - (i-x*xstep)*(i-x*xstep);
temp = <