[英]How to detect lines in OpenCV?
我正在嘗試檢測停車線,如下所示。
我希望得到的是清晰的線條和交叉線上的 (x,y) 位置。 然而,結果並不是很有希望。
我想主要有兩個原因:
有些線路非常破損或丟失。 即使是人眼也能清楚地識別它們。 盡管 HoughLine 可以幫助連接一些缺失的線,但由於 HoughLine 有時會將不必要的線連接在一起,我寧願手動完成。
有一些重復的行。
工作的一般管道如下所示:
import cv2
import numpy as np
import matplotlib
from matplotlib.pyplot import imshow
from matplotlib import pyplot as plt
# white color mask
img = cv2.imread(filein)
#converted = convert_hls(img)
image = cv2.cvtColor(img,cv2.COLOR_BGR2HLS)
lower = np.uint8([0, 200, 0])
upper = np.uint8([255, 255, 255])
white_mask = cv2.inRange(image, lower, upper)
# yellow color mask
lower = np.uint8([10, 0, 100])
upper = np.uint8([40, 255, 255])
yellow_mask = cv2.inRange(image, lower, upper)
# combine the mask
mask = cv2.bitwise_or(white_mask, yellow_mask)
result = img.copy()
cv2.imshow("mask",mask)
height,width = mask.shape
skel = np.zeros([height,width],dtype=np.uint8) #[height,width,3]
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
temp_nonzero = np.count_nonzero(mask)
while(np.count_nonzero(mask) != 0 ):
eroded = cv2.erode(mask,kernel)
cv2.imshow("eroded",eroded)
temp = cv2.dilate(eroded,kernel)
cv2.imshow("dilate",temp)
temp = cv2.subtract(mask,temp)
skel = cv2.bitwise_or(skel,temp)
mask = eroded.copy()
cv2.imshow("skel",skel)
#cv2.waitKey(0)
edges = cv2.Canny(skel, 50, 150)
cv2.imshow("edges",edges)
lines = cv2.HoughLinesP(edges,1,np.pi/180,40,minLineLength=30,maxLineGap=30)
i = 0
for x1,y1,x2,y2 in lines[0]:
i+=1
cv2.line(result,(x1,y1),(x2,y2),(255,0,0),1)
print i
cv2.imshow("res",result)
cv2.waitKey(0)
我想知道為什么在選擇某種顏色的第一步后,線條被打破並帶有噪音。 我認為在這一步中我們應該做一些事情來使虛線成為一條完整的、噪音較小的線。 然后嘗試應用一些東西來做 Canny 和 Hough 線。 有任何想法嗎?
這是我的管道,也許它可以給你一些幫助。
首先,獲取灰度圖像並處理GaussianBlur。
img = cv2.imread('src.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
二、過程邊緣檢測使用Canny。
low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
然后,使用 HoughLinesP 獲取線條。 您可以調整參數以獲得更好的性能。
rho = 1 # distance resolution in pixels of the Hough grid
theta = np.pi / 180 # angular resolution in radians of the Hough grid
threshold = 15 # minimum number of votes (intersections in Hough grid cell)
min_line_length = 50 # minimum number of pixels making up a line
max_line_gap = 20 # maximum gap in pixels between connectable line segments
line_image = np.copy(img) * 0 # creating a blank to draw lines on
# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
min_line_length, max_line_gap)
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5)
最后,在 srcImage 上繪制線條。
# Draw the lines on the image
lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)
這是我最后的表演。
最終圖像:
我不確定您到底在問什么,因為您的帖子中沒有任何問題。
LSD(線段檢測器)是一種很好且強大的檢測線段的技術,從 openCV 3 開始就可以在 openCV 中使用。
這是一些簡單的基本 C++ 代碼,可以很容易地轉換為 python:
int main(int argc, char* argv[])
{
cv::Mat input = cv::imread("C:/StackOverflow/Input/parking.png");
cv::Mat gray;
cv::cvtColor(input, gray, CV_BGR2GRAY);
cv::Ptr<cv::LineSegmentDetector> det;
det = cv::createLineSegmentDetector();
cv::Mat lines;
det->detect(gray, lines);
det->drawSegments(input, lines);
cv::imshow("input", input);
cv::waitKey(0);
return 0;
}
給出這個結果:
哪個看起來比您的圖像更適合進一步處理(沒有重復行等)
對於您問題的第一部分,這里有一些很好的答案,但至於第二部分(找到線的交點),我沒有看到很多。
我建議你看看Bentley-Ottmann算法。
編輯:使用 VeraPoseidon 的 Houghlines 實現和此處鏈接的第二個庫,我設法獲得了以下交叉點檢測結果。 感謝 Vera 和圖書館作者的出色工作。 綠色方塊代表檢測到的交叉點。 有一些錯誤,但這對我來說似乎是一個非常好的起點。 似乎您實際上想要檢測交叉點的大多數位置都檢測到了多個交叉點,因此您可能會在圖像上運行一個適當大小的窗口,該窗口尋找多個交叉點並將真正的交叉點視為該窗口激活的一個。
這是我用來產生該結果的代碼:
import cv2
import numpy as np
import isect_segments_bentley_ottmann.poly_point_isect as bot
img = cv2.imread('parking.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
rho = 1 # distance resolution in pixels of the Hough grid
theta = np.pi / 180 # angular resolution in radians of the Hough grid
threshold = 15 # minimum number of votes (intersections in Hough grid cell)
min_line_length = 50 # minimum number of pixels making up a line
max_line_gap = 20 # maximum gap in pixels between connectable line segments
line_image = np.copy(img) * 0 # creating a blank to draw lines on
# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
min_line_length, max_line_gap)
print(lines)
points = []
for line in lines:
for x1, y1, x2, y2 in line:
points.append(((x1 + 0.0, y1 + 0.0), (x2 + 0.0, y2 + 0.0)))
cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 5)
lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)
print(lines_edges.shape)
#cv2.imwrite('line_parking.png', lines_edges)
print points
intersections = bot.isect_segments(points)
print intersections
for inter in intersections:
a, b = inter
for i in range(3):
for j in range(3):
lines_edges[int(b) + i, int(a) + j] = [0, 255, 0]
cv2.imwrite('line_parking.png', lines_edges)
您可以使用類似以下代碼塊的策略來刪除小區域內的多個交叉點:
for idx, inter in enumerate(intersections):
a, b = inter
match = 0
for other_inter in intersections[idx:]:
if other_inter == inter:
continue
c, d = other_inter
if abs(c-a) < 15 and abs(d-b) < 15:
match = 1
intersections[idx] = ((c+a)/2, (d+b)/2)
intersections.remove(other_inter)
if match == 0:
intersections.remove(inter)
不過,您必須使用窗口功能。
如果調整 maxLineGap 或侵蝕內核的大小會發生什么。 或者,您可以找到線之間的距離。 你必須通過成對的線說 ax1,ay1 到 ax2,ay2 cf bx1,by1 到 bx2,by2 你可以找到直角梯度(-1 線的梯度)與線 b 相交的點。 基本學校幾何和聯立方程,例如:
x = (ay1 - by1) / ((by2 - by1) / (bx2 - bx1) + (ax2 - ax1) / (ay2 - ay1))
# then
y = by1 + x * (by2 - by1) / (bx2 - bx1)
並將 x,y 與 ax1,ay1 進行比較
PS,您可能需要檢查 ax1,ay1 和 bx1,by1 之間的距離,因為您的某些線看起來是其他線的延續,而這些線可能會被最近點技術消除。
我是初學者。 我得到了一些可能對這個問題有幫助的東西。
一種檢測圖像中線條的簡單方法。
下面是在 google colab 中執行的代碼
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
!wget https://i.stack.imgur.com/sDQLM.png
#read image
image = cv2.imread( "/content/sDQLM.png")
#convert to gray
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#performing binary thresholding
kernel_size = 3
ret,thresh = cv2.threshold(gray,200,255,cv2.THRESH_BINARY)
#finding contours
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
#drawing Contours
radius =2
color = (30,255,50)
cv2.drawContours(image, cnts, -1,color , radius)
# cv2.imshow(image) commented as colab don't support cv2.imshow()
cv2_imshow(image)
# cv2.waitKey()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.