OpenCV 4.12.0
開源計算機視覺
載入中...
搜尋中...
無匹配項
samples/cpp/tutorial_code/ImgTrans/houghlines.cpp

一個使用霍夫線檢測器的例子

using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// 宣告輸出變數
Mat dst, cdst, cdstP;
const char* default_file = "sudoku.png";
const char* filename = argc >=2 ? argv[1] : default_file;
// 載入影像
Mat src = imread( samples::findFile( filename ), IMREAD_GRAYSCALE );
// 檢查影像是否載入成功
if(src.empty()){
printf(" Error opening image\n");
printf(" Program Arguments: [image_name -- default %s] \n", default_file);
return -1;
}
// 邊緣檢測
Canny(src, dst, 50, 200, 3);
// 複製邊緣到將以 BGR 顯示結果的影像
cvtColor(dst, cdst, COLOR_GRAY2BGR);
cdstP = cdst.clone();
// 標準霍夫線變換
vector<Vec2f> lines; // 將儲存檢測結果
HoughLines(dst, lines, 1, CV_PI/180, 150, 0, 0 ); // 執行實際檢測
// 繪製線條
for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0], 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));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
line( cdst, pt1, pt2, Scalar(0,0,255), 3, LINE_AA);
}
// 機率霍夫線變換
vector<Vec4i> linesP; // 將儲存檢測結果
HoughLinesP(dst, linesP, 1, CV_PI/180, 50, 50, 10 ); // 執行實際檢測
// 繪製線條
for( size_t i = 0; i < linesP.size(); i++ )
{
Vec4i l = linesP[i];
line( cdstP, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, LINE_AA);
}
// 顯示結果
imshow("Source", src);
imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst);
imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP);
// 等待並退出
cv::Mat::empty
return 0;
}
n 維密集陣列類
定義 mat.hpp:830
CV_NODISCARD_STD Mat clone() const
建立陣列及其底層資料的完整副本。
cv::getTickFrequency
double getTickFrequency()
_Tp y
點的 y 座標
定義 types.hpp:202
_Tp x
點的 x 座標
定義 types.hpp:201
短數值向量的模板類,是 Matx 的一個特例。
定義 matx.hpp:369
int cvRound(double value)
將浮點數舍入為最接近的整數。
Definition fast_math.hpp:200
#define CV_PI
定義 cvdef.h:380
int main(int argc, char *argv[])
定義 highgui_qt.cpp:3
定義 core.hpp:107
STL 名稱空間。