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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

what's that copy function?
imgNew.copy()
The following link explains the issue you are having. stackoverflow.com/a/5234279/947212

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.