Numpy:调整灰度图像的大小,最后一个维度为np数组

时间:2016-12-03 14:46:10

标签: python arrays numpy

我有一张(3000,3000)的灰度2图像,我使用np.resize将其调整为(128,128,1),并使用np.stack将它们叠加,以生成4D np数组(2,128,128,1)。

但是,我意识到最后一个维度是整数形式。如何将图像调整为特定尺寸,并以np数组形式保留最后一个尺寸(通道数,灰度为1),而不是整数形式?

from scipy.misc import imread
import numpy as np

image = imread('myimage.jpg')
image = np.resize(image, (128, 128, 1))

image2 = imread('myimage2.jpg')
image2 = np.resize(image2, (128, 128, 1))

images = np.stack((image, image2), axis = 0) #Shape of (2, 128, 128, 1).

#Problem: Is there a function that resizes in this fashion but detects the image as a grayscale one, which allows for the last dimension to be in np array?

当我print type(images[0, 0, 0, 0])时,我得到一个np.uint8。但是,是否可以将其转换为np数组?因为目前,如果数字保持为整数,我会收到TypeError: 'int' object has no attribute '__getitem__'错误。

我正在寻找的是是否有一个功能可以调整(3000,3000)图像的大小:

[0.0, 0.0 .........]
[0.0, 0.0 .........]
...

#into something like:
[[0.0], [0.0], [0.0]....]
[[0.0], [0.0], [0.0]....]
...

之后我可以将图像堆叠在一起以创建一个4D np数组 (N,H,W,C)形状。 (图像数量为N,高度和宽度为H和W,像素值通道数为C)。

导致错误的代码是:

network = input_data(shape = [None, 256, 256, 1], name = 'input')
network = conv_2d(network, 32, 3, activation = 'relu', regularizer = 'L2')
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = fully_connected(network, 128, activation = 'tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 1, activation = 'softmax')
network = regression(network, optimizer = 'adam', learning_rate = '0.001', name = 'target')


model = tflearn.DNN(network, tensorboard_verbose = None )
model.fit({'input': image_train}, {'target': 0}, n_epoch = 20, batch_size = 1,
          validation_set = ({'input': image_test}, {'target': 0}),
          snapshot_step = 100, show_metric = True, run_id = 'convnet_images')

这给了我错误:

Run id: convnet_images
Log directory: /tmp/tflearn_logs/
Exception in thread Thread-14:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/data_flow.py", line 186, in fill_feed_dict_queue
    data = self.retrieve_data(batch_ids)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/data_flow.py", line 221, in retrieve_data
    utils.slice_array(self.feed_dict[key], batch_ids)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/utils.py", line 187, in slice_array
    return X[start]
TypeError: 'int' object has no attribute '__getitem__'

0 个答案:

没有答案