为什么我的 Python OpenCV 视频输出为灰度,即使我有颜色叠加?

问题描述 投票:0回答:0

背景:我的目标是学习 OpenCV 并练习条码检测/解码,使用 2 个摄像头或摄像头和条码扫描仪制作条码匹配系统。

我有一个正在修补的旧工业相机(IDS Ueye UI221)。 IDS 提供了一个基于 python 的示例应用程序,用于执行基本的相机连接(内存、色彩空间检测等)。摄像头画面良好清晰,我的条形码可以正确读取和解码。为了便于阅读,我得到了解码数据的叠加层。灿烂!但是,我正在尝试用颜色渲染叠加层(增加可读性),但它总是显示为白色。

White bounding box (cv2.rectangle()) and 'decode' placeholder text displaying putText().

我希望 putText() 覆盖层现在可以是白色以外的任何颜色,但可能会使用 (255, 0 , 0) RGB——但即使 openCV 想让它成为 BGR,我也会得到蓝色文本和盒子,我不在乎!

我知道我可能遗漏了一些基本的东西,但在阅读了很多之后,包括: 用openCV检测色彩空间如何在 OpenCV (Python) 中将灰度图像转换为 RGB?

我试过放

cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
在我覆盖颜色 putText() 东西之前,我想也许我不能在灰度上覆盖颜色?嗯,我期待从我收到的任何回复中学习。非常感谢!

from pyueye import ueye
import numpy as np
import cv2
from pyzbar.pyzbar import decode
import sys`

# identify camera color space -- programmed by the camera manufacturer


if:
   pass

elif int.from_bytes(sInfo.nColorMode.value, byteorder='big') == ueye.IS_COLORMODE_MONOCHROME:
    # for color camera models use RGB32 mode
    m_nColorMode = ueye.IS_CM_MONO8
    nBitsPerPixel = ueye.INT(8)
    bytes_per_pixel = int(nBitsPerPixel / 8)
    print("IS_COLORMODE_MONOCHROME: ", )
    print("\tm_nColorMode: \t\t", m_nColorMode)
    print("\tnBitsPerPixel: \t\t", nBitsPerPixel)
    print("\tbytes_per_pixel: \t\t", bytes_per_pixel)
    print()

# Continuous image display
while nRet == ueye.IS_SUCCESS:

    # In order to display the image in an OpenCV window we need to extract the data of our image memory
    array = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)

    # bytes_per_pixel = int(nBitsPerPixel / 8)

    # ...reshape it in an numpy array...
    frame = np.reshape(array, (height.value, width.value, bytes_per_pixel))

    # ...resize the image by a half
    # frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)

    # ...double the image size for easier readability
    frame = cv2.resize(frame, (0, 0), fx=2, fy=2)
    
# HERE WAS MY MISTAKE :( I needed to assign the change to color not just try # to call the function
    # cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)

    **frame =** cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)


# ---------------------------------------
    # Include image data processing here

    processed_image = decode(frame)
    print(processed_image)  # just for troubleshooting aiming the camera and stuff

    # adding bounding box around the scanned barcode and human-readable content
    for barcode in processed_image:
        (x, y, w, h) = barcode.rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 5)

        # font
        font = cv2.FONT_HERSHEY_PLAIN

        # org
        org = (x, y+h+30)

        # fontScale
        fontScale = 3

        # Blue color in BGR
        color = (255, 0, 0)

        # Line thickness of 2 px
        thickness = 2

        # Using cv2.putText() method
        cv2.putText(frame, 'Decode', org, font, fontScale, color, thickness, cv2.LINE_AA)

这是我从控制台输出得到的:

开始

IS_COLORMODE_MONOCHROME: m_nColorMode:6,nBitsPerPixel:8,bytes_per_pixel:1

相机型号:UI221xSE-M, 相机序列号:4002909292, 最大图像宽度:640, 最大图像高度:480

[] [解码(data=b'https:/testtesttest', type='QRCODE', rect=Rect(left=887, top=109, width=328, height=312), polygon=[Point(x=887, y =421), Point(x=1215, y=418), Point(x=1206, y=109), Point(x=888, y=115)], quality=1, orientation='UP'), 已解码(data=b'https://testtesttest', type='QRCODE', rect=Rect(left=248, top=114, width=341, height=320), polygon=[Point(x=248, y= 433), Point(x=579, y=434), Point(x=589, y=114), Point(x=270, y=122)], quality=1, orientation='UP')]

视频输出显示在顶部的第一个屏幕截图中。

python opencv overlay barcode
© www.soinside.com 2019 - 2024. All rights reserved.