Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python OpenCV将实时视频流中检测到的二维码保存为图像_Python_Opencv_Machine Learning_Deep Learning_Computer Vision - Fatal编程技术网

使用Python OpenCV将实时视频流中检测到的二维码保存为图像

使用Python OpenCV将实时视频流中检测到的二维码保存为图像,python,opencv,machine-learning,deep-learning,computer-vision,Python,Opencv,Machine Learning,Deep Learning,Computer Vision,我正在使用Python(3.7)和OpenCV 2进行一个项目,其中我必须检测二维码并将其保存为图像,我已经成功地完成了检测部分,但不知道如何将二维码保存为图像 以下是我迄今为止所尝试的: 代码的检测部分: while True: frame = vs.read() frame = imutils.resize(frame, width=400) barcodes = pyzbar.decode(frame) for barcode in barcodes:

我正在使用Python(3.7)和OpenCV 2进行一个项目,其中我必须检测二维码并将其保存为图像,我已经成功地完成了检测部分,但不知道如何将二维码保存为图像

以下是我迄今为止所尝试的:

代码的检测部分:

while True:
    frame = vs.read()
    frame = imutils.resize(frame, width=400)
    barcodes = pyzbar.decode(frame)

    for barcode in barcodes:
        (x, y, w, h) = barcode.rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        barcodeData = barcode.data.decode("utf-8")
        barcodeType = barcode.type
        text = "{}".format(barcodeData)
        cv2.putText(frame, '', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

        if barcodeData not in found:
            csv.write("{}\n".format(barcodeData))
            csv.flush()

            found.clear()
            found.add(barcodeData)

    # Título do Frame
    cv2.imshow("Live Stream Window", frame)
    key = cv2.waitKey(1) & 0xFF

    if key == ord("q"):
        break
如何将检测到的区域(二维码)保存为图像

更新:下面是自动更新到svae图像的代码,但这不起作用


假设
(x,y,w,h)=barcode.rect
返回与
x,y,w,h=cv2.boundingRect(轮廓)
相同的值,下面是一个从图像裁剪ROI的可视化

-------------------------------------------
|                                         | 
|    (x1, y1)                             |
|      ------------------------           |
|      |                      |           |
|      |                      |           | 
|      |         ROI          |           |  
|      |                      |           |   
|      |                      |           |   
|      |                      |           |       
|      ------------------------           |   
|                           (x2, y2)      |    
|                                         |             
|                                         |             
|                                         |             
-------------------------------------------
(0,0)
视为图像的左上角,从左到右为x方向,从上到下为y方向。如果我们将
(x1,y1)
作为ROI的左上角,将
(x2,y2)
作为ROI的右下角顶点,我们可以使用Numpy切片来裁剪图像:

ROI = image[y1:y2, x1:x2]
但通常我们没有右下角的顶点。在典型情况下,我们将迭代通过轮廓,在那里可以找到矩形ROI坐标。此外,如果我们想保存多个ROI,我们可以保留一个计数器

cnts = cv2.findContours(grayscale_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

ROI_number = 0
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    ROI = image[y:y+h, x:x+w]
    cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
    ROI_number += 1

回到你的问题上来,我们可以这样做。注意:我们复制了一个框架
original=frame.copy()
,因为一旦我们使用
cv2.rectangle
在图像上绘制,它就会绘制到框架上。当我们裁剪它时,我们不想要这个画框,所以我们从画框的副本中裁剪

while True:
    frame = vs.read()
    frame = imutils.resize(frame, width=400)
    original = frame.copy()
    barcodes = pyzbar.decode(frame)
    barcode_num = 0

    for barcode in barcodes:
        (x, y, w, h) = barcode.rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        ROI = original[y:y+h, x:x+w]
        cv2.imwrite('barcode_{}.png'.format(barcode_num), ROI)
        barcode_num += 1
        barcodeData = barcode.data.decode("utf-8")
        barcodeType = barcode.type
        text = "{}".format(barcodeData)
        cv2.putText(frame, '', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

        if barcodeData not in found:
            csv.write("{}\n".format(barcodeData))
            csv.flush()

            found.clear()
            found.add(barcodeData)

    # Título do Frame
    cv2.imshow("Live Stream Window", frame)
    key = cv2.waitKey(1) & 0xFF

    if key == ord("q"):
        break

这回答了你的问题吗?我已经尝试过你的代码,当我按下“q”键时它正在捕获图像,这意味着退出程序,我们如何在不按下“q”键的情况下自动捕获实时流中的图像,以及如何提高捕获图像的质量?如果按下其他键手动保存帧,你可以添加另一项检查。如果要自动捕获图像,请创建计时器以定期保存帧。如果不知道您的设置,也不让sample imagescan定期更新代码以自动捕获图像,我无法确切地说明如何提高图像质量?我尝试添加了一个新的waitKey,但它不起作用。您可以在问题中看到更新的代码。如果您想自动捕获图像,请取出way键并保存每个帧的图像
while True:
    frame = vs.read()
    frame = imutils.resize(frame, width=400)
    original = frame.copy()
    barcodes = pyzbar.decode(frame)
    barcode_num = 0

    for barcode in barcodes:
        (x, y, w, h) = barcode.rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        ROI = original[y:y+h, x:x+w]
        cv2.imwrite('barcode_{}.png'.format(barcode_num), ROI)
        barcode_num += 1
        barcodeData = barcode.data.decode("utf-8")
        barcodeType = barcode.type
        text = "{}".format(barcodeData)
        cv2.putText(frame, '', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

        if barcodeData not in found:
            csv.write("{}\n".format(barcodeData))
            csv.flush()

            found.clear()
            found.add(barcodeData)

    # Título do Frame
    cv2.imshow("Live Stream Window", frame)
    key = cv2.waitKey(1) & 0xFF

    if key == ord("q"):
        break