如何在python中随机化图像像素 [英] How to randomize image pixels in python

查看:174
本文介绍了如何在python中随机化图像像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对计算视觉和python还是陌生的,我无法真正弄清楚出了什么问题.我尝试将RGB图像中的所有图像像素随机化,但事实证明我的图像完全错误,如下所示.有人可以说明一下吗?

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()

原始图片

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

image of my attempt in randomizing image pixel

推荐答案

您没有对像素进行改组,而是在随后使用np.ravel()np.shuffle()时对所有内容进行了改组.

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

在对像素进行混洗时,必须确保颜色(RGB元组)保持不变.

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.

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

There are some other issues with your code I removed:

  • 请不要使用嵌套的for循环,慢点.

  • Do not use the nested for loops, slow.

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

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)

这篇关于如何在python中随机化图像像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