0

I have a RGB image of size (227,227,3) and one grayscale image of size (227,227). Now I want to replace RGB planes of the image by the grayscale image. Below I have the code. Now, the issue with code is imgNew is getting overridden even though I am not changing it's value and getting wrong images for imgR,imgG,imgB.

#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. 

I don't understand what's wrong?

talos1904
  • 952
  • 3
  • 9
  • 24

1 Answers1

4

You have this statement: imgB = imgNew which essentially makes imgB to be the same image as imgNew. So when you manipulate imgB in following lines it also changes imgNew. If you wanted imgB to be a copy of imgNew you have to explicitly use a copy function.

petyanca
  • 126
  • 1
  • 2