【问题标题】:How to randomize image pixels in python如何在python中随机化图像像素
【发布时间】:2018-09-21 04:33:50
【问题描述】:

我是计算视觉和 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()

原图

我尝试随机化图像像素的图像

【问题讨论】:

    标签: python image image-processing


    【解决方案1】:

    当您使用np.ravel()np.shuffle() 之后,您不是在洗牌像素,而是在洗牌。

    当您洗牌像素时,您必须确保颜色、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()
    
    #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()
    

    这是随机浣熊,注意颜色。那里没有红色或蓝色。只是原始的,白色、灰色、绿色、黑色。

    我删除的代码还有一些其他问题:

    • 不要使用嵌套的 for 循环,慢。

    • 不需要np.zeros 的预分配(如果您需要它,只需将rgbImg.shape 作为参数传递,无需解压缩单独的值)

    【讨论】:

    • 感谢您的意见!你的意思是我可以只使用 np.zeros(rgbImg.shape) 吗?您能否就我何时需要使用 np.zeros 提出方案建议?
    • 是的,您可以使用np.zeros(rgbImg.shape),但还有一种更方便的方式:np.zeros_like(rgbImg)。您的总体想法很好,np.zeros 通常用于预分配数组并在之后填充它。但通常你不会一个一个地添加单个元素。你可以有一些 for 循环并插入整行或整列。
    • 嗨,我完全复制了这段代码,但它只显示了原始浣熊图片。它给了我错误ValueError: assignment destination is read-only...我使用python2。
    • 关于ValueError:赋值目标是只读的:stackoverflow.com/a/54308748/7919597
    【解决方案2】:

    plt.imshow(rdmImg) 更改为plt.imshow(rdmImg.astype(np.uint8))
    这可能与这个问题有关https://github.com/matplotlib/matplotlib/issues/9391/

    【讨论】:

    • 您的原始代码应在您的 python 控制台Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). 中打印此消息。谷歌一下,你会找到问题的答案。
    • 感谢您的意见!我已尝试按照您的建议添加该行,并且图像确实发生了变化,但是,我可能在像素的洗牌方面遇到了问题,因为图像并非只包含原始图像的颜色。
    猜你喜欢
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2015-08-10
    • 2020-10-03
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多