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

Eun*_* TT 3 python opencv image python-imaging-library

我正在处理一些图像,我想将它们的大小从 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
Run Code Online (Sandbox Code Playgroud)

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

Sar*_*tus 6

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

resized = cv2.resize(image, (640, 480), interpolation = cv2.INTER_NEAREST)
Run Code Online (Sandbox Code Playgroud)

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