-
大小: 6KB文件類(lèi)型: .rar金幣: 2下載: 0 次發(fā)布日期: 2021-06-15
- 語(yǔ)言: 其他
- 標(biāo)簽: OpenCV??HoughCircles??
資源簡(jiǎn)介
Opencv內(nèi)部提供了一個(gè)基于Hough變換理論的找圓算法,HoughCircle與一般的擬合圓算法比起來(lái),各有優(yōu)勢(shì):優(yōu)勢(shì):HoughCircle對(duì)噪聲點(diǎn)不怎么敏感,并且可以在同一個(gè)圖中找出多個(gè)圓;反觀擬合圓算法,單純的擬合結(jié)果容易受噪聲點(diǎn)的影響,且不支持一個(gè)輸入中找多個(gè)圓。
因此通過(guò)優(yōu)化排序方法提高其精度。

代碼片段和文件信息
//----------------------------------------------------------------------------
//???20014.11.26?by?Charles?
/* circle_found為找圓結(jié)果的結(jié)構(gòu)
FindCircles為最終對(duì)外的接口,其他都是內(nèi)部函數(shù)
//優(yōu)化處理后打包好的HoughCircles接口,cv核心算法的優(yōu)化補(bǔ)充在該函數(shù)中添加如下:
//1、添加cvCanny的低閾值low_threshold,參數(shù)控制可以更靈活
//2、可選擇性的手動(dòng)輸入輪廓圖代替cv核心算法內(nèi)自動(dòng)調(diào)用的canny,可以更有效的進(jìn)行預(yù)處理
//3、添加分?jǐn)?shù)過(guò)濾,將找出的圓與實(shí)際輪廓對(duì)比重合率(如果輸入了輪廓圖則與輪廓圖比,否則用算法內(nèi)部自動(dòng)生成的輪廓圖對(duì)比),
// 按照分?jǐn)?shù)高低排序并過(guò)濾掉分?jǐn)?shù)過(guò)低的圓
*/
//----------------------------------------------------------------------------
#include?“ExtendHoughCircle.h“
bool?SortCircleFound(?const?circle_found?&v1?const?circle_found?&v2)//注意:本函數(shù)的參數(shù)的類(lèi)型一定要與vector中元素的類(lèi)型一致??
{??
return?v1.score?>?v2.score;//降序排列??
}??
const?int?STORAGE_SIZE?=?1?<12;
#define?hough_cmp_gt(l1l2)?(aux[l1]?>?aux[l2])
#define?cvSobel_Core?5
#define?overlap_check_brush?2
enum?{?XY_SHIFT?=?16?XY_ONE?=?1?<
static?CV_IMPLEMENT_QSORT_EX(?icvHoughSortDescent32s?int?hough_cmp_gt?const?int*?)
static?void?seqToMat(const?CvSeq*?seq?cv::OutputArray?_arr)
{
if(?seq?&&?seq->total?>?0?)
{
_arr.create(1?seq->total?seq->flags?-1?true);
cv::Mat?arr?=?_arr.getMat();
cvCvtSeqToArray(seq?arr.data);
}
else
_arr.release();
}
static?void?icvFindCirclesGradient(?CvMat*?img?cv::Mat?&contour_img?float?dp?float?min_distint?min_radius?int?max_radius
int?low_threshold?int?high_thresholdint?acc_thresholdCvSeq*?circles?int?circles_max?)
{
const?int?SHIFT?=?10?ONE?=?1?< cv::Ptr?dx?dy;
cv::Ptr?edges?accum?dist_buf;
std::vector?sort_buf;
cv::Ptr?storage;
int?x?y?i?j?k?center_count?nz_count;
float?min_radius2?=?(float)min_radius*min_radius;
float?max_radius2?=?(float)max_radius*max_radius;
int?rows?cols?arows?acols;
int?astep?*adata;
float*?ddata;
CvSeq?*nz?*centers;
float?idp?dr;
CvSeqReader?reader;
//如果輸入的輪廓圖尺寸與輸入的源圖完全相同,則使用輸入的輪廓圖,否則cvCanny提取輪廓圖,可以更靈活的預(yù)處理圖像
if?(?contour_img.cols==img->cols?&&?contour_img.rows==img->rows?)
{
edges?=?cvCloneMat(&CvMat(contour_img));
}else
{
edges?=?cvCreateMat(?img->rows?img->cols?CV_8UC1?);
cvCanny(?img?edges?low_threshold?high_threshold?3?); //添加cvCanny的低閾值low_threshold,參數(shù)控制可以更靈活
contour_img=cv::Mat(edges)+0;
}
dx?=?cvCreateMat(?img->rows?img->cols?CV_16SC1?);
dy?=?cvCreateMat(?img->rows?img->cols?CV_16SC1?);
///////////////////////////////////計(jì)算方向?qū)?shù)的Sobel核大小,重要參數(shù)////////////////////////////////////////
cvSobel(?img?dx?1?0?cvSobel_Core?);
cvSobel(?img?dy?0?1?cvSobel_Core?);
if(?dp?1.f?)
dp?=?1.f;
idp?=?1.f/dp;
accum?=?cvCreateMat(?cvCeil(img->rows*idp)+2?cvCeil(img->cols*idp)+2?CV_32SC1?);
cvZero(accum);
storage?=?cvCreateMemStorage();
nz?=?cvCreateSeq(?CV_32SC2?sizeof(CvSeq)?sizeof(CvPoint)?storage?);
centers?=?cvCreateSeq(?CV_32SC1?sizeof(CvSeq)?sizeof(int)?storage?);
rows?=?img->rows;
cols?=?img->cols;
arows?=?accum->rows
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件????????547??2017-01-12?16:33??Main.cpp
?????文件????????965??2017-01-12?16:33??ExtendHoughCircle.h
?????文件??????14129??2017-01-12?16:33??ExtendHoughCircle.cpp
-----------?---------??----------?-----??----
????????????????15641????????????????????3
評(píng)論
共有 條評(píng)論