霍夫变换
2016-03-02 06:50
281 查看
霍夫变换是图像处理中识别几何形状的一种方法, 主要用来检测直线和圆形。
所谓极坐标平面化是指, 将ρ-θ的关系像x-y那样在平面内展开。
公式推导: x-y坐标中的点(x0, y0), 代入极坐标ρ-θ中得
$\quad \begin{align*}\rho = x_{0}\: cos\theta + y_{0}\: sin\theta = \sqrt{x_{0}^{2}+y_{0}^{2}}\:\left (\frac{x_{0}}{\sqrt{x_{0}^{2}+y_{0}^{2}}}\: cos\theta + \frac{y_{0}}{\sqrt{x_{0}^{2}+y_{0}^{2}}}\:sin\theta\right )
= \sqrt{x_{0}^{2}+y_{0}^{2}} \; sin(\varphi_{0} +\theta ) \end{align*} $
其中, $sin\varphi_{0} = \frac{x_{0}}{\sqrt{x_{0}^{2}+y_{0}^{2}}}$
由上述公式可以明显的看出ρ与θ之间的函数关系。
View Code
以上例程均摘自 Opencv 3.1.0 tutorial
1 平面坐标和极坐标
1) 平面坐标的点 <=> 极坐标(平面化)的曲线所谓极坐标平面化是指, 将ρ-θ的关系像x-y那样在平面内展开。
公式推导: x-y坐标中的点(x0, y0), 代入极坐标ρ-θ中得
$\quad \begin{align*}\rho = x_{0}\: cos\theta + y_{0}\: sin\theta = \sqrt{x_{0}^{2}+y_{0}^{2}}\:\left (\frac{x_{0}}{\sqrt{x_{0}^{2}+y_{0}^{2}}}\: cos\theta + \frac{y_{0}}{\sqrt{x_{0}^{2}+y_{0}^{2}}}\:sin\theta\right )
= \sqrt{x_{0}^{2}+y_{0}^{2}} \; sin(\varphi_{0} +\theta ) \end{align*} $
其中, $sin\varphi_{0} = \frac{x_{0}}{\sqrt{x_{0}^{2}+y_{0}^{2}}}$
由上述公式可以明显的看出ρ与θ之间的函数关系。
#include "opencv2/imgcodecs.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace std; using namespace cv; namespace { // windows and trackbars name const std::string windowName = "Hough Circle Detection Demo"; const std::string cannyThresholdTrackbarName = "Canny threshold"; const std::string accumulatorThresholdTrackbarName = "Accumulator Threshold"; const std::string usage = "Usage : tutorial_HoughCircle_Demo <path_to_input_image>\n"; // initial and max values of the parameters of interests. const int cannyThresholdInitialValue = 200; const int accumulatorThresholdInitialValue = 50; const int maxAccumulatorThreshold = 200; const int maxCannyThreshold = 255; void HoughDetection(const Mat& src_gray, const Mat& src_display, int cannyThreshold, int accumulatorThreshold) { // will hold the results of the detection std::vector<Vec3f> circles; // runs the actual detection HoughCircles( src_gray, circles, HOUGH_GRADIENT, 1, src_gray.rows/8, cannyThreshold, accumulatorThreshold, 0, 0 ); // clone the colour, input image for displaying purposes Mat display = src_display.clone(); 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 center circle( display, center, 3, Scalar(0,255,0), -1, 8, 0 ); // circle outline circle( display, center, radius, Scalar(0,0,255), 3, 8, 0 ); } // shows the results imshow( windowName, display); } } int main(int argc, char** argv) { Mat src, src_gray; if (argc < 2) { std::cerr<<"No input image specified\n"; std::cout<<usage; return -1; } // Read the image src = imread( argv[1], 1 ); if( src.empty() ) { std::cerr<<"Invalid input image\n"; std::cout<<usage; return -1; } // Convert it to gray cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Reduce the noise so we avoid false circle detection GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 ); //declare and initialize both parameters that are subjects to change int cannyThreshold = cannyThresholdInitialValue; int accumulatorThreshold = accumulatorThresholdInitialValue; // create the main window, and attach the trackbars namedWindow( windowName, WINDOW_AUTOSIZE ); createTrackbar(cannyThresholdTrackbarName, windowName, &cannyThreshold,maxCannyThreshold); createTrackbar(accumulatorThresholdTrackbarName, windowName, &accumulatorThreshold, maxAccumulatorThreshold); // infinite loop to display // and refresh the content of the output image // until the user presses q or Q int key = 0; while(key != 'q' && key != 'Q') { // those paramaters cannot be =0 // so we must check here cannyThreshold = std::max(cannyThreshold, 1); accumulatorThreshold = std::max(accumulatorThreshold, 1); //runs the detection, and update the display HoughDetection(src_gray, src, cannyThreshold, accumulatorThreshold); // get user key key = waitKey(10); } return 0; }
View Code
以上例程均摘自 Opencv 3.1.0 tutorial
相关文章推荐
- 虚拟化 - 每天5分钟玩转 OpenStack(2)
- Llinux环境下编译并使用OpenCV
- 4291: [PA2015]Kieszonkowe|贪心
- IOS学习笔记1`
- 虚拟化 - 每天5分钟玩转 OpenStack(2)
- MyEclipse 2015优化技巧
- Xsolla采访Fenix Soft
- MVC 的八个扩展点
- 62.View the E xhibit and examine the structure of the CUSTOMERS table.
- ConcurrentHashMap
- 边看书边做边发挥-图书软件-4
- 隋文帝杨坚隐忍弘博、沉猜刻薄的一生
- adidas originals zflb mbah dzbr
- Eclipse适用android 6.0的ADT23.0.7下载地址
- ASP.NET Core – 2300% More Requests Served Per Second
- Unity VS插件配置
- 48. Rotate Image
- HDU 2589 正方形划分(DFS+回溯)
- C语言设置文本颜色和移动光标
- Linux系统安装字体