首页 > 解决方案 > 程序在 while 循环的一帧后关闭

问题描述

我有这个程序,它应该检测光源并使用 python 的开放 cv 库将它们圈起来。该程序适用于正在捕获的相机的第一帧,然后当 while 循环尝试捕获第二帧时,终端给了我这个错误:

   VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
    Unable to stop the stream: Device or resource busy
    OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/imgproc/src/color.cpp, line 11111
    Traceback (most recent call last):
      File "lazer.py", line 27, in <module>
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    cv2.error: /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/imgproc/src/color.cpp:11111: error: (-215) scn == 3 || scn == 4 in function cvtColor

这似乎是一个简单的修复,但我是打开 cv 的新手,以前从未使用过它。我想我必须以某种方式在帧之间停止相机,因为错误表明设备或资源正忙。此外,我想明确指出,这不是我之前的问题的重复,我之前遇到了不同的问题,因为之前我没有得到任何图像,现在我得到了一些东西。任何帮助将不胜感激这里是我的代码:

# import the necessary packages
from imutils import contours
from skimage import measure
import numpy as np
import argparse
import imutils
import cv2


    # construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="path to the image file")
args = vars(ap.parse_args())
while(1):
    camera = cv2.VideoCapture(0)
    #problem is here ********************************************
    ret, image = camera.read()
    #image.shape
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (11, 11), 0)
    #threshold the image to reveal light regions in the
    # blurred image
    thresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)[1]
    # perform a series of erosions and dilations to remove
    # any small blobs of noise from the thresholded image
    thresh = cv2.erode(thresh, None, iterations=2)
    thresh = cv2.dilate(thresh, None, iterations=4)
    # perform a connected component analysis on the thresholded
    # image, then initialize a mask to store only the "large"
    # components
    labels = measure.label(thresh, neighbors=8, background=0)
    mask = np.zeros(thresh.shape, dtype="uint8")

    # loop over the unique components
    for label in np.unique(labels):
        # if this is the background label, ignore it
        if label == 0:
            continue

        # otherwise, construct the label mask and count the
            # number of pixels 
        labelMask = np.zeros(thresh.shape, dtype="uint8")
        labelMask[labels == label] = 255
        numPixels = cv2.countNonZero(labelMask)

            # if the number of pixels in the component is sufficiently
            # large, then add it to our mask of "large blobs"
        if numPixels > 300:
            mask = cv2.add(mask, labelMask)
    # find the contours in the mask, then sort them from left to
    # right
    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = contours.sort_contours(cnts)[0]

        # loop over the contours
    for (i, c) in enumerate(cnts):
            # draw the bright spot on the image
        (x, y, w, h) = cv2.boundingRect(c)
        ((cX, cY), radius) = cv2.minEnclosingCircle(c)
        #x and y center are cX and cY
        cv2.circle(image, (int(cX), int(cY)), int(radius),
            (0, 0, 255), 3)
        cv2.putText(image, "#{}".format(i + 1), (x, y - 15),
            cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)

        # show the output image
    cv2.imshow("Image", image)
    #cv2.waitKey(1000)
    if cv2.waitKey(1) == 27:
        break

标签: pythonopencv

解决方案


我发现我需要在程序结束时使用 camera.release()


推荐阅读