简体   繁体   中英

OpenCV: Error (-215) depth == CV_8U || depth == CV_16U|| depth == CV_32F in function cv:::cvtColor

Doing a motion capture program, ran into this beautiful error. It appears that there is a problem with the depth, however nowhere in the code should this be a problem.

    in get_x_centroid
    grayscale_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    error: C:\projects\opencv-
    python\opencv\modules\imgproc\src\color.cpp:10600: error: (-215) depth 
    == CV_8U || depth == CV_16U || depth == CV_32F in function cv::cvtColor

I researched this error but no one had a solution for live video feed. Please help, this is due soon. Will attach all of the code below.

import time
import cv2
import numpy as np

cap =cv2.VideoCapture(0)

def on_mouse(event, x, y, flags, params):
    if event == cv2.EVENT_LBUTTONDOWN:
       # print "Left mouse button clicked at: x =", x, ",y=", y
        hsv = hsv_img[y,x]
        #print "hue:", hsv_img[0],"saturation:", hsv[1],"value:",hsv[2]

cv2.namedWindow("Orginal")
cv2.namedWindow("Red")
cv2.namedWindow("Blue")
cv2.namedWindow("Green")

cv2.setMouseCallback("Original", on_mouse, param = None)
#Find the actual numbers,, captureimage3 has the clicky
lower_b = [163,180,183]
upper_b = [129,132,255]
lower_g = [109,93,100]
upper_g = [95,98,255]
lower_r = [97,10,24]
upper_r = [180,140,255]
lower_R = [0,70,195]
upper_R = [20,140,255]
lower_y = [122,113,116]
upper_y = [170,65, 255]

lower_b = np.array(lower_b,dtype = "uint8")
upper_b = np.array(upper_b,dtype = "uint8")
lower_g = np.array(lower_g,dtype = "uint8")
upper_g = np.array(upper_g,dtype = "uint8")
lower_r = np.array(lower_r,dtype = "uint8")
upper_r = np.array(upper_r,dtype = "uint8")
lower_R = np.array(lower_R,dtype = "uint8")
upper_R = np.array(upper_R,dtype = "uint8")
lower_y = np.array(lower_y,dtype = "uint8")
upper_y = np.array(upper_y,dtype = "uint8")

kernel = np.ones((0,0), np.uint8)

def show_filtered_image(window_name,lower,upper):
    new_mask = cv2.inRange(hsv_img,lower,upper)
    filtered_image = cv2.bitwise_or(frame, frame, mask = new_mask)
    eroded_image = cv2.erode(filtered_image, kernel, iterations=1)
    dilated_image = cv2.dilate(eroded_image, kernel, iterations=1)
    cv2.imshow(window_name, dilated_image)

def get_x_centroid(image):
    grayscale_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    moment_values = cv2.moments(grayscale_image)

    if moment_values["m00"] !=0:
        x_centroid = moment_values ["m10"]/moment_values["m00"]
    else:
        x_centroid = 1
    return x_centroid

def get_y_centroid(image):
    grayscale_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    moment_values = cv2.moments(grayscale_image)

    if moment_values["m00"] !=0:
        y_centroid = moment_values ["m01"]/moment_values["m00"]
    else:
        y_centroid = 1
    return y_centroid

k = cv2.waitKey(5) & 0xFF
while (k != 27):
    ret, frame = cap.read()
    hsv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    cv2.imshow("Original", frame)

    show_filtered_image("Blue", lower_b, upper_b)
    show_filtered_image("Green", lower_g, upper_g)
    show_filtered_image("Yellow", lower_y, upper_y)

    blue_mask = cv2.inRange(hsv_img, lower_b, upper_b)
    filtered_b = cv2.bitwise_or(frame, frame, mask = blue_mask)
    green_mask = cv2.inRange(hsv_img, lower_g, upper_g)
    filtered_g = cv2.bitwise_or(frame, frame, mask = green_mask)
    yellow_mask = cv2.inRange(hsv_img, lower_y, upper_y)
    filtered_y = cv2.bitwise_or(frame, frame, mask = yellow_mask)

    #filtered_combo_red = cv2.bitwise_or(filtered_r, filtered_R)
    #cv2.imshow("Red", filtered_combo_red)

    #red_x = get_x_centroid(filtered_combo_red)
    #red_y = get_y_centroid(filtered_combo_red)
    blue_img = get_x_centroid(filtered_b)
    blue_img = get_y_centroid(filtered_b)
    green_img = get_x_centroid(filtered_g)
    green_img = get_y_centroid(filtered_g)
    yellow_img = get_x_centroid(filtered_y)
    yellow_img = get_y_centroid(filtered_y)
    #print  "Red X=", red_x," Red Y=", red_y

    #red_x = int(get_x_centroid(filtered_combo_red))
    #red_y = int(get_y_centroid(filtered_combo_red))
    blue_x = int(get_x_centroid(blue_img))
    blue_y = int(get_y_centroid(blue_img))
    green_x = int(get_x_centroid(green_img))
    green_y = int(get_y_centroid(green_img))
    yellow_x = int(get_x_centroid(yellow_img))
    yellow_y = int(get_y_centroid(yellow_img))

    cv2.line(frame,(red_x, red_y),(green_x, green_y), (255,255,255), 3)
    cv2.line(frame,(blue_x, blue_y),(yellow_x, yellow_y), (255,255,255), 3)    


    key_pressed = cv2.waitKey(30)
    if key_pressed == 27:
        break

The error message implies that cv2.cvtColor expects an image with a (color) depth of 8 or 16 bit unsigned int (8U, 16U) or 32 bit float (32F) .

image needs to be casted or created as cv2.cvtColor expexts, for example uint8 . CV_8U is an alias for the data type uint8

A conversion is needed. Try:

import numpy as np

image1copy = np.uint8(image)

grayscale_image  = cv2.cvtColor(image1copy, cv2.COLOR_HSV2BGR)

BTW:

OpenCV 2.4.13.6 documentation » OpenCV API Reference » core. The Core Functionality »

bitwise_or

Calculates the per-element bit-wise disjunction of two arrays or an array and a scalar.

C++: void bitwise_or(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())

and

Python: cv2.bitwise_or(src1, src2[, dst[, mask]]) → dst

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM