利用Opencv中的Houghline方法進行直線檢測—python語言
這是給Python部落翻譯的文章,請在這里看原文。
在圖像處理中,霍夫變換用來檢測任意能夠用數(shù)學公式表達的形狀,即使這個形狀被破壞或者有點扭曲。
下面我們將看到利用HoughLine算法來闡述霍夫變化進行直線檢測的原理,把此算法應用到特點圖像的邊緣檢測是可取的。邊緣檢測方法請參考這篇文章–邊緣檢測。
Houghline算法基礎
直線可以表示為y=mx+c,或者以極坐標形式表示為r=xcosθ+ysinθ,其中r是原點到直線的垂直距離,θ是水平軸順時針方向到垂直線的夾角(這個方向取決于坐標的形式,在OpenCV就是采用這種極坐標形式)。
因此任意的曲線都可以用兩個參數(shù)(r,θ)表示。
HoughLine算法原理:
例子:
假設一幅100x100的圖像,在圖像中間有一條水平直線。設直線的第一個點的坐標為(x,y),在直線方程中,令參數(shù)θ=0,12,⋯,180,觀查參數(shù)r變化。對每一個參數(shù)對(r,θ),在累加器中將(r,θ)對應的單元格中的值遞增1,比如現(xiàn)在在累加器中,某個單元(50,90)的值等于1,其它的值也如此。
對于直線上的第二個點,重復上述操作。將得到的參數(shù)對(r,θ)的對應值繼續(xù)遞增,然后(50,90)對應的值等于2。實現(xiàn)上我們是對參數(shù)對(r,θ)進行投票,對直線上的每一個點重復上述操作,對每一個點,單元格(50,90)對應的值會遞增,或者說投票給參數(shù)對(50,90),而會或者不會投票給其它參數(shù)對。以這種方式,最后單元格(50,90)的值將會是大的值。然后搜索累加器的大值,將會找到參數(shù)對(50,90)。也就是說,在圖像中找到了到原點距離為50,角度為90的一條直線。
上述算法的過程被封裝成OpenCV函數(shù)cv2.HoughLines(),函數(shù)返回(r,θ)的一個數(shù)組,其中r的單位為像素,θ的單位為弧度。
# Python program to illustrate HoughLine # method for line detection import cv2 import numpy as np # Reading the required image in # which operations are to be done. # Make sure that the image is in the same # directory in which this python program is img = cv2.imread('xyz.jpg') # Convert the img to grayscale gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # Apply edge detection method on the image edges = cv2.Canny(gray,50,150,apertureSize = 3) # This returns an array of r and theta values lines = cv2.HoughLines(edges,1,np.pi/180, 200) # The below for loop runs till r and theta values # are in the range of the 2d array for r,theta in lines[0]: # Stores the value of cos(theta) in a a = np.cos(theta) # Stores the value of sin(theta) in b b = np.sin(theta) # x0 stores the value rcos(theta) x0 = a*r # y0 stores the value rsin(theta) y0 = b*r # x1 stores the rounded off value of (rcos(theta)-1000sin(theta)) x1 = int(x0 + 1000*(-b)) # y1 stores the rounded off value of (rsin(theta)+1000cos(theta)) y1 = int(y0 + 1000*(a)) # x2 stores the rounded off value of (rcos(theta)+1000sin(theta)) x2 = int(x0 - 1000*(-b)) # y2 stores the rounded off value of (rsin(theta)-1000cos(theta)) y2 = int(y0 - 1000*(a)) # cv2.line draws a line in img from the point(x1,y1) to (x2,y2). # (0,0,255) denotes the colour of the line to be #drawn. In this case, it is red. cv2.line(img,(x1,y1), (x2,y2), (0,0,255),2) # All the changes made in the input image are finally # written on a new image houghlines.jpg cv2.imwrite('houghlines3.jpg', img)
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)站題目:利用Opencv中Houghline方法實現(xiàn)直線檢測-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://sd-ha.com/article30/dceppo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站制作、定制開發(fā)、ChatGPT、App開發(fā)、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容