Python+OpenCV实现图像识别替换功能详解

 更新时间:2022年07月14日 16:11:47   作者:阿涛的一天  
OpenCV-Python是一个Python库,旨在解决计算机视觉问题。本文将利用Python+OpenCV实现图像识别替换功能,感兴趣的小伙伴可以动手尝试一下

OpenCV-Python是一个Python库,旨在解决计算机视觉问题。

OpenCV是一个开源的计算机视觉库,1999年由英特尔的Gary Bradski启动。Bradski在访学过程中注意到,在很多优秀大学的实验室中,都有非常完备的内部公开的计算机视觉接口。这些接口从一届学生传到另一届学生,对于刚入门的新人来说,使用这些接口比重复造轮子方便多了。这些接口可以让他们在之前的基础上更有效地开展工作。OpenCV正是基于为计算机视觉提供通用接口这一目标而被策划的。

安装opencv

pip3 install -i https://pypi.doubanio.com/simple/ opencv-python

思路:

1、首先区分三张图片:

base图片代表初始化图片;

template图片代表需要在大图中匹配的图片;

white图片为需要替换的图片。

2、然后template图片逐像素缩小匹配,设定阈值,匹配度到达阈值的图片,判定为在初始图片中;否则忽略掉。

3、匹配到最大阈值的地方,返回该区域的位置(x,y)

4、然后用white图片resize到相应的大小,填补到目标区域。

match函数:

"""检查模板图片中是否包含目标图片"""
def make_cv2(photo1, photo2):
    global x, y, w, h, num_1,flag
    starttime = datetime.datetime.now()
    #读取base图片
    img_rgb = cv2.imread(f'{photo1}')
    #读取template图片
    template = cv2.imread(f'{photo2}')
    h, w = template.shape[:-1]
    print('初始宽高', h, w)
    res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
    print('初始最大相似度', res.max())
    threshold = res.max()
    """,相似度小于0.2的,不予考虑;相似度在[0.2-0.75]之间的,逐渐缩小图片"""
    print(threshold)
    while threshold >= 0.1 and threshold <= 0.83:
        if w >= 20 and h >= 20:
            w = w - 1
            h = h - 1
            template = cv2.resize(
                template, (w, h), interpolation=cv2.INTER_CUBIC)
            res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
            threshold = res.max()
            print('宽度:', w, '高度:', h, '相似度:', threshold)
        else:
            break
    """达到0.75覆盖之前的图片"""
    if threshold > 0.8:
        loc = np.where(res >= threshold)
        x = int(loc[1])
        y = int(loc[0])
        print('覆盖图片左上角坐标:', x, y)
        for pt in zip(*loc[::-1]):
            cv2.rectangle(
                img_rgb, pt, (pt[0] + w, pt[1] + h), (255, 144, 51), 1)
        num_1 += 1
        endtime = datetime.datetime.now()
        print("耗时:", endtime - starttime)
        overlay_transparent(x, y, photo1, photo3)
    else:
        flag = False

replace函数:

"""将目标图片镶嵌到指定坐标位置"""
def overlay_transparent(x, y, photo1, photo3):
    #覆盖图片的时候上下移动的像素空间
    y += 4
    global w, h, num_2
    background = cv2.imread(f'{photo1}')
    overlay = cv2.imread(f'{photo3}')
    """缩放图片大小"""
    overlay = cv2.resize(overlay, (w, h), interpolation=cv2.INTER_CUBIC)
    background_width = background.shape[1]
    background_height = background.shape[0]
    if x >= background_width or y >= background_height:
        return background
    h, w = overlay.shape[0], overlay.shape[1]
    if x + w > background_width:
        w = background_width - x
        overlay = overlay[:, :w]
    if y + h > background_height:
        h = background_height - y
        overlay = overlay[:h]
    if overlay.shape[2] < 4:
        overlay = np.concatenate([overlay, np.ones((overlay.shape[0], overlay.shape[1], 1), dtype=overlay.dtype) * 255],axis=2,)
    overlay_image = overlay[..., :3]
    mask = overlay[..., 3:] / 255.0
    background[y:y + h,x:x + w] = (1.0 - mask) * background[y:y + h,x:x + w] + mask * overlay_image
    # path = 'result'
    path = ''
    cv2.imwrite(os.path.join(path, f'1.png'), background)
    num_2 += 1
    print('插入成功。')
    init()

每次执行需要初始化x,y(图片匹配初始位置参数),w,h(图片缩放初始宽高)

x = 0
y = 0
w = 0
h = 0
flag = True
threshold = 0
template = ''
num_1 = 0
num_2 = 0
photo3 = ''
"""参数初始化"""
def init():
    global x, y, w, h, threshold, template,flag
    x = 0
    y = 0
    w = 0
    h = 0
    threshold = 0
    template = ''

完整代码

import cv2
import datetime
import os
import numpy as np
x = 0
y = 0
w = 0
h = 0
flag = True
threshold = 0
template = ''
num_1 = 0
num_2 = 0
photo3 = ''
"""参数初始化"""
def init():
    global x, y, w, h, threshold, template,flag
    x = 0
    y = 0
    w = 0
    h = 0
    threshold = 0
    template = ''

