OpenCV -cv2.putText中的文本在带有cv2.imshow的numpy ndarray中 Flink

iyfamqjs  于 7个月前  发布在  Flink
关注(0)|答案(2)|浏览(94)

在转换我使用我使用cv2.imshow来显示每一帧,并添加tqdm来显示转换的进度。我的问题是文本在窗口中 Flink 。我以前玩过cv2.putText,使用fps计数器,我得到了大约25 fps,大约4毫秒一帧,但使用时间戳-datetime.now().timestamp()每个循环返回大约0.001。不知道,但我认为cv2.putText可能在它自己的线程中关闭这些,或者cv2.imshow可能在它自己的线程中。
无论如何,我简化了这个贴在这里,并省略了视频的转换。我的问题是,文本是 Flink 的cv2.imshow

import cv2 as cv
import numpy as np
from tqdm import tqdm
from datetime import datetime

image = np.full(shape = (100, 200, 3), fill_value = (100, 100, 100), dtype = np.uint8)
timestamp = datetime.now().timestamp()

start, stop, scale, font = 6, 0, 0.7, cv.FONT_HERSHEY_COMPLEX
for i in range(500):
    for thikness in range(start, stop, stop + 1 - start):
        color = (0, 0, 0) if thikness == start else (255, 255, 255)

        percent, time, iteration = tqdm.format_meter(
            n = i,
            total = 500,
            elapsed = datetime.now().timestamp() - timestamp,
            ascii = '  ■',
            bar_format = '{percentage:3.0f}% {bar};{remaining};{n_fmt} / {total_fmt}').split(';')

        cv.putText(
            img = image,
            text = percent,
            org = (10, 30),
            fontFace = font,
            fontScale = scale,
            color = color,
            thickness = thikness,
            lineType = cv.LINE_AA
        )
        cv.putText(
            img = image,
            text = time,
            org = (10, 60),
            fontFace = font,
            fontScale = scale,
            color = color,
            thickness = thikness,
            lineType = cv.LINE_AA
        )

        cv.putText(
            img = image,
            text = iteration,
            org = (10, 90),
            fontFace = font,
            fontScale = scale,
            color = color,
            thickness = thikness,
            lineType = cv.LINE_AA
        )

        cv.imshow(winname = 'Frame', mat = image)
        cv.waitKey(1)
uz75evzq

uz75evzq1#

您在cv2.imshow中遇到的 Flink 问题可能是由于您不断更新图像内容并显示它的方式。在您提供的代码中,您在循环中在图像上绘制文本,然后在每次迭代中显示更新的图像。 Flink 是由图像的快速更新引起的。
减少 Flink 的一种方法是在每次迭代中绘制新文本之前清除图像。可以通过在循环开始时创建一个空白图像,然后在其上绘制文本来实现这一点。下面是代码的更新版本:

import cv2 as cv
import numpy as np
from tqdm import tqdm
from datetime import datetime

# Create a blank image to start with
image = np.full(shape=(100, 200, 3), fill_value=(100, 100, 100), dtype=np.uint8)
timestamp = datetime.now().timestamp()

start, stop, scale, font = 6, 0, 0.7, cv.FONT_HERSHEY_COMPLEX
for i in range(500):
    # Create a new blank image in each iteration
    frame = np.copy(image)

    for thickness in range(start, stop, stop + 1 - start):
        color = (0, 0, 0) if thickness == start else (255, 255, 255)

        percent, time, iteration = tqdm.format_meter(
            n=i,
            total=500,
            elapsed=datetime.now().timestamp() - timestamp,
            ascii='  ■',
            bar_format='{percentage:3.0f}% {bar};{remaining};{n_fmt} / {total_fmt}').split(';')

        cv.putText(
            img=frame,
            text=percent,
            org=(10, 30),
            fontFace=font,
            fontScale=scale,
            color=color,
            thickness=thickness,
            lineType=cv.LINE_AA
        )
        cv.putText(
            img=frame,
            text=time,
            org=(10, 60),
            fontFace=font,
            fontScale=scale,
            color=color,
            thickness=thickness,
            lineType=cv.LINE_AA
        )

        cv.putText(
            img=frame,
            text=iteration,
            org=(10, 90),
            fontFace=font,
            fontScale=scale,
            color=color,
            thickness=thickness,
            lineType=cv.LINE_AA
        )

    cv.imshow(winname='Frame', mat=frame)
    cv.waitKey(1)
lzfw57am

lzfw57am2#

我看到了。
它是cv.imshow和cv. waitKey底部的循环。我在第二个for循环中有循环,而不是第一个for循环。
在第二个for循环中有两个文本图像。每个循环中有一个。它显示黑色文本,然后调用cv.imshow,并再次使用白色文本,因此它在白色和黑色之间 Flink ,而不是将白色文本放在黑色之上,这就是我想要的。

import cv2 as cv
import numpy as np
from tqdm import tqdm
from datetime import datetime

start, stop, scale, font, timestamp = 6, 0, 0.7, cv.FONT_HERSHEY_COMPLEX, datetime.now().timestamp()
for i in range(500):
    image = np.full(shape = (100, 300, 3), fill_value = (100, 100, 100), dtype = np.uint8)

    percent, time, iteration = tqdm.format_meter(
        n = i,
        total = 500,
        elapsed = datetime.now().timestamp() - timestamp,
        ascii = ' ',
        bar_format = '{percentage:3.0f}% {bar};{remaining};{n_fmt} / {total_fmt}').split(';')

    for thickness in range(start, stop, stop + 1 - start):
        color = (0, 0, 0) if thickness == start else (255, 255, 255)
        cv.putText(
            img = image,
            text = percent,
            org = (10, 30),
            fontFace = font,
            fontScale = scale,
            color = color,
            thickness = thickness,
            lineType = cv.LINE_AA
        )
        cv.putText(
            img = image,
            text = time,
            org = (10, 60),
            fontFace = font,
            fontScale = scale,
            color = color,
            thickness = thickness,
            lineType = cv.LINE_AA
        )

        cv.putText(
            img = image,
            text = iteration,
            org = (10, 90),
            fontFace = font,
            fontScale = scale,
            color = color,
            thickness = thickness,
            lineType = cv.LINE_AA
        )

    cv.imshow(winname = 'Frame', mat = image)
    cv.waitKey(1)

相关问题