2017-07-28 70 views
0

我看了幾篇文章和鋸線檢測的視頻,因此決定學習它是如何工作 我完全新的OpenCV所以懇請原諒我愚蠢的質疑。 我把Udacity開源項目開發裏檢測,但我無法執行代碼。我越來越值錯誤,我無法理解值錯誤浮NAN整數霍夫線

代碼:

import numpy as np 
 
import cv2 
 
import math 
 
import matplotlib.pyplot as plt 
 

 

 
def grayscale(img): 
 
    """Applies the Grayscale transform 
 
    This will return an image with only one color channel 
 
    but NOTE: to see the returned image as grayscale 
 
    you should call plt.imshow(gray, cmap='gray')""" 
 
    return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
 

 

 
def canny(img, low_threshold, high_threshold): 
 
    """Applies the Canny transform""" 
 
    return cv2.Canny(img, low_threshold, high_threshold) 
 

 

 
def gaussian_blur(img, kernel_size): 
 
    """Applies a Gaussian Noise kernel""" 
 
    return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0) 
 

 

 
def region_of_interest(img, vertices): 
 
    """ 
 
    Applies an image mask. 
 
    Only keeps the region of the image defined by the polygon 
 
    formed from `vertices`. The rest of the image is set to black. 
 
    """ 
 
    # defining a blank mask to start with 
 
    mask = np.zeros_like(img) 
 

 
    # defining a 3 channel or 1 channel color to fill the mask with depending on the input image 
 
    if len(img.shape) > 2: 
 
     channel_count = img.shape[2] # i.e. 3 or 4 depending on your image 
 
     ignore_mask_color = (255,) * channel_count 
 
    else: 
 
     ignore_mask_color = 255 
 

 
    # filling pixels inside the polygon defined by "vertices" with the fill color 
 
    cv2.fillPoly(mask, vertices, ignore_mask_color) 
 

 
    # returning the image only where mask pixels are nonzero 
 
    masked_image = cv2.bitwise_and(img, mask) 
 
    return masked_image 
 

 

 
def draw_lines(img, lines, color=[255, 0, 0], thickness=10): 
 
    """ 
 
    NOTE: this is the function you might want to use as a starting point once you want to 
 
    average/extrapolate the line segments you detect to map out the full 
 
    extent of the lane (going from the result shown in raw-lines-example.mp4 
 
    to that shown in P1_example.mp4). 
 
    Think about things like separating line segments by their 
 
    slope ((y2-y1)/(x2-x1)) to decide which segments are part of the left 
 
    line vs. the right line. Then, you can average the position of each of 
 
    the lines and extrapolate to the top and bottom of the lane. 
 
    This function draws `lines` with `color` and `thickness`.  
 
    Lines are drawn on the image inplace (mutates the image). 
 
    If you want to make the lines semi-transparent, think about combining 
 
    this function with the weighted_img() function below 
 
    """ 
 
    imshape = img.shape 
 
    left_x1 = [] 
 
    left_x2 = [] 
 
    right_x1 = [] 
 
    right_x2 = [] 
 
    y_min = img.shape[0] 
 
    y_max = int(img.shape[0] * 0.611) 
 
    for line in lines: 
 
     for x1, y1, x2, y2 in line: 
 
      if ((y2 - y1)/(x2 - x1)) < 0: 
 
       mc = np.polyfit([x1, x2], [y1, y2], 1) 
 
       left_x1.append(np.int(np.float((y_min - mc[1]))/np.float(mc[0]))) 
 
       left_x2.append(np.int(np.float((y_max - mc[1]))/np.float(mc[0]))) 
 
      # cv2.line(img, (xone, imshape[0]), (xtwo, 330), color, thickness) 
 
      elif ((y2 - y1)/(x2 - x1)) > 0: 
 
       mc = np.polyfit([x1, x2], [y1, y2], 1) 
 
       right_x1.append(np.int(np.float((y_min - mc[1]))/np.float(mc[0]))) 
 
       right_x2.append(np.int(np.float((y_max - mc[1]))/np.float(mc[0]))) 
 
       #   cv2.line(img, (xone, imshape[0]), (xtwo, 330), color, thickness) 
 
    l_avg_x1 = np.int(np.nanmean(left_x1)) 
 
    l_avg_x2 = np.int(np.nanmean(left_x2)) 
 
    r_avg_x1 = np.int(np.nanmean(right_x1)) 
 
    r_avg_x2 = np.int(np.nanmean(right_x2)) 
 
    #  print([l_avg_x1, l_avg_x2, r_avg_x1, r_avg_x2]) 
 
    cv2.line(img, (l_avg_x1, y_min), (l_avg_x2, y_max), color, thickness) 
 
    cv2.line(img, (r_avg_x1, y_min), (r_avg_x2, y_max), color, thickness) 
 

 

 
