3

I am new to computational vision and python and I could not really figure out what went wrong. I have tried to randomize all the image pixels in a RGB image, but my image turned out to be completely wrong as seen below. Can someone please shed some light?

from scipy import misc

import numpy as np
import matplotlib.pyplot as plt

#Loads an arbitrary RGB image from the misc library
rgbImg = misc.face()     

%matplotlib inline

#Display out the original RGB image 
plt.figure(1,figsize = (6, 4))
plt.imshow(rgbImg)
plt.show()

#Initialise a new array of zeros with the same shape as the selected RGB image 
rdmImg = np.zeros((rgbImg.shape[0], rgbImg.shape[1], rgbImg.shape[2]))
#Convert 2D matrix of RGB image to 1D matrix
oneDImg = np.ravel(rgbImg)

#Randomly shuffle all image pixels
np.random.shuffle(oneDImg)

#Place shuffled pixel values into the new array
i = 0 
for r in range (len(rgbImg)):
    for c in range(len(rgbImg[0])):
        for z in range (0,3):
            rdmImg[r][c][z] = oneDImg[i] 
            i = i + 1

print rdmImg
plt.imshow(rdmImg) 
plt.show()

original image
image

image of my attempt in randomizing image pixel
image

Kelsey Jamie
  • 43
  • 1
  • 4

2 Answers2

5

You are not shuffling the pixels, you are shuffling everything when you use np.ravel() and np.shuffle() afterwards.

When you shuffle the pixels, you have to make sure that the color, the RGB tuples, stay the same.

from scipy import misc

import numpy as np
import matplotlib.pyplot as plt

#Loads an arbitrary RGB image from the misc library
rgbImg = misc.face()

#Display out the original RGB image
plt.figure(1,figsize = (6, 4))
plt.imshow(rgbImg)
plt.show()

# doc on shuffle: multi-dimensional arrays are only shuffled along the first axis
# so let's make the image an array of (N,3) instead of (m,n,3)

rndImg2 = np.reshape(rgbImg, (rgbImg.shape[0] * rgbImg.shape[1], rgbImg.shape[2]))
# this like could also be written using -1 in the shape tuple
# this will calculate one dimension automatically
# rndImg2 = np.reshape(rgbImg, (-1, rgbImg.shape[2]))



#now shuffle
np.random.shuffle(rndImg2)

#and reshape to original shape
rdmImg = np.reshape(rndImg2, rgbImg.shape)

plt.imshow(rdmImg)
plt.show()

This is the random racoon, notice the colors. There is not red or blue there. Just the original ones, white, grey, green, black.

enter image description here

There are some other issues with your code I removed:

  • Do not use the nested for loops, slow.

  • The preallocation with np.zeros is not needed (if you ever need it, just pass rgbImg.shape as argument, no need to unpack the separate values)

Joe
  • 6,758
  • 2
  • 26
  • 47
  • Thank you for your input! Do you mean I can just use np.zeros(rgbImg.shape)? And would you be kind enough to suggest scenarios on when do I need to use np.zeros? – Kelsey Jamie Sep 21 '18 at 14:31
  • 1
    Yes, you can use `np.zeros(rgbImg.shape)`, but there is an even more convenient way: `np.zeros_like(rgbImg)`. Your general idea was fine, `np.zeros` is often used to preallocate an array and fill it afterwards. But usually you are not adding single elements one by one. You could have some for-loop and insert whole rows or columns. – Joe Sep 22 '18 at 05:07
  • hi, I copied exactly this code but it only shows me the original racoon pic. It gives me the error `ValueError: assignment destination is read-only`... I use python2. – Aubrey Mar 06 '20 at 15:00
  • About ValueError: assignment destination is read-only: https://stackoverflow.com/a/54308748/7919597 – Joe Oct 28 '21 at 05:15
1

Change plt.imshow(rdmImg) into plt.imshow(rdmImg.astype(np.uint8))
This may related to this issue https://github.com/matplotlib/matplotlib/issues/9391/

mJace
  • 49
  • 1
  • 8
  • Your original code should print this message in your python console `Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).`. Google it , and you will find the answer for is problem. – mJace Sep 21 '18 at 05:44
  • Thank you for your input! I have tried to add in the line as you suggested, and the image does change, however, I might have the issue in the shuffling of pixels as the image does not turn out to contain only the colours of the original image. – Kelsey Jamie Sep 21 '18 at 14:15