2017-05-01 79 views
0

我有一個尺寸爲(227,227,3)的RGB圖像和一個尺寸爲(227,227)的灰度圖像。現在我想用灰度圖像替換圖像的RGB平面。下面我有代碼。現在,代碼問題是imgNew即使我沒有改變它的價值和imgR,imgG,imgB錯誤圖像越來越重寫。python代碼問題,爲什麼變量被覆蓋?

#this is my rgb image 
imgNew = imresize(img[480/2 - 160 + r[l, 0]:480/2 + 160 + r[l, 0], 640/2 - 160 + r[m, 1]:640/2 + 160 + r[m, 1], :],(227,227)) 
# this is grascale 
imgDNew = imresize(imgx[480/2 - 160 + r[l, 0]:480/2 + 160 + r[l, 0], 640/2 - 160 + r[m, 1]:640/2 + 160 + r[m, 1]],(227,227)) 
# if I plot the images here both are fine. 
# Now replacing the planes one by one. 
imgR[:,:,0] = imgDNew 
imgR[:,:,1] = imgNew[:,:,1] 
imgR[:,:,2] = imgNew[:,:,2] 

imgG[:,:,1] = imgDNew 
imgG[:,:,2] = imgNew[:,:,2] 
imgG[:,:,0] = imgNew[:,:,0] 

imgB = imgNew 
imgB[:,:,2] = imgDNew 
imgB[:,:,0] = imgNew[:,:,0] 
imgB[:,:,1] = imgNew[:,:,1] 

#Now if I plot the images my original image is changed (imgNew) and imgR,imgG,imgB images are wrong. 

我不明白什麼是錯?

+1

http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list –

回答

4

您有此陳述:imgB = imgNew基本上使得imgBimgNew的圖像相同。所以當你在下面的行中操縱imgB時,它也會改變imgNew。如果你想imgBimgNew的副本,你必須明確地使用複製功能。

+0

什麼是複製功能? – talos1904

+0

'imgNew.copy()' –

+0

我使用numpy,所以我猜'np.copy()'會工作嗎? – talos1904