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'

Open
#989 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.4k
Forks
1k
Avg merge
22h 17m
Merged PRs (30d)
3

Description

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'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in find_lane_pixels at the out_img construction and the cv2.rectangle calls, using the printed shapes and the OpenCV assertion as the initial clues. Check the channel dimensions expected by the drawing calls and trace how binary_warped is passed into fit_polynomial_first_lane. Done means the lane-window drawing runs without the assertion and the subsequent polynomial-fitting flow can be reached.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-vision
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.