您的位置:首页 > 编程语言

图像识别与处理之Opencv——霍夫变换编程思路(11月2日暂存)

2017-11-02 19:33 645 查看
三、编程思路

1. 读取一幅带处理二值图像,最好背景为黑色;

2. 取得源像素数据;

3. 根据直线的霍夫变换公式完成霍夫变换,预览霍夫空间结果;

4. 寻找最大霍夫值,设置阈值,反变换到图像RGB值空间(程序难点之一);

5. 越界处理,显示霍夫变换处理以后的图像;

代码暂存

#include "stdafx.h"

#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
Mat image = imread("all1.jpg");
Mat imageGray;   //
cvtColor(image, imageGray, CV_RGB2GRAY);  //将数组的通道从一个颜色空间转换另外一个颜色空间,转化为二值化图

GaussianBlur(imageGray, imageGray, Size(3, 3), 1, 2);  //高斯滤波,高斯模糊降低噪声 平滑它,否则可能会检测到很多假圆

namedWindow("Gaussian Image", 1);
imshow("Gaussian Image", imageGray);  //高斯滤波后的图像

vector  <Vec3f>  circles;   //三维向量,存储圆心和半径

/************************************************************************************************************

HoughCircles( InputArray image, OutputArray circles,
int method, double dp, double minDist,
double param1 = 100, double param2 = 100,
int minRadius = 0, int maxRadius = 0 );

*********************************************************************************************************/

HoughCircles(imageGray, circles, CV_HOUGH_GRADIENT, 1, 1, 200, 160, 0);   // 关键句,霍夫梯度法

for (int i = 0; i<circles.size(); i++)    //
{
Point circleCenter(circles[i][0], circles[i][1]);   //

int radius = circles[i][2];   //

circle(image, circleCenter, radius, Scalar(0, 0, 255), 3); //做圆

circle(image, circleCenter, 3, Scalar(255, 0, 0), 3);    //圆心
}

namedWindow("Circle Image", 1);
imshow("Circle Image", image);  //在图像上显示圆

namedWindow("Gaussian Circle Image", 1);
imshow("Gaussian Circle Image", imageGray);  //在图像上显示圆
waitKey(0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: