Opencv错误:错误:(-215)(scn == 3 || scn == 4)

时间:2019-04-07 10:45:09

标签: opencv image-processing raspberry-pi

Raspberry Pi在cvtCOLOR中出现网络摄像头错误 错误 追溯(最近一次通话):   在第30行的文件“ test1.py”中     imgHSV = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) cv2.error:/home/pi/opencv-3.1.0/modules/imgproc/src/color.cpp:8141:error:(-215)(scn == 3 || scn == 4)&&(深度==函数cvtColor中的CV_8U || depth == CV_32F)

# -*- coding: utf-8 -*-
"""
Created on Sat Apr  6 09:53:56 2019

@author: lenovo
"""

import cv2
import numpy as np
import time
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)

lowerBound=np.array([0,143,173])
upperBound=np.array([255,255,255])
cam= cv2.VideoCapture(1)

kernelOpen=np.ones((5,5))
kernelClose=np.ones((20,20))

font = cv2.FONT_HERSHEY_SIMPLEX
while True:
    ret, img=cam.read()
#    img=cv2.resize(img,(340,220))

    #convert BGR to HSV
    imgHSV= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    # create the Mask
    mask=cv2.inRange(imgHSV,lowerBound,upperBound)
    #morphology
    maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen)
    maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose)

    maskFinal=maskClose
    _,conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

    cv2.drawContours(img,conts,-1,(255,0,0),3)
    cam.set(3, 160)

    cam.set(4, 120)

    for i in range(len(conts)):
        M = cv2.moments(conts[i])
        x,y,w,h=cv2.boundingRect(conts[i])
        if M["m00"] != 0:

            cX = int(M["m10"] / M["m00"])
            cY = int(M["m01"] / M["m00"])
        else:

    # set values as what you need in the situation
            cX, cY = 0, 0
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255), 2)
        cv2.circle(img, (cX, cY), 5, (255, 255, 255), -1)
        cv2.putText(img, str(i+1),(x,y+h),cv2.FONT_HERSHEY_SIMPLEX,1.0,(0,255,255))
        val = ser.readline()
        print(val)
        ser.write(val)
        print("X center: ",cX)
        print("Y center: ",cY)
        #time.sleep(2)
        print("-------------------------------------")
        print("W: ",w)
        print("H: ",h)


    cv2.imshow("maskClose",maskClose)
    cv2.imshow("maskOpen",maskOpen)
    cv2.imshow("mask",mask)
    cv2.imshow("cam",img)
    cv2.waitKey(10)

0 个答案:

没有答案