numpy 为什么当实际值改变时,'plt.imshow'显示的是原始图像?

6rqinv9w  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(107)

这段代码有什么问题?

它修改了值,但当它想给我显示实际修改后的图像时,它会再次给我显示原始图像。我做错了什么吗?
让我用图片来解释一下:
1-原始图像:


的数据
2-修改的图像:



从图像的直方图中可以明显看出,值发生了变化,但图片显示的是完全相同的图像。
这是显示此页面的代码。

# Load the image
img = cv2.imread(current_dir + "/img.jpg", cv2.IMREAD_GRAYSCALE)

plt.figure("Original Image", figsize=(6, 6))
plt.subplot(211)
plt.title("Original Image")
plt.imshow(img, "gray")
plt.subplot(212)
hist_full = cv2.calcHist([img], [0], None, [256], [0, 256])
plt.plot(hist_full)

img_min = np.amin(img)
img_max = np.amax(img)
gray_levels = np.arange(256, dtype=np.uint8)
gray_levels[:] = np.uint8(
    ((gray_levels - img_min) / (img_max - img_min)) * (30 - 0) + 0
)
img_shrunk = cv2.LUT(img, gray_levels)

plt.figure("Shrunk Original Image", figsize=(6, 6))
plt.subplot(211)
plt.title("Shrunk Original Image")
plt.imshow(img_shrunk, "gray")
plt.subplot(212)
hist_full = cv2.calcHist([img_shrunk], [0], None, [256], [0, 256])
plt.plot(hist_full)

字符串
问题出在哪里?

jaql4c8m

jaql4c8m1#

问题是imshow会自动设置可视化范围。它不会假设或期望输入在[0-255]范围内。
尝试

plt.imshow(img_shrunk, "gray", vmin=0,vmax=255)

字符串

相关问题