python - 从python中的灰度热图创建多色阈值图像
问题描述
我有一个灰度图像,并希望在 4 个值范围内(0.035、0.7、0.75)对它们进行阈值处理,以 4 种不同的颜色显示。我需要将结果保存为 UINT8 格式的图像。灰度图像信息如下:
print(type(grads))
print(grads.shape)
print(grads.min())
print(grads.max())
cv2.imshow('1_grads', grads)
cv2.waitKey()
### OUTPUT
<class 'numpy.ndarray'>
(512, 512)
0.0
1.0
我尝试了以下方法:
thresh_map = Image.new('RGB', grads.shape, color='white')
thresh_map = np.where(grads < 0.035, (0, 0, 0), (0, 0, 0))
thresh_map = np.where(0.035 < grads < 0.7, (0, 0, 255), (0, 0, 0))
thresh_map = np.where(0.7 < grads < 0.75, (0, 255, 0), (0, 0, 0))
thresh_map = np.where(0.75 < grads, (0, 255, 0), (0, 0, 0))
这将返回此错误:
ValueError: operands could not be broadcast together with shapes (512,512) (3,) (3,)
我通过使用for循环并一一粘贴像素值以某种方式解决了这个问题。但考虑到我将把它应用于大约 4000 张图像这一事实,它并不好并且需要永远。
thresh_map = Image.new('RGB', grads.shape, color='white')
blac = Image.new('RGB', (1, 1), color='black')
blue = Image.new('RGB', (1, 1), color='blue')
redd = Image.new('RGB', (1, 1), color='red')
gree = Image.new('RGB', (1, 1), color='green')
for i in range(grads.shape[0]):
for j in range(grads.shape[1]):
print(i, j)
if grads[i, j] < 0.035:
thresh_map.paste(blac, (i, j))
elif .035 < grads[i, j] < 0.7:
thresh_map.paste(redd, (i, j))
elif 0.7 < grads[i, j] < 0.75:
thresh_map.paste(gree, (i, j))
elif 0.75 < grads[i, j]:
thresh_map.paste(blue, (i, j))
np_thresh_map = np.asarray(thresh_map)
cv2.imshow('1_thresh', np_thresh_map)
cv2.waitKey()
有没有更复杂和有效的方法来做到这一点?
解决方案
这是我使用 NumPy 的布尔数组索引的解决方案。代码应该很简单。如果没有,请询问。然后,我将提供更多解释。
import cv2
import numpy as np
# Set up some random grayscale image; and sort for better visualization
image = np.sort(np.random.rand(300, 400))
# Given thresholds
thresholds = [0.035, 0.7, 0.75]
# Expand threshold with boundaries
thresholds = np.concatenate(([0], thresholds, [1]))
# Initialize output, and map colors
map = np.zeros((image.shape[0], image.shape[1], 3), np.uint8)
colors = np.array([[255, 0, 0], [128, 128, 0], [0, 0, 255], [0, 255, 0]])
# Iterate ranges; find proper pixel indices; set proper color in map at these indices
for i in range(len(thresholds)-1):
idx = (thresholds[i] <= image) & (image < thresholds[i+1])
map[idx, :] = colors[i, :]
# Show image and map
cv2.imshow('image', image)
cv2.imshow('map', map)
cv2.waitKey(0)
cv2.destroyAllWindows()
image
可能看起来像这样:
并且,对应的map
看起来像这样:
希望有帮助!
推荐阅读
- docker - 在 Windows 10 上构建 Linux Docker 映像:“exec 格式错误”
- algorithm - 计算将一行球放入盒子中的方法数量的算法
- c++ - 创建 1 到 20 之间的 5 个唯一整数的数组的算法
- javascript - FirebaseError:collection() 的第一个参数应为 CollectionReference、DocumentReference 或 FirebaseFirestore
- active-directory - 2021 年在 Windows 10 上安装 Active Directory 用户和计算机 (ADUC)
- python - 如何将多个视图合并为一个?
- java - 在 Java 中返回一个 int 数组
- java - 如何在 apache POI 中使用 office.js 提供的开放 XML 字符串创建 word 文件?
- r - 将长度为一的列表数据框字段转换回数据框列
- spring-security - 如何使用spring boot + msal和angular解决“已被CORS政策阻止”?