相关疑难解决方法(0)

用列表的值替换numpy索引数组的值

假设你有一个numpy数组和一个列表:

>>> a = np.array([1,2,2,1]).reshape(2,2)
>>> a
array([[1, 2],
       [2, 1]])
>>> b = [0, 10]
Run Code Online (Sandbox Code Playgroud)

我想替换数组中的值,以便将1替换为0,将2替换为10.

我在这里发现了类似的问题 - http://mail.python.org/pipermail//tutor/2011-September/085392.html

但使用此解决方案:

for x in np.nditer(a):
    if x==1:
        x[...]=x=0
    elif x==2:
        x[...]=x=10
Run Code Online (Sandbox Code Playgroud)

给我一个错误:

ValueError: assignment destination is read-only
Run Code Online (Sandbox Code Playgroud)

我想这是因为我无法写入一个numpy数组.

PS numpy数组的实际大小是514乘504,列表是8.

python indexing numpy

19
推荐指数
3
解决办法
6万
查看次数

如何在Python中随机化图像像素

我是计算视觉和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 …
Run Code Online (Sandbox Code Playgroud)

python image image-processing

3
推荐指数
1
解决办法
5241
查看次数

Keras自定义图像预处理函数引发Value Error“输出数组为只读”

我想在 Keras 中使用一些自定义的图像预处理函数和 ImageDataGenerator 函数。例如,我的自定义函数如下所示:

def customizedDataAugmentation(x):
   choice = np.random.choice(np.arange(1, 4), p=[0.3, 0.3, 0.4])
   if choice==1:
       x = exposure.adjust_gamma(x, np.random.uniform(0.5,1.5))
   elif choice==2:
       ix = Image.fromarray(np.uint8(x))
       blurI = ix.filter(ImageFilter.GaussianBlur(np.random.uniform(0.1,2.5)))
       x = np.asanyarray(blurI)
   return x
Run Code Online (Sandbox Code Playgroud)

使用方法如下:

        self.train_datagen = image.ImageDataGenerator(
            rescale=1./255,
            zoom_range=0.15,
            height_shift_range=0.1,
            horizontal_flip=True,
            preprocessing_function=customizedDataAugmentation
        )
Run Code Online (Sandbox Code Playgroud)

但是,当我开始训练时,它跳出这个错误:

Traceback (most recent call last):
File "/home/joseph/miniconda3/envs/py27/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
File "/home/joseph/miniconda3/envs/py27/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
File "/home/joseph/miniconda3/envs/py27/lib/python2.7/site-packages/keras/utils/data_utils.py", line 560, in data_generator_task
    generator_output = next(self._generator)
File "/home/joseph/miniconda3/envs/py27/lib/python2.7/site-packages/keras/preprocessing/image.py", line 1039, in next
    x …
Run Code Online (Sandbox Code Playgroud)

python keras valueerror

0
推荐指数
1
解决办法
3398
查看次数

标签 统计

python ×3

image ×1

image-processing ×1

indexing ×1

keras ×1

numpy ×1

valueerror ×1