栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【学习记录】图片局部放大

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【学习记录】图片局部放大

写在前面:本博客仅作记录学习之用,部分图片来自网络,如需使用请注明出处,同时如有侵犯您的权益,请联系删除!

文章目录
  • 前言
  • 局部放大
  • 函数实现
    • 单一局部放大置于图像内部
    • 多局部放大拼接于图形侧面
    • 主函数
  • 效果
  • 总结
  • 致谢

前言

本博客仅为学习记录之用,目的在于后续若需要相关的有资可查。在言语上恐有诸多纰漏,如有错误,欢迎指出交流学习!
本博客所包含的大致内容: 图片某一局部放大并置于图形内部;图片多个局部放大并拼接在原图侧面。


局部放大

为什么要局部放大?无论的图像生成、修复、去污等等,如生成对抗方法;亦或是图像分割,如unet。对于方法的结果都需要进行有效的展示,凸显方法的优越性能,往往细节的部分才是我们所更加关注的。但往往在图像本身很难观察到这些细节,因此有意识的将需要的局部放大是展示效果的有效方法。


函数实现
单一局部放大置于图像内部

思路也简单,首先获取到需要关注的部分,然后在将其进行插值放大,最后放在原图的某一位置即可。

本函数参数传入图像,需要目标区域的指标,其次放在的位置,支持四个角以及中心,以及放大的倍数。
目标区域为矩形,长宽比任意。


def Partial_magnification(pic, target, location='lower_right', ratio=1):
    '''
    :param pic: input pic
    :param target: Intercept area, for example [target_x, target_y, target_w, target_h]
    :param location: lower_right,lower_left,top_right,top_left,center
    :param ratio: gain
    :return: oringal pic, pic
    '''
    img = copy.copy(pic)

    w, h = pic.shape[1], pic.shape[0],

    target_x, target_y = target[0], target[1]
    target_w, target_h = target[2], target[3]
    cv2.rectangle(pic, (target_x, target_y), (target_x + target_w, target_y + target_h), (255, 255, 0), 2)
    new_pic = pic[target_y:target_y + target_h, target_x:target_x + target_w]
    new_pic = cv2.resize(new_pic, (target_w*ratio, target_h*ratio), interpolation=cv2.INTER_CUBIC)
    if location == 'lower_right':
        pic[h-1-target_h*ratio:h-1, w-1-target_w*ratio:w-1] = new_pic
        cv2.line(pic, (target_x + target_w, target_y + target_h), (w-1-target_w*ratio, h-1-target_h*ratio), (255, 0, 0),2)
    elif location == 'lower_left':
        pic[h-1-target_h*ratio:h-1, 0:target_w*ratio] = new_pic
    elif location == 'top_right':
        pic[0:target_h*ratio, w-1-target_w*ratio:w-1] = new_pic
    elif location == 'top_left':
        pic[0:target_h*ratio, 0:target_w*ratio] = new_pic
    elif location == 'center':
        pic[int(h/2-target_h*ratio/2):int(h/2+target_h*ratio/2),
            int(w/2-target_w*ratio/2):int(w/2+target_w*ratio/2)] = new_pic
    return img, pic

多局部放大拼接于图形侧面

当存在多个局部需要放大时,若都置于原图内部,遮挡图片内容的可能性较大,因此将其置于图像侧面。

本函数仅支持两个局部区域放大。和第一个函数不同的是两个局部放大的区域长宽比需要一致,为了方便拼接。没有设置放大倍数,默认局部放大后拼接图片与原图等高。