"""检查模板图片中是否包含目标图片"""
def make_cv2(photo1, photo2):
    global x, y, w, h, num_1,flag
    starttime = datetime.datetime.now()
    img_rgb = cv2.imread(f'{photo1}')
    template = cv2.imread(f'{photo2}')
    h, w = template.shape[:-1]
    print('初始宽高', h, w)
    res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
    print('初始最大相似度', res.max())
    threshold = res.max()
    """,相似度小于0.2的,不予考虑;相似度在[0.2-0.75]之间的,逐渐缩小图片"""
    print(threshold)
    while threshold >= 0.1 and threshold <= 0.83:
        if w >= 20 and h >= 20:
            w = w - 1
            h = h - 1
            template = cv2.resize(
                template, (w, h), interpolation=cv2.INTER_CUBIC)
            res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
            threshold = res.max()
            print('宽度:', w, '高度:', h, '相似度:', threshold)
        else:
            break
    """达到0.75覆盖之前的图片"""
    if threshold > 0.8:
        loc = np.where(res >= threshold)
        x = int(loc[1])
        y = int(loc[0])
        print('覆盖图片左上角坐标:', x, y)
        for pt in zip(*loc[::-1]):
            cv2.rectangle(
                img_rgb, pt, (pt[0] + w, pt[1] + h), (255, 144, 51), 1)
        num_1 += 1
        endtime = datetime.datetime.now()
        print("耗时:", endtime - starttime)
        overlay_transparent(x, y, photo1, photo3)
    else:
        flag = False


"""将目标图片镶嵌到指定坐标位置"""
def overlay_transparent(x, y, photo1, photo3):
    y += 0
    global w, h, num_2
    background = cv2.imread(f'{photo1}')
    overlay = cv2.imread(f'{photo3}')
    """缩放图片大小"""
    overlay = cv2.resize(overlay, (w, h), interpolation=cv2.INTER_CUBIC)
    background_width = background.shape[1]
    background_height = background.shape[0]
    if x >= background_width or y >= background_height:
        return background
    h, w = overlay.shape[0], overlay.shape[1]
    if x + w > background_width:
        w = background_width - x
        overlay = overlay[:, :w]
    if y + h > background_height:
        h = background_height - y
        overlay = overlay[:h]
    if overlay.shape[2] < 4:
        overlay = np.concatenate([overlay, np.ones((overlay.shape[0], overlay.shape[1], 1), dtype=overlay.dtype) * 255],axis=2,)
    overlay_image = overlay[..., :3]
    mask = overlay[..., 3:] / 255.0
    background[y:y + h,x:x + w] = (1.0 - mask) * background[y:y + h,x:x + w] + mask * overlay_image
    # path = 'result'
    path = ''
    cv2.imwrite(os.path.join(path, f'1.png'), background)
    num_2 += 1
    print('插入成功。')
    init()


if __name__ == "__main__":
    photo1 = "1.png"
    photo2 = "3.png"
    photo3 = "white.png"

    while flag == True:
        make_cv2(photo1, photo2)
        overlay_transparent(x, y, photo1, photo3)

执行结果:

到此这篇关于Python+OpenCV实现图像识别替换功能详解的文章就介绍到这了,更多相关Python OpenCV图像识别替换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python 中的异步 for 循环示例详解

    Python 中的异步 for 循环示例详解

    这篇文章主要介绍了Python中的异步for循环,我们将讨论 Python 库 asyncio 和运行异步代码所需的函数,需要的朋友可以参考下
    2023-05-05
  • python基础之文件操作和异常处理

    python基础之文件操作和异常处理

    这篇文章主要介绍了python基础之文件操作和异常处理,文中有非常详细的代码示例,对正在学习python基础的小伙伴们有一定的帮助,需要的朋友可以参考下
    2021-04-04
  • Python模块Typing.overload的使用场景分析

    Python模块Typing.overload的使用场景分析

    在 Python 中,typing.overload 是一个用于定义函数重载的装饰器,函数重载是指在一个类中可以定义多个相同名字但参数不同的函数,使得在调用函数时可以根据参数的不同选择不同的函数执行,这篇文章主要介绍了Python模块Typing.overload的使用,需要的朋友可以参考下
    2024-02-02
  • Python实现的字典值比较功能示例

    Python实现的字典值比较功能示例

    这篇文章主要介绍了Python实现的字典值比较功能,可实现针对字典格式数据的判断、比较功能,涉及Python字典格式数据的遍历、判断等相关操作技巧,需要的朋友可以参考下
    2018-01-01
  • Python之Django自动实现html代码(下拉框,数据选择)

    Python之Django自动实现html代码(下拉框,数据选择)

    这篇文章主要介绍了Python之Django自动实现html代码(下拉框,数据选择),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • python重要函数eval多种用法解析

    python重要函数eval多种用法解析

    这篇文章主要介绍了python重要函数eval多种用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • Python 使用 docopt 解析json参数文件过程讲解

    Python 使用 docopt 解析json参数文件过程讲解

    这篇文章主要介绍了Python 使用 docopt 解析json参数文件过程讲解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • 详解pyqt5 动画在QThread线程中无法运行问题

    详解pyqt5 动画在QThread线程中无法运行问题

    这篇文章主要介绍了详解pyqt5 动画在QThread线程中无法运行问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Python读取和存储yaml文件的方法

    Python读取和存储yaml文件的方法

    本文主要介绍了Python读取和存储yaml文件的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • Python绘制七彩花朵(用Turtle)

    Python绘制七彩花朵(用Turtle)

    这篇文章主要给大家介绍了关于Python使用Turtle绘制七彩花朵的相关资料,通过本文介绍的方法就算刚入门的朋友也可以很快的入手绘制出漂亮的七彩花朵,需要的朋友可以参考下
    2023-07-07

最新评论