为什么我的颜色检测代码会导致OpenCV错误?

时间:2018-10-13 07:45:31

标签: python python-3.x opencv

我遇到以下错误:

  

cv2.error:opencv(3.4.3)   / home / pi / packaging / opencv-python / opencv / modules / highgui / src / cpp:356:   错误:(-215:声明失败)size.width> 0 && size.height> 0 in   功能'imshow'

使用以下代码:

from __future__ import division
import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera
import numpy as np
import time

def nothing(*arg):
        pass

FRAME_WIDTH = 320
FRAME_HEIGHT = 240

# Initial HSV GUI slider values to load on program start.
#icol = (36, 202, 59, 71, 255, 255)    # Green
#icol = (18, 0, 196, 36, 255, 255)  # Yellow
#icol = (89, 0, 0, 125, 255, 255)  # Blue
#icol = (0, 100, 80, 10, 255, 255)   # Red
#icol = (104, 117, 222, 121, 255, 255)   # test
icol = (0, 0, 0, 255, 255, 255)   # New start

cv2.namedWindow('colorTest')
# Lower range colour sliders.
cv2.createTrackbar('lowHue', 'colorTest', icol[0], 255, nothing)
cv2.createTrackbar('lowSat', 'colorTest', icol[1], 255, nothing)
cv2.createTrackbar('lowVal', 'colorTest', icol[2], 255, nothing)
# Higher range colour sliders.
cv2.createTrackbar('highHue', 'colorTest', icol[3], 255, nothing)
cv2.createTrackbar('highSat', 'colorTest', icol[4], 255, nothing)
cv2.createTrackbar('highVal', 'colorTest', icol[5], 255, nothing)

# Initialize webcam. Webcam 0 or webcam 1 or ...
vidCapture = cv2.VideoCapture(0)
vidCapture.set(cv2.CAP_PROP_FRAME_WIDTH,FRAME_WIDTH)
vidCapture.set(cv2.CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT)

while True:

    timeCheck = time.time()
    # Get HSV values from the GUI sliders.
    lowHue = cv2.getTrackbarPos('lowHue', 'colorTest')
    lowSat = cv2.getTrackbarPos('lowSat', 'colorTest')
    lowVal = cv2.getTrackbarPos('lowVal', 'colorTest')
    highHue = cv2.getTrackbarPos('highHue', 'colorTest')
    highSat = cv2.getTrackbarPos('highSat', 'colorTest')
    highVal = cv2.getTrackbarPos('highVal', 'colorTest')

    # Get webcam frame
    _, frame = vidCapture.read()

    # Show the original image.
    cv2.imshow('abcd', frame)

    # Convert the frame to HSV colour model.
    frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # HSV values to define a colour range we want to create a mask from.
    colorLow = np.array([lowHue,lowSat,lowVal])
    colorHigh = np.array([highHue,highSat,highVal])
    mask = cv2.inRange(frameHSV, colorLow, colorHigh)
    # Show the first mask
    cv2.imshow('mask-plain', mask)

    im2, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours]
    biggest_contour = max(contour_sizes, key=lambda x: x[0])[1]

    #cv2.drawContours(frame, biggest_contour, -1, (0,255,0), 3)

    x,y,w,h = cv2.boundingRect(biggest_contour)
    cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)

    #cv2.drawContours(frame, contours, -1, (0,255,0), 3)

    #cv2.drawContours(frame, contours, 3, (0,255,0), 3)

    #cnt = contours[1]
    #cv2.drawContours(frame, [cnt], 0, (0,255,0), 3)

    # Show final output image
    cv2.imshow('colorTest', frame)

    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break
    print('fps - ', 1/(time.time() - timeCheck))

cv2.destroyAllWindows()
vidCapture.release()

我已经从https://www.bluetin.io/opencv/object-detection-tracking-opencv-python/复制了代码。

我不知道为什么在错误消息中显示的路径上出现错误。我的Pi上甚至没有此路径。我在这里做什么错了?

0 个答案:

没有答案