2015-10-21 92 views
0

我有matlab代碼來獲取Hough變換矩陣,theta和rho值。如何獲取Hough變換矩陣,hough變換的theta和rho值在opencv中cpp

[H,T,R] = hough(EdgeImage); 

如何獲得OpenCV中HTR值?

+1

請分享你迄今在opencv中嘗試過的東西。它似乎支持Hough,那麼你的具體問題是什麼? –

+0

我找到了sobel邊緣圖像,現在我想對sobel邊緣圖像應用hough變換來獲得Hough變換矩陣,theta和rho值。 –

+0

如果你分享你的工作,有人(而不是我)可能會幫助你。請參閱http://stackoverflow.com/questions/15279892/to-find-minute-circles-using-hough-circle-function-in-opencv-for-iris作爲您的區域中一個好問題的樣子的例子。 –

回答

2

在OpenCV中,調用HT爲:

vector<Vec2f> lines; 
HoughLines(edges, lines, 1, CV_PI/180.0, 100); 

其中edge是您的二進制輸入圖像,並且linesVec2f一個std::vector,即2個浮子的值的矢量:第一值是rho,第二個是theta

OpenCV不會輸出H參數空間,如果您還需要自己編寫一些代碼並使HoughLines也輸出H值。但是,這在實踐中很少需要。

這是一個關於如何使用標準Hough變換,改編自OpenCV tutorials一個簡單的例子:

#include <opencv2\opencv.hpp> 
#include <vector> 
using namespace std; 
using namespace cv; 

int main() 
{ 
    // Load image 
    Mat3b img = imread("path_to_image"); 
    Mat3b res = img.clone(); 

    // Convert to grayscale 
    Mat1b gray; 
    cvtColor(img, gray, COLOR_BGR2GRAY); 

    // Compute edges 
    Mat1b edges; 
    Canny(gray, edges, 100, 400); 

    vector<Vec2f> lines; 
    HoughLines(edges, lines, 1, CV_PI/180.0, 100); 

    for (size_t i = 0; i < lines.size(); i++) 
    { 
     // rho and theta values 
     float rho = lines[i][0]; 
     float theta = lines[i][1]; 

     // Draw the line 
     Point pt1, pt2; 
     double a = cos(theta), b = sin(theta); 
     double x0 = a*rho, y0 = b*rho; 
     pt1.x = cvRound(x0 + 1000 * (-b)); 
     pt1.y = cvRound(y0 + 1000 * (a)); 
     pt2.x = cvRound(x0 - 1000 * (-b)); 
     pt2.y = cvRound(y0 - 1000 * (a)); 
     line(res, pt1, pt2, Scalar(0, 0, 255), 2); 
    } 

    imshow("Input", img); 
    imshow("Output", res); 
    waitKey(); 

    return 0; 
} 

輸入:

enter image description here

輸出:

enter image description here