opencv / opencv/opencv-python

error: OpenCV(4.8.0) /io/opencv/modules/core/src/copy.cpp:71: error: (-215:Assertion failed) cn <= 4 in function 'scalarToRawData'

Offen
#989 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Python
Sterne
5.4k
Forks
1k
Ø Merge
22 Std. 17 Min.
Gemergte PRs (30 T.)
3

Beschreibung

here is my code that gives me an error.

def find_lane_pixels(binary_warped):
# Debug: Check the shape and type of binary_warped
print(f"binary_warped shape: {binary_warped.shape}, type: {binary_warped.dtype}")

histogram = np.sum(binary_warped[binary_warped.shape[0]//2:, 50:], axis=0)
out_img = np.dstack((binary_warped, binary_warped, binary_warped))

# Debug: Check the shape and type of out_img
print(f"out_img shape: {out_img.shape}, type: {out_img.dtype}")

midpoint = histogram.shape[0] // 2
leftx_base = np.argmax(histogram[:midpoint])
rightx_base = np.argmax(histogram[midpoint:]) + midpoint

nwindows = 9
margin = 100
minpix = 50
window_height = int(binary_warped.shape[0] // nwindows)

nonzero = binary_warped.nonzero()
nonzeroy = np.array(nonzero[0])
nonzerox = np.array(nonzero[1])
leftx_current = leftx_base
rightx_current = rightx_base

left_lane_inds = []
right_lane_inds = []

for window in range(nwindows):
    win_y_low = binary_warped.shape[0] - (window+1) * window_height
    win_y_high = binary_warped.shape[0] - window * window_height
    win_xleft_low = leftx_current - margin  
    win_xleft_high = leftx_current + margin 
    win_xright_low = rightx_current - margin
    win_xright_high = rightx_current + margin
    
    # Ensure that window boundaries are within the image dimensions
    win_xleft_low = max(0, win_xleft_low)
    win_xleft_high = min(binary_warped.shape[1], win_xleft_high)
    win_xright_low = max(0, win_xright_low)
    win_xright_high = min(binary_warped.shape[1], win_xright_high)

    cv2.rectangle(out_img, (win_xleft_low, win_y_low), (win_xleft_high, win_y_high), (0, 255, 0), 2)
    cv2.rectangle(out_img, (win_xright_low, win_y_low), (win_xright_high, win_y_high), (0, 255, 0), 2)

    good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & 
                      (nonzerox >= win_xleft_low) & (nonzerox < win_xleft_high)).nonzero()[0]
    good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & 
                       (nonzerox >= win_xright_low) & (nonzerox < win_xright_high)).nonzero()[0]

    left_lane_inds.append(good_left_inds)
    right_lane_inds.append(good_right_inds)

    if len(good_left_inds) > minpix:
        leftx_current = int(np.mean(nonzerox[good_left_inds]))
    if len(good_right_inds) > minpix:        
        rightx_current = int(np.mean(nonzerox[good_right_inds]))

try:
    left_lane_inds = np.concatenate(left_lane_inds)
    right_lane_inds = np.concatenate(right_lane_inds)
except ValueError:
    print("An error occurred!")
    pass

leftx = nonzerox[left_lane_inds]
lefty = nonzeroy[left_lane_inds] 
rightx = nonzerox[right_lane_inds]
righty = nonzeroy[right_lane_inds]

return leftx, lefty, rightx, righty, out_img

def fit_polynomial_first_lane(binary_warped):
leftx, lefty, rightx, righty, out_img = find_lane_pixels(binary_warped)
left_fit = np.polyfit(lefty, leftx, 2)
right_fit = np.polyfit(righty, rightx, 2)
ploty = np.linspace(0, binary_warped.shape[0]-1, binary_warped.shape[0])
try:
left_fitx = left_fit[0] * ploty2 + left_fit[1] * ploty + left_fit[2]
right_fitx = right_fit[0] * ploty
2 + right_fit[1] * ploty + right_fit[2]
except TypeError:
print('The function failed to fit a line!')
left_fitx = 1 * ploty2 + 1 * ploty
right_fitx = 1 * ploty
2 + 1 * ploty

out_img[lefty, leftx] = [255, 0, 0]
out_img[righty, rightx] = [0, 0, 255]

left_pts = np.transpose(np.vstack((left_fitx, ploty))).astype(np.int32)
right_pts = np.transpose(np.vstack((right_fitx, ploty))).astype(np.int32)

cv2.polylines(out_img, np.int32([left_pts]), False, (255, 255, 0), thickness=5)
cv2.polylines(out_img, np.int32([right_pts]), False, (255, 255, 0), thickness=5)

return left_fit, right_fit, out_img

binary_warped pixel range: 0 to 255
binary_warped shape: (720, 1280, 3), type: uint8
out_img shape: (720, 1280, 9), type: uint8

error Traceback (most recent call last)
in <cell line: 129>()
146
147 # Find lane pixels and fit polynomial
--> 148 left_fit, right_fit, out_img = fit_polynomial_first_lane(binary_warped)
149
150 # Plot the results

1 frames
in find_lane_pixels(binary_warped)
66 win_xright_high = min(binary_warped.shape[1], win_xright_high)
67
---> 68 cv2.rectangle(out_img, (win_xleft_low, win_y_low), (win_xleft_high, win_y_high), (0, 255, 0), 2)
69 cv2.rectangle(out_img, (win_xright_low, win_y_low), (win_xright_high, win_y_high), (0, 255, 0), 2)
70

error: OpenCV(4.8.0) /io/opencv/modules/core/src/copy.cpp:71: error: (-215:Assertion failed) cn <= 4 in function 'scalarToRawData'

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in find_lane_pixels bei der Erstellung von out_img und den cv2.rectangle-Aufrufen und verwende die ausgegebenen Shapes sowie die OpenCV-Assertion als erste Hinweise. Prüfe die von den Zeichenaufrufen erwarteten Kanal-Dimensionen und verfolge, wie binary_warped an fit_polynomial_first_lane übergeben wird. Fertig ist die Untersuchung, wenn das Zeichnen der Lane-Fenster ohne die Assertion ausgeführt wird und der nachfolgende Ablauf zur Polynom-Anpassung erreicht werden kann.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
computer-vision
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.