python - 如何在不制作新的.jpg文件的情况下使用opencv从相机获取图像

标签 python opencv

我有这段代码可以使用开放的cv库检测激光点,并且可以将.jpg或.png文件作为增强文件输入,但是现在我想从相机中获取图像,因此可以正常工作。 “视频0”我正在使用Ubuntu 16.04,这是我的代码,我用******标记了问题
任何帮助将不胜感激:

# 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=False,
    help="path to the image file")
args = vars(ap.parse_args())

camera = cv2.VideoCapture(0)
#problem is here ********************************************
ret, image = camera.read()

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(0)

最佳答案

将带镜头的摄像头包裹在带有中断条件的While循环中可能会有所帮助:

  
import cv2

cap = cv2.VideoCapture(0)

while True:

    ret, frame = cap.read()
    cv2.imshow('frame', frame)

    # ADD LOGIC HERE 

    print(frame.shape)

    #  END 
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

关于python - 如何在不制作新的.jpg文件的情况下使用opencv从相机获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57297568/

相关文章:

python - 重新训练 MobileNet SSD V1 COCO 后,Tensorflow 的 pb 和 pbtxt 文件不适用于 OpenCV

image - 使用 python opencv 检测热视频中的眼动

c++ - OpenCV:没有 GPU 支持(该库是在没有 CUDA 支持的情况下编译的)

c# - KNN 计算的图像中的绘图匹配对和 Features2DToolbox.DrawMatches 中的潜在错误

Python:在比较期间强制 heapq.merge 将字符串解释为整数

python - 将 <Python.h> 包含到 makefile.am

python - 结合numpy多维数组

python - 什么 Python 正则表达式可以捕获文本中重叠的两个单词序列(包括缩写)?

python - 使用 Matplotlib 如何在最终图中突出显示一个点

python - 如何验证两个图像完全相同?