图像处理——EDSR\ESPCN\FSRCNN图像超分放大单输出

实现目标:

实现图像超分放大单输出,其中可采用edsr, espcn, fsrcnn or lapsrn等算法。

调用现成的算法模型进行处理,然后保存。


代码如下:

"""
Created on 2023/4/11 09:52
@author: liuwenq
图像超分放大单输出
"""
import cv2
from cv2 import dnn_superres
import matplotlib
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")

def main():

    # 选择算法,bilinear, bicubic, edsr, espcn, fsrcnn or lapsrn
    algorithm = "edsr"
    # 设置超分图像放大比例,但对应的模型倍数也要做出改变
    scale = 4
    # 模型路径

    path = "E:/EDSR_x4.pb"
    # 载入图像
    img = cv2.imread("E:/001.png")
    # 如果输入的图像为空
    if img is None:
        print("Couldn't load image ")
        return
    original_img = img.copy()
    # 创建模型
    sr = dnn_superres.DnnSuperResImpl_create()
    # 判别使用的模型
    if algorithm == "bilinear":  # 双线性插值法
        img_new = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_LINEAR)
    elif algorithm == "bicubic":  # 基于4x4像素邻域的三次样条插值法
        img_new = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
    elif algorithm == "edsr" or algorithm == "espcn" or algorithm == "fsrcnn" or algorithm == "lapsrn":
        # 读取模型
        sr.readModel(path)
        # 读入算法和放大比例
        sr.setModel(algorithm, scale)
        # 利用模型放大图像
        img_new = sr.upsample(img)
    # 如果模型读取失败,输出 Algorithm not recognized
    else:
        print("Algorithm not recognized")
    # 如果失败,输出 Loading failed
    if img_new is None:
        print("Loading failed")
    print("Loading succeeded. \n")
    # 展示图片
    #cv2.namedWindow("Initial Image", cv2.WINDOW_AUTOSIZE)
    #cv2.imshow("Initial Image", img_new)

    #直接输出图像
    # while (1):
    #     cv2.imshow('img', img)
    #     cv2.imshow('img_new', img_new)
    #     if cv2.waitKey(1) & 0xFF == 27:
    #         break
    # cv2.destroyAllWindows()

    #将结果保存在根目录下
    cv2.imwrite("./saved_origin.jpg", img)
    if algorithm == "bilinear":  # 双线性插值法
        cv2.imwrite("./saved_bilinear.jpg", img_new)
    elif algorithm == "bicubic":  # 基于4x4像素邻域的三次样条插值法
        cv2.imwrite("./saved_bicubic.jpg", img_new)
    elif algorithm == "edsr":
        cv2.imwrite("./saved_edsr.jpg", img_new)
    elif algorithm == "espcn":
        cv2.imwrite("./saved_espcn.jpg", img_new)
    elif algorithm == "fsrcnn":
        cv2.imwrite("./saved_fsrcnn.jpg", img_new)
    elif algorithm == "lapsrn":
        cv2.imwrite("./saved_lapsrn.jpg", img_new)
    # 如果图片读取失败,输出 image not recognized
    else:
        print("Algorithm not recognized")
    # cv2.imwrite("./saved_lapsrn.jpg", img_new)
    cv2.waitKey(0)
if __name__ == '__main__':
    main()

实现效果:

注意:该算法处理后直接输出为图片并保存在根目录下,(因为放大问题),未使用plt函数进行效果展示。

原图:

EDSR算法进行超分放大:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值