我有一个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)
答案 0 :(得分:0)
Numpy可以索引切片逻辑运算符
你为什么可以做类似的事情?
image[image.logical_and( image > -05 , image < 05 )]
https://docs.scipy.org/doc/numpy/reference/generated/numpy.logical_and.html