Python OpenCV慢速图像阈值设置,如何解决此问题?

2024-09-30 01:32:44 发布

您现在位置:Python中文网/ 问答频道 /正文

总结


导言

最近,我想开始一个使用OpenCV for Python的有趣的小项目。我想在我的命令提示符(Windows10)上播放Bad Apple,一个黑白视频。因为视频中只使用了两种色调,所以我没有关于阈值准确性的问题simple thresholding(二进制)就足够了

问题本身

程序本身运行完美,我没有遇到任何错误消息。然而,OpenCV播放视频异常缓慢,我不知道如何解决这个问题。您是否有解决方案来修复此缓慢的视频显示?提供一些代码也将非常感激

After some finagling, it appears that the thresholding process is the culprit. This diagnosis is merely a guess, you are welcome to enlighten me on that topic.

我试过的


  • 清理代码;关于OpenCV的性能,这是徒劳的
  • 使用线程。然而,由于我不是这方面的专家,这是一次失败的尝试。然而,我相信如果我们沿着同一条路线走下去,我们可以解决一些问题

代码


这是我的原始代码。您可以通过命令提示符运行它,请记住您必须下载了视频(在我的程序中它的名称为badapple.mp4)。您应该注意到,视频以慢动作播放,音乐不同步

import cv2
import numpy as np
import colorama
from ffpyplayer.player import MediaPlayer

cap = cv2.VideoCapture('badapple.mp4')
player = MediaPlayer('badapple.mp4')
size = (160, 60)
np.set_printoptions(threshold=np.inf)
colorama.init()


while cap.isOpened():

    ret, frame = cap.read()  # play the video associated to the file
    audio_frame = player.get_frame()  # play the audio associated to the file
    grey = frame[:, :, 0]  # convert current frame to a greyscale image

    ret, thresh = cv2.threshold(cv2.resize(grey, size), 100, 255, cv2.THRESH_BINARY)
    c = (thresh < 3).astype(int)
    threshStr = np.array2string(c, max_line_width=np.inf)

    threshStr = threshStr.replace(" ", "")  # remove superfluous empty spaces
    threshStr = threshStr.replace("[", "")  # remove [
    threshStr = threshStr.replace("]", "")  # remove ]
    threshStr = threshStr.replace("0", "&")  # display "&" for the binary value 0
    threshStr = threshStr.replace("1", " ")  # display an empty space for the binary value 1

    print("\033[12A")  # smoother rendering
    print(threshStr)  # display the image on the command prompt

    if cv2.waitKey(28) & 0xFF == ord("q"):  # exit the program when "Q" is pressed
        break

cap.release()
cv2.destroyAllWindows()

这里是另一个程序,我试图在其中使用线程来加速进程。我试图将我的程序实现成一个通用的OpenCV多线程处理程序,我在this website上找到了这个程序

from imutils.video import FileVideoStream
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import cv2
import colorama
from ffpyplayer.player import MediaPlayer

ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", required=True, help="path to input video file")
args = vars(ap.parse_args())

fvs = FileVideoStream(args["video"]).start()
fps = FPS().start()

player = MediaPlayer('badapple.mp4')
size = (160, 60)
np.set_printoptions(threshold=np.inf)
colorama.init()

while fvs.more():
    frame = fvs.read()

    # ---------------------------------------------------------------------

    audio_frame = player.get_frame()  # play the audio associated to the file
    grey = frame[:, :, 0]  # convert current frame to a greyscale image

    ret, thresh = cv2.threshold(cv2.resize(grey, size), 100, 255, cv2.THRESH_BINARY)  # define display parameters
    c = (thresh < 3).astype(int)
    threshStr = np.array2string(c, max_line_width=np.inf)

    threshStr = threshStr.replace(" ", "")  # remove empty spaces
    threshStr = threshStr.replace("[", "")  # remove [
    threshStr = threshStr.replace("]", "")  # remove ]
    threshStr = threshStr.replace("0", "&")  # display "&" for the binary value 0
    threshStr = threshStr.replace("1", " ")  # display an empty space for the binary value 1

    print("\033[12A")  # smoother rendering
    print(threshStr)  # display the image on the command prompt

    if cv2.waitKey(28) & 0xFF == ord("q"):  # exit the program when "Q" is pressed
        break

    # ---------------------------------------------------------------------

    frame = imutils.resize(frame, width=450)
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    frame = np.dstack([frame, frame, frame])

    cv2.imshow("Frame", frame)
    cv2.waitKey(1)
    fps.update()

fps.stop()
cv2.destroyAllWindows()
fvs.stop()

Tags: thetoimportfor视频videodisplaynp

热门问题