def Partial_magnification_2(pic, target1, target2, *args):
    '''
    :param pic: input pic
    :param target: Intercept area, for example [target_x, target_y, target_w, target_w]
    :param location: lower_right,lower_left,top_right,top_left,center
    :param ratio: gain
    :return: oringal pic, pic
    '''
    # imgg = copy.copy(pic)
    w, h = pic.shape[1], pic.shape[0],

    assert target1[0]+target1[2] < w and target1[1]+target1[3] < h,
        'The target1 area is too large and exceeds the image size'
    assert target2[0]+target2[2] < w and target2[1]+target2[3] < h,
        'The target2 area is too large and exceeds the image size'

    assert target1[2] > 10 or target1[3] > 10, 
        'The target1 area is too small, not recommended'
    assert target2[2] > 10 or target2[3] > 10, 
        'The target2 area is too small, not recommended'

    assert target1[0] > 0 and target1[0] < w, 
        'The starting point of the target1 area is beyond the scope of the image'
    assert target2[0] > 0 and target2[0] < w, 
        'The starting point of the target2 area is beyond the scope of the image'

    if target2[2] / target2[3] == target1[2] / target1[3]:  #
        R = target2[2]/target2[3]
        if target1[1] > target2[1]:
            target1_x, target1_y = target2[0], target2[1]
            target1_w, target1_h = target2[2], target2[3]

            target2_x, target2_y = target1[0], target1[1]
            target2_w, target2_h = target1[2], target1[3]

        else:
            target1_x, target1_y = target1[0], target1[1]
            target1_w, target1_h = target1[2], target1[3]

            target2_x, target2_y = target2[0], target2[1]
            target2_w, target2_h = target2[2], target2[3]

        cv2.rectangle(pic, (target1_x, target1_y), (target1_x + target1_w, target1_y + target1_h), (255, 250, 255), 2)
        cv2.rectangle(pic, (target2_x, target2_y), (target2_x + target2_w, target2_y + target2_h), (255, 252, 255), 2)

        new_pic1 = pic[target1_y:target1_y + target1_h, target1_x:target1_x + target1_w]
        new_pic1 = cv2.resize(new_pic1, (int(h//2 * R), h//2), cv2.INTER_CUBIC)

        new_pic2 = pic[target2_y:target2_y + target2_h, target2_x:target2_x + target2_w]
        new_pic2 = cv2.resize(new_pic2, (int(h//2 * R), h//2))

        img = np.zeros((h, int(h//2 * R), 3), np.uint8)
        img[0:h//2, 0:int(h//2 * R)] = new_pic1
        img[h//2:h, 0:int(h//2 * R)] = new_pic2

        hmerge = np.hstack((pic, img))
        cv2.line(hmerge, (target1_x + target1_w, target1_y), (w, 0), (255, 255, 255), 2)
        cv2.line(hmerge, (target2_x + target2_w, target2_y + target2_h), (w, h), (255, 255, 255), 2)
        return  hmerge
    else:
        raise ValueError('Make sure the aspect ratio of target is consistent !')


主函数

为方便复现提供了参考的主程序,如果需要批量放大,添加循环即可。


if __name__ == '__main__':
    img = cv2.imread(r'./test/Image_11L.jpg')  
    target1 = [250, 250, 100, 100]
    pic, pic1 = Partial_magnification(img, target1, location='lower_right', ratio=4)
    target2 = [180, 400, 100, 50]
    pic, pic2 = Partial_magnification(pic, target2, location='lower_left', ratio=2)
    hmerge = np.hstack((pic1, pic2))

    if max(hmerge.shape[0], hmerge.shape[1]) > 1000:
        # cv2.namedWindow('merge', 0)
        # cv2.resizeWindow('merge', 1000, int(1000*hmerge.shape[0]/hmerge.shape[1]))
        # cv2.imshow('merge', hmerge)
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()
        hmerge = cv2.cvtColor(hmerge, cv2.COLOR_BGR2RGB)
        fig = plt.figure(figsize=(40, 20))  # figsize 尺寸
        plt.imshow(hmerge)
        plt.savefig('aaa_bbb.png', dpi=200)  # dpi分辨率
        plt.show()
    else:
        plt.imshow(hmerge)
        plt.show()
if __name__ == '__main__':
    img = cv2.imread(r'./test/Image_11L.jpg')  
    target1 = [250, 650, 100, 100]
    target2 = [450, 400, 100, 100]
    pic1 = Partial_magnification_2(img, target1, target2)

    hmerge = cv2.cvtColor(pic1, cv2.COLOR_BGR2RGB)
    fig = plt.figure(figsize=(30, 20))
    fig.patch.set_facecolor('gray')
    plt.imshow(hmerge)
    plt.savefig('aaa_bbb.png', dpi=300, bbox_inches='tight')
    plt.show()

效果

以下是两个函数的测试结果,主程序如上。



总结

以上就是本文的主要内容,都是OpenCV和plt的基本使用上实现图像的局部放大。


致谢

欲尽善本文,因所视短浅,怎奈所书皆是瞽言蒭议。行文至此,诚向予助与余者致以谢意。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026270.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号