19,468
社区成员
发帖
与我相关
我的任务
分享
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
const int kvalue = 15;//双边滤波邻域大小
int main()
{
Mat src_color = imread("c://kk.bmp");//读取原彩色图
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_RGB2GRAY);
//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);
Mat canSrc;//对灰度图像进行双边滤波
Canny(bf,canSrc,10,250,5);
imshow("canny", canSrc);
bilateralFilter(canSrc, dst, kvalue, kvalue*2, kvalue/2);
vector<Vec3f> circles;//声明一个向量,保存检测出的圆的圆心坐标和半径
// HoughCircles(canSrc, circles, CV_HOUGH_GRADIENT, 1.5, 100, 130, 38, 20, 100);//霍夫变换检测圆
HoughCircles(canSrc, circles, CV_HOUGH_GRADIENT, 1.5, 100, 130, 38, 20, 300);//霍夫变换检测圆
cout<<circles.size()<<endl;
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, 0, Scalar(0, 255, 0), -1, 8, 0 );
circle( dst, center, radius, Scalar(255, 255, 255), 1, 8, 0 );
cout << cvRound(circles[i][0]) << "\t" << cvRound(circles[i][1]) << "\t"
<< cvRound(circles[i][2]) << endl;//在控制台输出圆心坐标和半径
}
//dilate(canSrc, dst,getStructuringElement(MORPH_ELLIPSE,Size(5,5)));
imshow("特征提取", dst);
//imwrite("dst.png", dst);
waitKey();
}