简体   繁体   English

使用cv2.imread加载图像时发生意外的图像修改

[英]Unexpected image modification when using cv2.imread for image loading

I am trying to do some modifications to the image ('some_image.jpeg'). 我正在尝试对图像('some_image.jpeg')进行一些修改。 For that reason I am loading it into to variables: image_org and image_mod. 因此,我将其加载到变量:image_org和image_mod中。 In image_mod i want to do modifications, image_org i want to keep unchanged for later comparizons. 我想在image_mod中进行修改,而image_org我想对以后的比较保持不变。 After doing some changes to the image_mod (basically drawing some lines on it). 对image_mod进行一些更改后(基本上在上面绘制一些线)。 I am creating a new image which is a difference btween the modified one and the original one: image_diff = cv2.subtract(image_mod, image_org). 我正在创建一个新图像,该图像是修改后的图像和原始图像之间的区别:image_diff = cv2.subtract(image_mod,image_org)。 I calculate the difference into one number with: diff_num = cv2.sumElems(image_diff)[0] and save all 3 images into .png files. 我使用diff_num = cv2.sumElems(image_diff)[0]将差值计算为一个数,并将所有3张图像保存到.png文件中。 I am expacting to obtain: - image that is identical to original file (image_org) - image that has additional lines on it (image_mod) - image with lines only that were added to the image_mod (image_diff) - diff_num to be a number, rather big number However what i get: - image_org is changed and looks exactlz the same as image_mod - diff_num is equal to 0.0 我正努力获得:-与原始文件(image_org)相同的图像-在其上具有其他行的图像(image_mod)-仅将行添加到image_mod(image_diff)的图像-diff_num是一个数字,而不是大数字但是我得到的是:-image_org被更改,并且看起来与image_mod完全相同-diff_num等于0.0

I quess that i am making mistake in the first few lines of the code, however i can not understand how image_org is getting modified with my code. 我问我在代码的前几行中犯了错误,但是我不明白image_org是如何用我的代码修改的。 Please help how to fix it so i can get what i am expecitng to get. 请帮助如何解决它,这样我就可以得到我正在获得的东西。

import cv2

image_org = cv2.imread('some_image.jpeg',0)
image_mod = image_org

for i in range(10):
    cv2.line(image_mod,(100+i*5,0),(0+i*5,150),(255),1,16)

image_diff = cv2.subtract(image_mod, image_org)
diff_num = cv2.sumElems(image_diff)[0]

cv2.imwrite('test_org.png',image_org)
cv2.imwrite('test_mod.png',image_mod)
cv2.imwrite('test_dif.png',image_diff)

print(diff_num)

image_org and image_mod are just two names for the same object. image_orgimage_mod只是同一对象的两个名称。

You need to make a copy of your original image: 您需要复制原始图像:

image_mod = image_org.copy()

image_mod will then be a different object. 然后image_mod将是一个不同的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM