python-opencv实现缩放后以pad扩展

#coding=utf-8  
import cv2  
import numpy as np  
  
def ImageScale(img, scale):  
    size = img.shape  
    SIZE1 = size[0]  
    SIZE2 = size[1]  
    if scale > 1:  
        size1 = int(SIZE1 * scale) + 1  
        size2 = int(SIZE2 * scale)    #是否加1,根据具体图像尺寸的奇偶决定  
        #这里需要注意,对于w h 不等的图像, w h 的顺序值需要调整好.  
        img = cv2.resize(img, (size2, size1), interpolation = cv2.INTER_CUBIC)  #双三次线性插值法.  
        a1 = (size1 - SIZE1) / 2  
        b1 = size1 - a1  
        a2 = (size2 - SIZE2) / 2  
        b2 = size2 - a2  
        #print a1,b1,a2,b2  
        img = img[a1:b1, a2:b2]  
        #print img.shape  
    else:   #即scale<1  
        size1 = int(SIZE1 * scale)  
        size2 = int(SIZE2 * scale) + 1   # 是否加1,根据具体图像尺寸的奇偶决定  
        img = cv2.resize(img, (size2, size1), interpolation=cv2.INTER_CUBIC)  # 双三次线性插值法.  
  
    return img  
  
def mirrpadding(img, s1, s2):  
    orgsize = img.shape  
    size1 = orgsize[0]  
    size2 = orgsize[1]  
    a1 = (s1 - size1) / 2   #例:(129-103)/2 = 13  
    b1 = size1 - a1           #103-13  
    a2 = (s2 - size2) / 2     #例:(84-68)/2 = 8  
    b2 = size2 - a2           # 68-8  
    #print a1,b1,a2,b2  
    img1 = np.rot90((np.rot90(img[:a1, :a2].T)).T, 3)  
    print img1.shape, '1'  
    img2 = np.rot90(img[:a1,:].T)  
    print img2.shape, '2'  
    img3 = np.rot90((np.rot90(img[:a1, b2:].T)).T, 3)  
    print img3.shape, '3'  
    img4 = np.rot90(img[:,:a2].T, 3)  
    print img4.shape, '4'  
    img5 = np.rot90(img[:, b2:].T, 3)  
    print img5.shape, '5'  
    img6 = np.rot90((np.rot90(img[b1:, :a2].T)).T, 3)  
    print img6.shape, '6'  
    img7 = np.rot90(img[b1:, :].T)  
    print img7.shape, '7'  
    img8 = np.rot90((np.rot90(img[b1:, b2:].T)).T, 3)  
    print img8.shape, '8'  
    img = np.concatenate((img4, img, img5), axis=1)  #concatenate拼接函数,axis=1即在第二个维度上进行拼接.  
    img1 = np.concatenate((img1, img2, img3), axis=1)  
    img6 = np.concatenate((img6, img7, img8), axis=1)  
    img = np.concatenate((img1, img, img6), axis=0)  
    print img.shape, 'img'  
    cv2.imwrite('/.../img/mirror.png', img)  
  
  
  
#关于填充什么像素值,可以根据图像特点进行修改.  
def padding(img, s1, s2):   #s1 s2为原图的w h值  
    img_1 = img  
    orgsize = img.shape  
    size1 = orgsize[0]  
    size2 = orgsize[1]  
    a1 = (s1 - size1) / 2   #例:(129-103)/2 = 13  
    b1 = size1 - a1           #103-13  
    a2 = (s2 - size2) / 2     #例:(84-68)/2 = 8  
    b2 = size2 - a2           # 68-8  
    #print a1,b1,a2,b2  
  
    img1 = np.zeros([a1, a2],np.uint)  
    size = img1.shape  
    for i in range(size[0]):  
        for j in range(size[1]):  
            img1[i, j] = img[0, 0]   #padding为最上角的像素值.  
    print img1.shape, '1'  
    img2 = img_1[:a1,:]  
    size = img2.shape  
    for i in range(size[0]):  
        for j in range(size[1]):  
            img2[i, j] = img[0, 0]   #  得视情况而定...  
    print img2.shape, '2'  
    img3 = img_1[:a1, b2:]  
    size = img3.shape  
    for i in range(size[0]):  
        for j in range(size[1]):  
            img3[i, j] = img[0, 0]  #   
    print img3.shape, '3'  
    img4 = img_1[:,:a2]  
    size = img4.shape  
    for i in range(size[0]):  
        for j in range(size[1]):  
            img4[i, j] = img[0, 0]  #   
    print img4.shape, '4'  
    img5 = img_1[:, b2:]  
    size = img5.shape  
    for i in range(size[0]):  
        for j in range(size[1]):  
            img5[i, j] = img[0, 0]  #   
    print img5.shape, '5'  
    img6 = img_1[b1:, :a2]  
    size = img6.shape  
    for i in range(size[0]):  
        for j in range(size[1]):  
            img6[i, j] = img[0, 0]  #   
    print img6.shape, '6'  
    img7 = img_1[b1:, :]  
    size = img7.shape  
    for i in range(size[0]):  
        for j in range(size[1]):  
            img7[i, j] = img[0, 0]  #   
    print img7.shape, '7'  
    img8 = img_1[b1:, b2:]  
    size = img8.shape  
    for i in range(size[0]):  
        for j in range(size[1]):  
            img8[i, j] = img[0, 0]  #   
    print img8.shape, '8'  
    img = np.concatenate((img4, img, img5), axis=1)  #concatenate拼接函数,axis=1即在第二个维度上进行拼接.  
    img1 = np.concatenate((img1, img2, img3), axis=1)  
    img6 = np.concatenate((img6, img7, img8), axis=1)  
    img = np.concatenate((img1, img, img6), axis=0)  
    cv2.imwrite('.../img/padding.png', img)  
  
  
img = cv2.imread('.../img/1_1.png', -1)  
s1 = img.shape[0]  
s2 = img.shape[1]  
img1 = ImageScale(img, 0.8)  
cv2.imwrite('/home/chenjia/HWDB1.1tst_gnt/img/0.8.png', img1)  
# img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)  
# img = cv2.resize(img, (102,102), interpolation = cv2.INTER_CUBIC)  
# cv2.imwrite('/home/lenovo/2Tdisk/face/code/test/gray.jpg', img)  
mirrpadding(img1, s1, s2)  
padding(img1, s1, s2) 

猜你喜欢

转载自blog.csdn.net/xunan003/article/details/80341292
今日推荐