如何调整图像大小,但使用 python 保留像素的价值



我正在处理一些图像,我想将它们的大小从 1080 * 1920 调整为 480 * 640。我将每个像素分类到一个特定的类中,因此每个像素都有一个唯一的值。但是,如果我调整图像大小,像素的这些值会改变。

python
resized = cv2.resize(image, (640, 480), interpolation = cv2.INTER_AREA)
print(set(resized.flat)) --> a dict {0,1,2,3,4……,38,39,40}
print(set(image.flat)) --> a dict {0,10,40}
# image size is 1080 * 1920
# resized size is 480 * 640
desired_image = cv2.imread(desired_image_path,cv2.IMREAD_GRAYSCALE).astype(np.uint8)
print(set(desired_image.flat)) --> a dict {0,10,40}
# desired_image size is 480 * 640

我希望获得所需的图像,其大小为480 * 640而没有任何裁剪,并保持像素的值相同。现在我有了正确的大小,但像素的值变化很大。

如果我理解正确,您希望在不创建新的像素值的情况下调整图像大小。这可以通过将cv2.resizeinterpolation参数设置为INTER_NEAREST

resized = cv2.resize(image, (640, 480), interpolation = cv2.INTER_NEAREST)

来源: https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html#resize

最新更新