
x
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h> using namespace cv;
using namespace std; int main(int argc, char** argv)
{
Mat src, src_gray, dst;
src = imread("test1.jpg"); char INPUT_TITLE[] = "input image"; imshow(INPUT_TITLE, src); Canny(src, src_gray, , );
cvtColor(src_gray, dst, CV_GRAY2BGR);
imshow("edge image", src_gray);
imshow("gray", dst); //方法1(标准霍夫变换)
//vector<Vec2f> lines;
//HoughLines(src_gray, lines, 1, CV_PI / 180, 150, 0, 0);
//for (size_t i = 0; i < lines.size(); i++) {
// float rho = lines[i][0]; // 极坐标中的r长度
// float theta = lines[i][1]; // 极坐标中的角度
// Point pt1, pt2;
// double a = cos(theta), b = sin(theta);
// double x0 = a * rho, y0 = b * rho;
// // 转换为平面坐标的四个点
// pt1.x = cvRound(x0 + 1000 * (-b));//对一个double型的数进行四舍五入,并返回一个整型数!
// pt1.y = cvRound(y0 + 1000 * (a));
// pt2.x = cvRound(x0 - 1000 * (-b));
// pt2.y = cvRound(y0 - 1000 * (a));
// line(dst, pt1, pt2, Scalar(0, 0, 255), 1, CV_AA);
//} //第二种方法(概率霍夫变换)
vector<Vec4f> plines;
HoughLinesP(src_gray, plines, , CV_PI / 180.0, , , );
Scalar color = Scalar(, , );
for (size_t i = ; i < plines.size(); i++) {
Vec4f hline = plines[i];
line(dst, Point(hline[], hline[]), Point(hline[], hline[]), color, , LINE_AA);
} imshow("效果图",dst); waitKey();
return ; }
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h> using namespace cv;
using namespace std; int main(int argc, char** argv)
{
Mat src, src_gray;
src = imread("3 input.bmp"); char INPUT_TITLE[] = "input image"; imshow(INPUT_TITLE, src); //转成灰度图
cvtColor(src, src_gray, COLOR_BGR2GRAY);
GaussianBlur(src_gray, src_gray, Size(, ), , ); //进行霍夫圆变换
vector<Vec3f> circles;
HoughCircles(src_gray, circles, HOUGH_GRADIENT, 1.5, , , , , ); for (size_t i = ; i < circles.size(); i++)
{
Point center(cvRound(circles[i][]), cvRound(circles[i][]));
int radius = cvRound(circles[i][]);
//绘制圆心
circle(src, center, , Scalar(, , ), -, , );
//绘制圆的轮廓
circle(src, center, radius, Scalar(, , ), , , ); } imshow("效果图", src); waitKey();
return ; }
霍夫圆检测一般只会找出最大的一个圆