我想做一个仿射变换,然后使用最近邻插值,同时保持输入和输出图像的相同维数。例如,我使用缩放变换T=[[2,0,0],[0,2,0],[0,0,1]]。知道如何用最近邻填充黑色像素吗?我试着给他们邻居强度的最小值。例如,如果一个像素有邻居[55,22,44,11,22,55,23231],我给它最小强度的值:11。但结果并不明朗
import numpy as np
from matplotlib import pyplot as plt
#Importing the original image and init the output image
img = plt.imread('/home/left/Desktop/computerVision/SET1/brain0030slice150_101x101.png',0)
outImg = np.zeros_like(img)
# Dimensions of the input image and output image (the same dimensions)
(width , height) = (img.shape[0], img.shape[1])
# Initialize the transformation matrix
T = np.array([[2,0,0], [0,2,0], [0,0,1]])
# Make an array with input image (x,y) coordinations and add [0 0 ... 1] row
coords = np.indices((width, height), 'uint8').reshape(2, -1)
coords = np.vstack((coords, np.zeros(coords.shape[1], 'uint8')))
output = T @ coords
# Arrays of x and y coordinations of the output image within the image dimensions
x_array, y_array = output[0] ,output[1]
indices = np.where((x_array >= 0) & (x_array < width) & (y_array >= 0) & (y_array < height))
# Final coordinations of the output image
fx, fy = x_array[indices], y_array[indices]
# Final output image after the affine transformation
outImg[fx, fy] = img[fx, fy]
输入图像为:
缩放后的输出图像为:
如果您需要手动执行此操作,则只需在调整大小的图像中检测暗像素,并将其值更改为4个相邻像素的平均值(例如,这取决于所需的alghoritm) 见:东北邻、双线性、双三次等
您可以简单地使用opencv resize函数
它将一次性调整大小并填充空像素
more on cv2.resize
相关问题 更多 >
编程相关推荐