2016-03-24 5 views
3

Iはpythonopencv両方における初心者と私は地面に敷設黒い線のストリップを有し、次の画像にラインを検出する際に問題に直面しています。私が使用openCV pythonでHoughLines transformを使って正確に行を検出する方法は?

enter image description here

以下のコード:

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
edges = cv2.Canny(gray,50,150,apertureSize = 3) 
print img.shape[1] 
print img.shape 
minLineLength = img.shape[1]-1 
maxLineGap = 10 
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap) 
for x1,y1,x2,y2 in lines[0]: 
    cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2) 

正確にラインを検出できないとのみ偶数行全体を覆っていない底から第黒いストリップ上の緑の線を描画し、
また、
各行の座標をyにすることをお勧めします。

+2

これらの行は実際にはまっすぐではありません。私はハフ変換のための私自身のコードは、(この質問は理解についてのように見えるので)缶詰めの解決策を使用するよりも多くの洞察を私に与えてくれました。簡単なホームスピンハフ変換例を参照してください。ここでは:https://nabinsharma.wordpress.com/2012/12/26/linear-hough-transform-using-python/トランスフォームの出力を自分で視覚化すると、入力ラインの品質が再生されているかどうかをすぐに確認できます役割(変換空間内のより広いピーク、最大値がどこにあるのか明確ではない)。 – roadrunner66

答えて

8

Sanj、

以下に、1つではなく多くのハフラインを検出する変更コードを示します。私は線配列をループする方法を改良し、より多くの線分を得るようにしました。

あなたはパラメータが、しかし、私はそこに示すように、あなたの他のポストで輪郭アプローチはほとんどの場合、あなたのタスクを解決するためのより良い方法になると思いをさらに調整することができます How to detect horizontal lines in an image and obtain its y-coordinates using python and opencv?

import numpy as np 
import cv2 

img = cv2.imread('lines.jpg') 

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
edges = cv2.Canny(gray,50,150,apertureSize = 3) 
print img.shape[1] 
print img.shape 
minLineLength=img.shape[1]-300 
lines = cv2.HoughLinesP(image=edges,rho=0.02,theta=np.pi/500, threshold=10,lines=np.array([]), minLineLength=minLineLength,maxLineGap=100) 

a,b,c = lines.shape 
for i in range(a): 
    cv2.line(img, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA) 


cv2.imshow('edges', edges) 
cv2.imshow('result', img) 

cv2.waitKey(0) 
cv2.destroyAllWindows() 
0

私が試しましたイメージ内の水平線と垂直線を抽出することができます。このために形態操作を使用することができます。この問題のために最良の方法です。試してみてください。

Mat img = imread(argv[1]); 

if(!src.data) 
    cerr << "Problem loading image!!!" << endl; 

imshow("img .jpg", img); 

cvtColor(img, gray, CV_BGR2GRAY); 
imshow("gray", gray); 


Mat binary_image; 
adaptiveThreshold(gray, binary_image, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2); 
imshow("binary.jpg", binary_image); 

// Create the images that will use to extract the horizontal and vertical lines 
Mat horizontal = binary_image.clone(); 
Mat vertical = binary_image.clone(); 

int horizontalsize = horizontal.cols/30; 

Mat horizontalStructure = getStructuringElement(MORPH_RECT, Size(horizontalsize,1)); 


erode(horizontal, horizontal, horizontalStructure, Point(-1, -1)); 
dilate(horizontal, horizontal, horizontalStructure, Point(-1, -1)); 
imshow("horizontal", horizontal); 

int verticalsize = vertical.rows/30; 

Mat verticalStructure = getStructuringElement(MORPH_RECT, Size(1,verticalsize)); 

erode(vertical, vertical, verticalStructure, Point(-1, -1)); 
dilate(vertical, vertical, verticalStructure, Point(-1, -1)); 

imshow("vertical", vertical); 

bitwise_not(vertical, vertical); 
imshow("vertical_bit", vertical); 


Mat edges; 
adaptiveThreshold(vertical, edges, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 3, -2); 
imshow("edges", edges); 

Mat kernel = Mat::ones(2, 2, CV_8UC1); 
dilate(edges, edges, kernel); 
imshow("dilate", edges); 

Mat smooth; 
vertical.copyTo(smooth); 

blur(smooth, smooth, Size(2, 2)); 

smooth.copyTo(vertical, edges); 

imshow("smooth", vertical); 
waitKey(0); 
return 0; 
関連する問題