image = cv2.imread('black.jpg')[:,:,0] # let's just take the red channel alone for brevity
print(image[75:85,155:165]) # print middle 10x10
# output: [[0,0,0,....],...]
我想从所有像素强度中减去 1。是的,我绝对不希望 0 变成 255。我只希望它们保持为 0。
print(image[75:85,155:165]-1)
# output: [[255,255,255,....],...]
print(np.array(image[75:85,155:165])-1)
# output: [[255,255,255,....],...]
print(np.array(image[75:85,155:165], dtype='float32')-1)
# output: [[-1.,-1.,-1.,....],...]
我可以在 .clip(0,255) 之后将最后一个转换回 uint8,但感觉这不是正确的做法。有没有一种方法可以直接执行此操作(无需强制转换,如果条件可能对并行处理效率不高)?
请您参考如下方法:
最好的方法是使用带有掩码的np.where:
grey_new = np.where(image == 0, 0, image - 1)
您可以在 this answer 中找到更多信息, 和 numpy.where文档。
