1

我目前正在开发一个使用 Raspberry Pi 及其相机模块 v2.1 的项目。

我需要使用我正在使用 OpenCV 和 pyzbar 库的相机扫描一些条形码。

我在 OpenCV 返回的图像上遇到了一些问题,示例如下:

从运行返回的图像libcamera-hellolibcamera 图像

运行我的脚本返回的图像: 脚本图像

如您所见,图像非常不同,OpenCV 图像放大了更多。

我试过调整图像大小,甚至改变框架的大小,但似乎没有帮助,图像只是被拉伸了!

有没有人知道为什么会发生这种情况?

我用于捕获图像的脚本如下:

import cv2
from pyzbar import pyzbar
from gpiozero import Button

from readBarcodeData import read_text

button = Button(25)

def read_barcodes(frame):
    barcodes = pyzbar.decode(frame)
    for barcode in barcodes:
        x, y , w, h = barcode.rect
        barcode_info = barcode.data.decode('utf-8')
        cv2.rectangle(frame, (x, y),(x+w, y+h), (0, 255, 0), 2)

        with open("barcode_result.txt", mode ='w') as file:
            file.write(str(barcode_info).replace("'", ""))
        

    return frame


def main():
    while True:
        if button.is_pressed:
            camera = cv2.VideoCapture(0)
            ret, frame = camera.read()
            
            while ret:
                ret, frame = camera.read()
                frame = read_barcodes(frame)
                cv2.imshow('Barcode Scanner', frame)
                if cv2.waitKey(0) & 0xFF == 27:
                    break
                break
                
            camera.release()
            cv2.destroyAllWindows()
            read_text()
        

if __name__ == '__main__':
    main()


编辑: 我还尝试使用以下代码捕获图像:

import cv2

vid = cv2.VideoCapture(0)
  
while(True):
      
    # Capture the video frame
    ret, frame = vid.read()
  
    # Display the resulting frame
    cv2.imshow('frame', frame)
      
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
  
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()

但我仍然得到一个裁剪的图像。

编辑 2:从视频捕获返回的属性:

CV_CAP_PROP_FRAME_WIDTH: '640.0'
CV_CAP_PROP_FRAME_HEIGHT : '480.0'
[ WARN:0] global /tmp/pip-wheel-j62hpwu1/opencv-python_19cf39855c924932a2df50dd2b502cd2/opencv/modules/videoio/src/cap_v4l.cpp (1911) getProperty VIDEOIO(V4L2:/dev/video0): Unable to get camera FPS
CAP_PROP_FPS : '-1.0'
CAP_PROP_POS_MSEC : '911170.05'
CAP_PROP_FRAME_COUNT  : '-1.0'
CAP_PROP_BRIGHTNESS : '-1.0'
CAP_PROP_CONTRAST : '-1.0'
CAP_PROP_SATURATION : '-1.0'
CAP_PROP_HUE : '-1.0'
CAP_PROP_GAIN  : '-1.0'
CAP_PROP_CONVERT_RGB : '1.0'
4

1 回答 1

0

我设法通过使用 PiCamera 库捕获图像然后通过 cv2 运行它来“修复”这个问题:

读取 PiCamera 图像的代码如下:

import cv2
from time import sleep
from pyzbar import pyzbar
from gpiozero import Button
from picamera.array import PiRGBArray
import picamera

from readBarcodeData import read_text

button = Button(25)



def read_barcodes(frame):
    barcodes = pyzbar.decode(frame)
    for barcode in barcodes:
        x, y , w, h = barcode.rect
        barcode_info = barcode.data.decode('utf-8')
        cv2.rectangle(frame, (x, y),(x+w, y+h), (0, 255, 0), 2)

        with open("barcode_result.txt", mode ='w') as file:
            file.write(str(barcode_info).replace("'", ""))
        

    return frame


def main():
    while True:
        if button.is_pressed:
            with picamera.PiCamera() as camera:
                rawCapture = PiRGBArray(camera)
                #camera.resolution = (3280, 2464)
                camera.start_preview()
                #sleep(1)
                camera.capture(rawCapture, format="bgr")
                img = rawCapture.array
            #camera = cv2.VideoCapture(0)
            
            #ret, frame = camera.read()
            ret = True
            
            while ret:
                #ret, frame = img
                frame = read_barcodes(img)
                #cv2.imshow('Barcode Scanner', frame)
                #print(frame.shape)
                #if cv2.waitKey(0) & 0xFF == 27:
                    #break
                break
                
            cv2.destroyAllWindows()
            read_text()
        

if __name__ == '__main__':
    main()


PiCamera 捕获的图像似乎返回了没有裁剪的完整图像,因此很有效。

于 2022-01-07T09:37:38.210 回答