图片透明化过渡效果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

猜你喜欢

转载自blog.csdn.net/white_156/article/details/104945702