def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap): 
 
    """ 
 
    `img` should be the output of a Canny transform. 
 
    Returns an image with hough lines drawn. 
 
    """ 
 
    lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, 
 
          maxLineGap=max_line_gap) 
 
    line_img = np.zeros(img.shape, dtype=np.uint8) 
 
    draw_lines(line_img, lines) 
 
    return line_img 
 

 

 
def process_image(img): 
 
    img_test = grayscale(img) 
 
    img_test = gaussian_blur(img_test, 7) 
 
    img_test = canny(img_test, 50, 150) 
 
    imshape = img.shape 
 
    vertices = np.array([[(100, imshape[0]), (400, 330), (600, 330), (imshape[1], imshape[0])]], dtype=np.int32) 
 
    img_test = region_of_interest(img_test, vertices) 
 
    rho = 2 # distance resolution in pixels of the Hough grid 
 
    theta = np.pi/180 # angular resolution in radians of the Hough grid 
 
    threshold = 55 # minimum number of votes (intersections in Hough grid cell) 
 
    min_line_length = 40 # minimum number of pixels making up a line 
 
    max_line_gap = 100 # maximum gap in pixels between connectable line segments 
 
    line_image = np.copy(img) * 0 # creating a blank to draw lines on 
 
    img_test = hough_lines(img_test, rho, theta, threshold, min_line_length, max_line_gap) 
 
    return img_test 
 

 

 
img = cv2.imread("sy1.jpg") 
 
res = process_image(img) 
 
plt.imshow(res)

所產生的誤差:

/Users/ViditShah/anaconda/envs/py27/bin/python /Users/ViditShah/Downloads/untitled1/gist.py 
 
/Users/ViditShah/Downloads/untitled1/gist.py:85: RuntimeWarning: Mean of empty slice 
 
    r_avg_x1 = np.int(np.nanmean(right_x1)) 
 
Traceback (most recent call last): 
 
    File "/Users/ViditShah/Downloads/untitled1/gist.py", line 122, in <module> 
 
    res = process_image(img) 
 
    File "/Users/ViditShah/Downloads/untitled1/gist.py", line 117, in process_image 
 
    img_test = hough_lines(img_test, rho, theta, threshold, min_line_length, max_line_gap) 
 
    File "/Users/ViditShah/Downloads/untitled1/gist.py", line 100, in hough_lines 
 
    draw_lines(line_img, lines) 
 
    File "/Users/ViditShah/Downloads/untitled1/gist.py", line 85, in draw_lines 
 
    r_avg_x1 = np.int(np.nanmean(right_x1)) 
 
ValueError: cannot convert float NaN to integer 
 

 
Process finished with exit code 1

我使用python2.7

請指導我。 您誠摯的, Vidit Shah

回答

0

一種可能性是除以零時創建Nans當你計算梯度;嘗試過濾出x1 == x2。這種潛在的錯誤來源很少會出現。

最重要的問題是,門檻設置得太高,在霍夫變換(55)爲您的代碼的結構。如果Hough Lines階段沒有識別線條,那麼您將無法繪製線條。

您可以通過降低閾值(以及在工作時檢測到行檢測時丟失質量)或調整代碼中的其他內容來解決此問題,例如使用error handling或預處理圖像這樣總是會有霍夫步輸出的線。