OpenCV學習筆記(三) 直線提取

2021-10-07 17:37:24 字數 2558 閱讀 1909

首先熟悉opencv中的直線檢測的函式。我在數字影象處理課程中學習到的直線檢測的方法是hough變換,因此此處也使用hough相關的函式進行直線檢測與提取。

1、houghlines()

函式原型

2、houghlinesp()函式原型

void cv::houghlinesp	(	inputarray 	image,

outputarray lines,

double rho,

double theta,

int threshold,

double minlinelength = 0,

double maxlinegap = 0

)

變數解釋:

這裡是此函式的示例**:

#include #include #include #include //標頭檔案可以不要這麼多,我是因為暫時還不熟悉,為了方便就都加上去了

using namespace std;

using namespace cv;

void drawdetectlines(mat& image, const vector& lines, scalar color);

int main()

mat i;

cvtcolor(img1_raw, i, color_bgr2gray);

mat contours;

canny(i, contours, 125, 350);

threshold(contours, contours, 128, 255, thresh_binary);//大津法自適應閾值判斷

vectorlines;

// 檢測直線,最小投票為90,線條不短於50,間隙不小於10

houghlinesp(contours, lines, 1, cv_pi / 180, 80, 50, 10);

drawdetectlines(img1_raw, lines, scalar(0, 255, 0));

imwrite("d:/desktop/2020暑期實習/6月30號測試/輸出/直線檢測與提取/line_1.png", img1_raw);

cout << "沒有意外發生";

return 0;

}void drawdetectlines(mat& image, const vector& lines, scalar color)

}結果如圖:

原圖

直線提取

opencv 霍夫曼變換 直線提取

import cv2 import numpy as np img cv2.imread hd.jpeg 0 img cv2.gaussianblur img,3,3 0 edges cv2.canny img,50,150,aperturesize 3 lines cv2.houghlines e...

opencv學習筆記 21 直線檢測

前提條件 邊緣檢測已經完成 平面空間 極座標 import cv2 as cv import numpy as np defline detection image 自己寫 param image return gray cv.cvtcolor image,cv.color bgr2gray ape...

OpenCV 學習(直線擬合

hough 變換可以提取影象中的直線。但是提取的直線的精度不高。而很多場合下,我們需要精確的估計直線的引數,這時就需要進行直線擬合。直線擬合的方法很多,比如一元線性回歸就是一種最簡單的直線擬合方法。但是這種方法不適合用於提取影象中的直線。因為這種演算法假設每個資料點的x 座標是準確的,y 座標是帶有...