圖片透明化過渡效果python實現

今天打開實驗要求一看:圖片融合 但是老師說交的作業要儘可能炫酷???又是就有了這麼個玩意
然後我們來看以下這玩意是怎麼寫的。要做動畫效果,實時計算每一幀就行了,改改fps和漸變係數就好了,於是就有了

import cv2 as cv
import numpy as np

# this python project perform transparency transition
# in input picture set

def transition(img_path, time):
    # time used to set the time of one frame remaining
    num = len(img_path)
    for i in range(0, num - 1):
        first = cv.imread(img_path[i], cv.IMREAD_COLOR)
        second = cv.imread(img_path[i+1], cv.IMREAD_COLOR)
        # temp array for show
        fs = np.zeros( (max(first.shape[0], second.shape[0]), max(first.shape[1], second.shape[1]), 3), dtype=np.uint8 )
        print("shape of first:", first.shape,"shape of second:", second.shape,"shape of show array:", fs.shape)
        coef = np.float(0)  # coefficient transparency
        while(coef < 1):
            for row in range(first.shape[0]):
                for col in range(first.shape[1]):
                    for channels in range(first.shape[2]):
                        fs[row][col][channels] = np.uint8((1 - coef) * first[row][col][channels])
            for row in range(second.shape[0]):
                for col in range(second.shape[1]):
                    for channels in range(first.shape[2]):
                        value = fs[row][col][channels] + np.uint8(coef * second[row][col][channels])
                        fs[row][col][channels] = value if value <= 255 else 255
            coef = coef + 0.8
            cv.imshow('show', fs)
            cv.waitKey(time)


if __name__=='__main__':
    print("program start!")
    num = int(input("enter numbers of picture set:"))
    path = []
    while(num):
        num = num - 1
        path.append(input("enter the picture path:"))
    transition(path, 15)

但是測試的時候發現,這個卡的一批,fps完全不是我設置的,而是取決於運行速度,不過作爲PPT動畫來說的話,這個還是可以的。
但是PPT動畫是不能滿足人類的 不然JC哪來那麼多作畫崩壞,作爲人類智慧的結晶—numpy , 必然存在人類加速智慧的奇巧淫技 沒錯那就是GPU加速 矩陣運算!所以下面我們的重點就是怎麼讓程序加速,實現三輪到瑪莎兒拉蒂的飛躍
衆所周知,numpy對於矩陣運算有着無可比擬的優越性,所以需要把對元素的操作改爲對矩陣的操作。但是由於每張圖不可能都一樣大,不同形狀的矩陣是不能運算,因此,BB這麼多就是想說怎麼補齊矩陣
這是說明,全是英文我也不想翻了,等什麼時候看完了ufnc再說吧

所以修改完之後是這個樣子的

import cv2 as cv
import numpy as np

# this python project perform transparency transition
# in input picture set

def transition(img_path, time):
    # time used to set the time of one frame remaining
    num = len(img_path)
    for i in range(0, num - 1):
        first = cv.imread(img_path[i], cv.IMREAD_COLOR)
        second = cv.imread(img_path[i+1], cv.IMREAD_COLOR)
        add_shape = (max(first.shape[0], second.shape[0]), max(first.shape[1], second.shape[1]))
        coef = np.float(0)  # coefficient transparency
        while(coef < 1):
            # temp array for show
            fs = np.array(np.uint8(first * (1 - coef)))
            fs = np.pad(fs, ((0, add_shape[0]- fs.shape[0]), (0, add_shape[1] - fs.shape[1]), (0, 0)), 'constant',constant_values=0)
            record = np.pad(second * coef, ((0, add_shape[0]- second.shape[0]), (0, add_shape[1] - second.shape[1]), (0, 0)), 'constant',constant_values=0)
            fs = fs + record
            fs[fs > 255] = 255
            fs = np.uint8(fs)
            cv.imshow('show', fs)
            cv.waitKey(time)
            coef = coef + 0.05



if __name__=='__main__':
    print("program start!")
    num = int(input("enter numbers of picture set:"))
    path = []
    while(num):
        num = num - 1
        path.append(input("enter the picture path:"))
    transition(path, 500)
    cv.destroyAllWindows()

這個是真的快,numpyNB

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章