OpenCV 图像复制像素值操作更改原始图像

OpenCV image copy pixel value manipulation changes original image

将原始图像副本上的像素值 255 更改为 1 后,原始图像像素似乎也发生了变化,图像显示为全黑。

#Load the image 
img = cv.imread('img/test.png', 0)

ret2, binarizedImage = cv.threshold(img, 127, 255, cv.THRESH_BINARY+cv.THRESH_OTSU)

cv.imshow('Binarized', binarizedImage) #This one displayes the image as expected

imageCopy= binarizedImage 
imageCopy[imageCopy==255]=1

cv.imshow('Binarized2', binarizedImage) # This one shows only black image

你能告诉我如何在不影响原始图像像素值的情况下操作 imageCopy 像素吗?

ImageCopy 并不是真正的副本。而是对原始对象的引用。您将更改的值会影响具有两个引用的同一对象。您需要克隆原始图像对象。使用 .copy() 来实现。有关详细信息,请参阅 Clone an image in cv2 python