簡體   English   中英

Python openCV - 處理視頻的時間很長

[英]Python openCV - very long time of processing video

我必須將視頻處理為灰度,然后對其執行抖動。 一切正常,但在最后 1.5 小時處理 16 秒視頻。 基本上每幀大約需要 10-20 秒。 我從未處理過任何視頻,但聽說這是一個非常繁重的過程,所以只是想確定它是否正常或我的算法是否錯誤?

這就是抖動算法的樣子:

def minmax(v):
    if v > 255:
        v = 255
    if v < 0:
        v = 0
    return v


def dithering_gray(inMat, samplingF):
    # grab the image dimensions
    h = inMat.shape[0]
    w = inMat.shape[1]

    for y in range(0, h - 1):
        for x in range(1, w - 1):
            old_p = inMat[y, x]
            new_p = np.round(samplingF * old_p / 255.0) * (255 / samplingF)
            inMat[y, x] = new_p

            quant_error_p = old_p - new_p

            inMat[y, x + 1] = minmax(inMat[y, x + 1] + quant_error_p * 7 / 16.0)
            inMat[y + 1, x - 1] = minmax(inMat[y + 1, x - 1] + quant_error_p * 3 / 16.0)
            inMat[y + 1, x] = minmax(inMat[y + 1, x] + quant_error_p * 5 / 16.0)
            inMat[y + 1, x + 1] = minmax(inMat[y + 1, x + 1] + quant_error_p * 1 / 16.0)

    return inMat

因此,感謝評論中的回答,我提高了 numba 的性能,也許通過一些更高級的設置可能會更好,但目前的結果完全讓我滿意,所以我需要做的就是添加裝飾器,現在它看起來像這樣並且工作得更快,從 1.5 小時到 30 秒:

from numba import jit

@jit(nopython=True)
def minmax(v):
    if v > 255:
        v = 255
    if v < 0:
        v = 0
    return v

@jit(nopython=True)
def dithering_gray(inMat, samplingF):
    # grab the image dimensions
    h = inMat.shape[0]
    w = inMat.shape[1]

    # loop over the image
    for y in range(0, h - 1):
        for x in range(1, w - 1):
            # threshold the pixel
            old_p = inMat[y, x]
            new_p = np.round(samplingF * old_p / 255.0) * (255 / samplingF)
            inMat[y, x] = new_p

            quant_error_p = old_p - new_p

            inMat[y, x + 1] = minmax(inMat[y, x + 1] + quant_error_p * 7 / 16.0)
            inMat[y + 1, x - 1] = minmax(inMat[y + 1, x - 1] + quant_error_p * 3 / 16.0)
            inMat[y + 1, x] = minmax(inMat[y + 1, x] + quant_error_p * 5 / 16.0)
            inMat[y + 1, x + 1] = minmax(inMat[y + 1, x + 1] + quant_error_p * 1 / 16.0)

    # return the thresholded image
    return inMat

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM