Python OpenCV - 图像阈值处理速度慢,我该如何解决这个问题?

问题描述

总结


简介

最近,我想开始一个使用 OpenCV for Python 的有趣小项目。我想在命令提示符 (Windows 10) 上播放黑白视频 Bad Apple。由于视频中仅使用了两种色调,因此我对阈值精度没有任何问题; simple thresholding(二进制)就足够了。

问题本身

程序本身运行完美,我没有遇到任何错误消息。但是,OpenCV播放视频异常缓慢,我不知道如何解决这个问题。你有解决视频显示缓慢的方法吗?提供一些代码也将不胜感激。

经过一番折腾,看来阈值化过程是罪魁祸首。这个诊断只是一个猜测,欢迎你就这个话题给我启发。

我尝试了什么


  • 清理代码;这对 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()

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

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[:,cv2.THRESH_BINARY)  # define display parameters
    c = (thresh < 3).astype(int)
    threshStr = np.array2string(c,"")  # remove empty spaces
    threshStr = threshStr.replace("["," ")  # 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()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)