Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Izzatullokh24 opened this issue May 29, 2024 · 0 comments

Comments

@Izzatullokh24
Copy link

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant