Ask Your Question
1

No idea why the script gets this error code

asked 2018-04-05 10:48:33 -0600

eri gravatar image

updated 2019-10-09 19:44:57 -0600

supra56 gravatar image

Hello everybody,

I've written a script for detection of edges, but the problem is that my script get an error which i have added in the following. Whenever i want to run the script the shell appears for 1-2 seconds and after that it closes itself. I have no idea what it could be, i hope someone have an idea which would help me. The script is this here and the error code ist also in the following :

Script :

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(1):

    _, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lower_red = np.array([30,150,50])
    upper_red = np.array([255,255,180])

    mask = cv2.inRange(hsv, lower_red, upper_red)
    res = cv2.bitwise_and(frame,frame, mask= mask)

    cv2.imshow('Original',frame)
    edges = cv2.Canny(frame,100,200)
    cv2.imshow('Edges',edges)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()
cap.release()

The Error Code:

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
cv2.error: C:\build\2_4_winpack-bindings-win64-vc14-static\opencv\modules\imgproc\src\color.cpp:3961: error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cv::cvtColor
edit retag flag offensive close merge delete

Comments

Check if the frame being read is 8bit rgb. OpenCV is complaining that it's not.
To check the depth do frame.dtype and to check number of channels do frame.shape.

nicolasabril gravatar imagenicolasabril ( 2019-10-10 08:28:45 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2019-10-10 08:32:30 -0600

Jorge Diaz gravatar image

I sounds like your camera is feeding images that are in a different format of what you are expecting or that the camera is not accessible. Make sure that you are pathing to device. It might not hurt to check the type of image either.

edit flag offensive delete link more
-2

answered 2019-10-10 08:12:14 -0600

supra56 gravatar image

There is nothing wrong with your code. I will fix for you to get better result for Canny. Because I'm using 4K UHD monitor. You can select range between 100 to 250 or slightly lower or higher for 1080P. The values for red is right colour. But depending on various red colour.

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    _, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lower_red = np.array([0, 0, 220])
    upper_red = np.array([180, 255, 255])

    mask = cv2.inRange(hsv, lower_red, upper_red)
    res = cv2.bitwise_and(frame, frame, mask= mask)

    edges = cv2.Canny(frame, 125, 200)
    cv2.imshow('Edges', edges) 

    cv2.imshow('Original', frame)
    cv2.imshow('Red', res) 

    k = cv2.waitKey(5)
    if k == 27:
        break

cv2.destroyAllWindows()
cap.release()
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-04-05 10:48:33 -0600

Seen: 457 times

Last updated: Oct 10 '19