我对计算视觉和python还不熟悉,我真的不知道出了什么问题。我曾试图随机化RGB图像中的所有图像像素,但我的图像被证明是完全错误的,如下所示。有人能帮我解释一下吗?在
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()
你不是在洗牌像素,而是在之后使用
np.ravel()
和np.shuffle()
时对所有像素进行洗牌。在当你洗牌像素,你必须确保颜色,RGB元组,保持不变。在
这是随机浣熊,注意颜色。那里没有红色或蓝色。只有原来的,白色,灰色,绿色,黑色。在
{a1}
我删除的代码还有一些其他问题:
不要使用嵌套for循环,慢。
不需要使用
np.zeros
的预分配(如果您需要它,只需将rgbImg.shape
作为参数传递,不需要解压缩单独的值)将
plt.imshow(rdmImg)
改为plt.imshow(rdmImg.astype(np.uint8))
这可能与此问题有关https://github.com/matplotlib/matplotlib/issues/9391/
相关问题 更多 >
编程相关推荐