我有一个numpy数组,我从数组中减去一个常量值。如果需要的话,我希望值变为负数(而不是环绕或楼层为零)。然后我需要提取所有在零附近的数组值,并生成一个新的二进制数组/图像。因此,结果图像将显示接近于零的区域中的白色。你知道吗
我试图实现这个,但它的黑客,我不知道它是否正确。你能帮我做上面的事吗?你知道吗
# roi is a numpy array/image in Cielab colour space
swatch_colour = (255, 10, 30) # Cielab colour space
swatch_roi = np.full((roi.shape[0], roi.shape[1], 3), swatch_colour, dtype='int8')
int_roi = roi.astype('int8')
diff = np.subtract(int_roi, swatch_roi)
thresh = diff.copy()
# Get all pixels whose Cielab colour is close to zero
thresh[np.abs(thresh) < (12,6,12)] = 0
# the remaining pixels are greater than/less than the above threshold
thresh[np.abs(thresh) > (0,0,0)] = 255
thresh = thresh.astype('uint8')
# convert from 3 channels to 1 channel
thresh = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
# Invert the image so that the pixels that were close to zero are white
thresh = cv2.bitwise_not(thresh)
Numpy可以索引切片逻辑运算符
你为什么能这样做?你知道吗
https://docs.scipy.org/doc/numpy/reference/generated/numpy.logical_and.html
相关问题 更多 >
编程相关推荐