OpenCV:错误(-215)深度== CV_8U ||深度== CV_16U ||函数cv ::: cvtColor中的depth == CV_32F

时间:2018-03-20 17:00:23

标签: python cv2

做一个动作捕捉程序,遇到了这个漂亮的错误。似乎深度存在问题,但是代码中没有任何地方存在问题。

    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

我研究了这个错误,但没有人有实时视频输入的解决方案。请帮助,这很快就会到期。将附上以下所有代码。

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

1 个答案:

答案 0 :(得分:0)

错误消息表明cv2.cvtColor期望图像的(颜色)深度为816位无符号整数(8U, 16U)  或32位浮动(32F)

image需要作为cv2.cvtColor例子进行投放或创建,例如uint8CV_8U是数据类型uint8

的别名

需要进行转换。试试:

import numpy as np

image1copy = np.uint8(image)

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

顺便说一句:

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

<强> bitwise_or

计算两个数组或数组和标量的每元素逐位析取。

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

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