通过python和两张图片实现漫天蝴蝶飞舞的合成图像

        学校里的老师要求用python语言实现漫天蝴蝶飞舞的合成图像,在网上找不到这道题,找到类似的也是用matlab写的。

        然后自己写完之后觉得展示的效果还不错,就分享出来让有需要的同学来参考参考。

原题如下:

 

 接下来是我的代码:

import cv2
import random
import matplotlib.pyplot as plt
from skimage import io, transform
import numpy as np


def cuoqie(imge):  # 错切函数
    m1, m2 = imge.shape[:2]
    q = random.randint(0, 3)
    if q == 0:
        cc = random.random()
        vv = random.random()
        x, y = 0, 0
    elif q == 1:
        cc = -random.random()
        vv = random.random()
        x, y = -cc * m1, 0
    elif q == 2:
        cc = -random.random()
        vv = -random.random()
        x, y = -cc * m1, -vv * m2
    else:
        cc = random.random()
        vv = -random.random()
        x, y = 0, -vv * m2
    shearM = np.array([
        [1, cc, x],
        [vv, 1, y]
    ], dtype=np.float32)
    img_shear = cv2.warpAffine(imge, shearM, dsize=(int(m2 + abs(cc * m1)), int(m1 + abs(vv * m2))))
    return img_shear


def xuanzhuan(imge):  # 旋转函数
    xz = random.randint(0, 360)
    imge = transform.rotate(imge, xz, resize=True)
    return imge


def daxiao(imge, y, x):  # 大小函数
    wz = random.randint(2, 5)  # 随机缩小倍数
    y1, x1 = int(y / wz), int(x / wz)  # 新的图片尺寸
    imge = transform.resize(imge, (y1, x1))  # 缩小后的图片
    return imge, y1, x1


def weiyi(imge, y1, x1, mz, my):  # 随机位移函数
    x2, y2 = random.randint(1, mz - x1), random.randint(1, my - y1)  # 选择随机位置
    mat_translation = np.float32([[1, 0, x2], [0, 1, y2]])  # 变换矩阵:设置平移变换所需的计算矩阵:2行3列
    dst = cv2.warpAffine(imge, mat_translation, (x1 + x2, y1 + y2))
    return dst


def pingjie(butterfly, massif):  # 拼接图片
    m1, m2, m3 = butterfly.shape
    for h in range(m1):
        for w in range(m2):
            for z in range(m3):
                if not butterfly[h, w, z] <= 30 / 255:
                    massif[h, w, z] = butterfly[h, w, z] * 255
                else:
                    massif[h, w, z] = massif[h, w, z]
    return massif


def tongdao(butterfly):  # 交换色彩通道的函数
    s = []
    while (len(s) < 2):
        x = random.randint(0, 2)
        if x not in s:
            s.append(x)
    p, q = s[:]
    m1, m2 = butterfly.shape[:2]
    for h in range(m1):
        for w in range(m2):
            n = butterfly[h, w, :]
            butterfly[h, w, p] = n[q]
            butterfly[h, w, q] = n[p]
    return butterfly


def img_mian(butterfly, massif):  # 主函数
    num = random.randint(10, 25)  # 设置随机数量的蝴蝶
    y, x = butterfly.shape[0:2]  # 的到蝴蝶图片的高和宽
    my, mz = massif.shape[0:2]
    for i in range(num):
        butterfly1 = cuoqie(butterfly)  # 对图片进行随机错切
        butterfly2 = xuanzhuan(butterfly1)  # 对图片进行随机旋转
        butterfly3, y1, x1 = daxiao(butterfly2, y, x)  # 对图片进行随机缩小
        butterfly4 = weiyi(butterfly3, y1, x1, mz, my)  # 对图片进行随机位移
        butterfly5 = tongdao(butterfly4)  # 随机交换两个色彩通道
        massif = pingjie(butterfly5, massif)  # 对图片进行拼接
    return massif


if __name__ == '__main__':
    massif = io.imread('./image.jpeg')
    butterfly = io.imread('./image.png')
    beautiful_pt = img_mian(butterfly, massif)  # 调用主函数
    plt.imshow(beautiful_pt.astype('uint8'))
    plt.show()

下面就是运行的结果了:

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值