霍夫直线变换
关于变换的原理,这篇博文写得非常详细,传送门
也可以看这个视频,传送门
Canny(gray_src,dst,50,100,3,false);
vector<Vec4f> lines; //定义容器
HoughLines(gray_src, lines, 1, CV_PI / 180, 10, 0, 10);//霍夫变换
for (size_t i = 0; i < lines.size(); i++)
{
Vec4f h1 = lines[i]; //获取到线段
line(gray_src,Point(h1[0],h1[1]),Point(h1[2],h1[3]),Scalar(0,0,255),3,CV_AA);//画线
}
霍夫圆变换
原理参考这篇博文,传送门
// 中值滤波
Mat moutput;
medianBlur(src, moutput, 3);
cvtColor(moutput, moutput, CV_BGR2GRAY);
// 霍夫圆检测
vector<Vec3f> pcircles;
HoughCircles(moutput, pcircles, CV_HOUGH_GRADIENT, 1, 10, 100, 30, 5, 50);
src.copyTo(dst);
for (size_t i = 0; i < pcircles.size(); i++) {
Vec3f cc = pcircles[i];
circle(dst, Point(cc[0], cc[1]), cc[2], Scalar(0, 0, 255), 2, LINE_AA);//画圆
circle(dst, Point(cc[0], cc[1]), 2, Scalar(198, 23, 155), 2, LINE_AA);//画圆心
}