[英]pixel values of image are changed without modification in opencv python
I tried to detect edges of an image. 我试图检测图像的边缘。 For that, I wrote the following code: 为此,我编写了以下代码:
import numpy as np
import cv2
import math
img = cv2.imread('download.jpg',0)
img1 = img
k = img.shape
i=1
x=k[0]
y=k[1]
print x,y
while(i<(x-2)):
j=1
while(j<(y-2)):
a = (int(img[i,j+1])-int(img[i,j]))
b = (int(img[i+1,j])-int(img[i,j]))
c = (a**2) + (b**2)
img1[i,j] = math.sqrt(c)
#img1[i,j] = a
j+=1
i+=1
i=1
print "img"
print img
print "img1"
print img1
print i,j
cv2.imshow("image1",img1)
cv2.imshow("image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
No where in the code img is modified. 在代码img中没有修改的地方。 Yet, at the end of the code, the pixel values of img are changed(which are same as img1). 但是,在代码末尾,img的像素值已更改(与img1相同)。 Can anybody explain me what Iam missing? 谁能解释我缺少的东西?
Reason is, assignment statements in Python do not copy objects, after you did img1 = img
, img
and img1
still point to the same Image object, you need to make a copy if you need to mutate the objects independently 原因是,Python中的赋值语句不会复制对象,在img1 = img
, img
和img1
仍然指向同一个Image对象之后,如果需要独立更改对象,则需要进行复制
You can also simply instantiate two Image objects 您还可以简单地实例化两个Image对象
img = cv2.imread('download.jpg', 0)
img1 = cv2.imread('download.jpg', 0)
Otherwise you'll need to copy the python object img
, eg using https://docs.python.org/2/library/copy.html 否则,您需要复制python对象img
,例如使用https://docs.python.org/2/library/copy.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.