常用的图像处理插值方法

#如果要缩小图像,通常推荐使用#INTER_AREA插值效果最好
#而要放大图像,通常使用INTER_CUBIC(速度较慢,但效果最好),或者使用INTER_LINEAR(速度较快,效果还可以)
#至于最近邻插值INTER_NEAREST,一般不推荐使用
image_path = "images_data/0_1.jpeg"
img = cv2.imread(image_path)
# img_new = cv2.resize(img, (64, 64), interpolation=cv2.INTER_CUBIC)
# img_new = cv2.resize(img, (64, 64), interpolation=cv2.INTER_LINEAR)
img_new = cv2.resize(img, (64, 64), interpolation=cv2.INTER_AREA)

cv2.namedWindow('case', cv2.WINDOW_AUTOSIZE)
cv2.imshow('case', img_new)
k = cv2.waitKey(0)
cv2.destroyAllWindows()

1.最近邻插值

import cv2  
import numpy as np 
import time 
from traitlets.config.application import catch_config_error
def function(img,dstHeight,dstWidth):  
    height,width,channels =img.shape  
    emptyImage=np.zeros((dstHeight,dstWidth,channels),np.uint8)  
    sh=dstHeight/height  
    sw=dstWidth/width 
    for i in range(dstHeight):  
        for j in range(dstWidth):  
            x=min(round(i/sh), height-1) 
            y=min(round(j/sw), width-1) 
            emptyImage[i,j]=img[x,y]  
    return emptyImage
if __name__ == '__main__':
    img_in = cv2.imread('1.jpg')
    start = time.time()
    dstHeight=200
    dstWidth=200
    img_out=function(img_in,dstHeight,dstWidth)  
    print ('cost %f seconds' % (time.time() - start))
    cv2.imshow('src_image', img_in)
    cv2.imshow('dst_image', img_out)
    cv2.waitKey()  

2.双线性插值

# coding=utf-8
import cv2
import numpy as np
import time
def resize(src, new_size):
    dst_w, dst_h = new_size # 目标图像宽高
    src_h, src_w = src.shape[:2] # 源图像宽高
    if src_h == dst_h and src_w == dst_w:
        return src.copy()
    scale_x = float(src_w) / dst_w # x缩放比例
    scale_y = float(src_h) / dst_h # y缩放比例
    # 遍历目标图像,插值
    dst = np.zeros((dst_h, dst_w, 3), dtype=np.uint8)
    for n in range(3): # 对channel循环
        for dst_y in range(dst_h): # 对height循环
            for dst_x in range(dst_w): # 对width循环
                # 目标在源上的坐标
                src_x = (dst_x + 0.5) * scale_x - 0.5  #定位像素点:中心对齐
                src_y = (dst_y + 0.5) * scale_y - 0.5
                # 计算在源图上四个近邻点的位置
                src_x_0 = int(np.floor(src_x))
                src_y_0 = int(np.floor(src_y))
                src_x_1 = min(src_x_0 + 1, src_w - 1)
                src_y_1 = min(src_y_0 + 1, src_h - 1)
                # 双线性插值
                # 设(x,y)为插值点在源图像的坐标,待计算插值为z
                # 首先x方向上插值:相邻两点为(x0,y0)、(x1,y0),像素值分别为z0=f(x0,y0)、z1=f(x1,y0),从而有公式:z−z0x−x0=z1−z0x1−x0,从而插值z=x1−xx1−x0z0+x−x0x1−x0z1
                # 
                # 然后y方向上插值:以上得到的上方插值z
                # 记为ztop,同理可得下方插值为zbot,那么最后插值为:Z=y1−yy1−y0ztop+y−y0y1−y0zbot
                value0 = (src_x_1 - src_x) * src[src_y_0, src_x_0, n] + (src_x - src_x_0) * src[src_y_0, src_x_1, n]
                value1 = (src_x_1 - src_x) * src[src_y_1, src_x_0, n] + (src_x - src_x_0) * src[src_y_1, src_x_1, n]
                dst[dst_y, dst_x, n] = int((src_y_1 - src_y) * value0 + (src_y - src_y_0) * value1)
    return dst

if __name__ == '__main__':
    img_in = cv2.imread('1.jpg')
    start = time.time()
    img_out = cv2.resize(img_in, (600,600))
    print ('cost %f seconds' % (time.time() - start))

    cv2.imshow('src_image', img_in)
    cv2.imshow('dst_image', img_out)
    cv2.waitKey()

 

相关博客:

 

https://blog.csdn.net/smf0504/article/details/51303962

https://blog.csdn.net/xbinworld/article/details/65660665

https://blog.csdn.net/qq_29058565/article/details/52769497

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值