图像副本不会在OpenCV上显示相同的图像



我想显示一个图像的副本,但它不起作用。

def display_and_close(img):
cv2.imshow("test",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('assets/tests.jpeg',0)
width, height = img.shape
new_img = np.zeros((width, height))
new_img[:width, :height] += img[:width, :height]

display_and_close(new_img)
display_and_close(img)

我还尝试过这样迭代图像:

for i in range(img.shape[0]):
for j in range(img.shape[1]):
new_img[i][j] = img[i][j]

但它不能再次工作

您需要在Python/OpenCV中将黑色图像的dtype指定为uint8,否则它将默认为float。

所以更换

new_img = np.zeros((width, height))

带有

new_img = np.zeros((width, height), dtype=np.uint8)

还要注意,Numpy和shape使用y,x表示法(height,width(,而您正在使用(width,height(。但既然你也把形状反过来了,你就可以了。但你应该把两者都反过来。

最新更新