これは私が使用しているコードです。車両が高速で移動しているときにこのエラーが発生します。この問題を解決するにはどうすればよいですか。線として検出された領域の黒いオブジェクトに線を表示したいのですが、できませんでした。あなたもこれを手伝ってもらえますか?
import cv2 import numpy as np def make_coordinates(image, line_parameters): slope, intercept = line_parameters y1 = image.shape[0] y2 = int(y1*(3/5)) x1 = int((y1 - intercept)/slope) x2 = int((y2 - intercept)/slope) return np.array([x1, y1, x2, y2]) def average_slope_intercept(image, lines): left_fit = [] right_fit = [] if lines is not None: for line in lines: x1, y1, x2, y2 = line.reshape(4) parameters = np.polyfit((x1, x2), (y1, y2), 1) slope = parameters[0] intercept = parameters[1] if slope < 0: left_fit.append((slope, intercept)) else: right_fit.append((slope, intercept)) left_fit_average = np.average(left_fit, axis=0) #right_fit_average = np.average(right_fit, axis=0) print(left_fit_average, 'left') #print(right_fit_average, 'right') left_line = make_coordinates(image, left_fit_average) #right_line = make_coordinates(image, right_fit_average) return np.array([left_line]) def canny(img): gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) kernel = 5 blur = cv2.GaussianBlur(gray, (kernel, kernel), 0) canny = cv2.Canny(blur, 50, 150) return canny def display_lines(img, lines): line_image = np.zeros_like(img) if lines is not None: for line in lines: for x1, y1, x2, y2 in line: cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 10) return line_image def region_of_interest(canny): height = canny.shape[0] width = canny.shape[1] mask = np.zeros_like(canny) square = np.array([[(635,478),(0,478),(281,200)] ]) cv2.fillPoly(mask, square, 255) masked_image = cv2.bitwise_and(canny, mask) return masked_image cap = cv2.VideoCapture(0) while True: _, frame = cap.read() canny_image = canny(frame) cropped_canny = region_of_interest(canny_image) lines = cv2.HoughLinesP(cropped_canny, 2, np.pi / 180, 100, np.array([]), minLineLength=40, maxLineGap=5) averaged_lines = average_slope_intercept(frame, lines) line_image = display_lines(frame, averaged_lines) combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1) cv2.imshow("result", combo_image) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
コードを実行すると、プログラムが表示するエラーは次のようになります。
/usr/lib/python3/dist-packages/numpy/lib/function_base.py:392: RuntimeWarning: Mean of empty slice. avg = a.mean(axis) /usr/lib/python3/dist-packages/numpy/core/_methods.py:85: RuntimeWarning: invalid value encountered in double_scalars ret = ret.dtype.type(ret / rcount) nan left Traceback (most recent call last): File "/home/pi/Desktop/code.py", line 71, in <module> averaged_lines = average_slope_intercept(rotated, lines) File "/home/pi/Desktop/code.py", line 48, in average_slope_intercept left_line = make_coordinates(image, left_fit_average) File "/home/pi/Desktop/code.py", line 55, in make_coordinates slope, intercept = line_parameters TypeError: cannot unpack non-iterable numpy.float64 object
私はopencvを初めて使用します。これを手伝っていただければ幸いです。
あなたの回答
tips
プレビュー