【问题标题】:Why does my image taken with OpenCV appear smaller than that of libcamera?为什么我用 OpenCV 拍摄的图像看起来比 libcamera 小?
【发布时间】:2022-01-24 17:15:01
【问题描述】:

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

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

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

运行libcamera-hello返回的图像:

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

正如您所看到的图像非常不同,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'

【问题讨论】:

  • 可能您正在使用 line frame = read_barcodes(frame) 裁剪图像,您可以尝试使用barcodeImage = read_barcodes(frame) 代替吗?
  • @Micka 谢谢,但这似乎不起作用。我尝试更改您建议的行,并更改cv2.imshow('Barcode Scanner', frame) 以包含barcodeImage,但它输出相同的作物
  • 当然,因为可能 read_barcodes(frame) 会返回作物...只是显示帧而不是?!!
  • @Micka 不幸的是,这也会返回裁剪/放大的图像。我测试了没有条形码读取代码的常规图像捕获,它仍然输出裁剪图像,我真的不知道为什么!
  • @Micka 我开始使用 VideoCapture,这就是我所学的,不过我现在开始使用 PiCamera 并且已经做到了拍摄图像(全尺寸,没有裁剪!)。现在尝试让它读取条形码,如果我管理它会回发!

标签: python opencv raspberry-pi picamera


【解决方案1】:

我设法通过使用 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 捕获的图像似乎返回了没有裁剪的完整图像,所以很享受。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多