图像分块及拼接
- 问题
- python 代码
- 效果图
问题
图像分块及拼接代码中对图像分块不均匀,本文给出的代码,图像分块大小相同。改进方法:图像分块不均匀时,填充事情shape相同
python 代码
import numpy as np
import cv2 as cv
import matplotlib.pyplot as pltdef get_patch(img,patch_size):imgs = []shape_list = []h, w, n = img.shapenew_h, new_w = patch_size, patch_sizecol = int(w / patch_size) + 1row = int(h / patch_size) + 1patch_n = col * rowtop = 0for r in range(row):insert_zeros_row = np.empty((0, 0, 3))foot = top + new_hif foot > h:foot_gap = foot - hfoot = hinsert_zeros_row = np.zeros((foot_gap, new_w,3))left = 0for c in range(col):insert_zeros_col = np.empty((0, 0, 3))right = left + new_wif right > w:right_gap = right - wright = wif r==row-1:insert_zeros_col = np.zeros((new_h-foot_gap, right_gap, 3))else:insert_zeros_col = np.zeros((new_h, right_gap, 3))img_patch = img[top:foot, left:right]shape_list.append(img_patch.shape)if insert_zeros_col.shape[0] > 0:img_patch = np.hstack((img_patch, insert_zeros_col))insert_zeros_col = np.empty((0, 0, 3))if insert_zeros_row.shape[0] > 0:img_patch = np.vstack((img_patch, insert_zeros_row))left = left + new_wimgs.append(img_patch)top = top + new_hreturn imgs,shape_list, row, col
def jointImage(imgs,shape_list,h_n,w_n,):for h in range(h_n):# 按行拼接for w in range(w_n):# 按列拼接imgif w==0:imgs_c=np.array(imgs[h*w_n+w][:shape_list[h*w_n+w][0],:shape_list[h*w_n+w][1]])else:img_c=np.array(imgs[h*w_n+w][:shape_list[h*w_n+w][0],:shape_list[h*w_n+w][1]])imgs_c=np.hstack((imgs_c,img_c))# print(imgs[h * w_n + w].shape)if h==0:imgs_h=imgs_celse:imgs_h=np.vstack((imgs_h,imgs_c))return imgs_h
完整代码查看我的github [代码](https://github.com/c1h2e3n4y5u6n7/Image-processing-and-deep-learning/blob/master/patch.py)
效果图