简体   繁体   English

在opencv python中无需更改即可更改图像的像素值

[英]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 = imgimgimg1仍然指向同一个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.

相关问题 如何在没有for循环的情况下更改python(opencv)中的像素值 - how to change pixel values in python (opencv) without for loops 使用 opencv 获取由椭圆包围的图像区域中的像素坐标和像素值 python - Getting pixel coordinates and pixel values in image region bounded by an ellipse using opencv python FFMPEG 在不修改的情况下读取和保存png时更改像素值 - FFMPEG changes pixel values when reading and saving png without modification 如何将图像的像素值复制到另一个 python 变量以使 python 变量将图像保存在 opencv - how to copy pixel values of an image to another python variable to make that python variable hold the image in opencv 使用 opencv、numpy 和 python 在图像中搜索像素 - Searching for a pixel in an image using opencv, numpy and python OpenCV Python-检查图像中的特定像素值 - Opencv python - check if particular pixel value in image 如何在 python OpenCV 中逐个像素地有效循环图像? - How to efficiently loop over an image pixel by pixel in python OpenCV? Python:图像显示中的像素值? - Python: Pixel values in image display? 使用imwrite opencv python函数在写入相同图像后更改像素值 - Changing of pixel values after writing the same image using imwrite opencv python function 旋转图像而不平均像素值 - Rotate an image without averaging pixel values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM