I'm having trouble getting a series of 3D "images" stored as a single 4D array. The code below is what I'm using to convert the 2D 640x512 images from the camera into a 3D array.
listImage = []
for i in range(492):
fname = '2019-12-04_11-59-12' + str(i) + '.fits'
fits_import = FITS.Read('Run 4\\' + fname)
listImage.append(fits_import[1])
arrayListImage = np.asarray(listImage)
The result, "arrayListImage", has a shape of [492, 640, 512]. Using the same code to read in 2 3D arrays results in an array of shape (2,). The type of "fits_import[1]" is a numpy.ndarray.
I expected this to return an array of shape [2, 492, 640, 512]. Why is this method not consistent in going to 4D?
Use np.stack instead of np.asarray:
arrayListImage = np.stack(listImage, axis=0)
Related
Please here is my code
mask = np.reshape(binarized_predict_masks[j], newshape=(input_img_size, input_img_size))
resized_mask = Image.fromarray(mask).resize((size, size))
resized_mask = np.asarray(resized_mask)
cropped_resized_mask = resized_mask[((size-w)//2):((size-w)//2 + w), ((size-h)//2):((size-h)//2 + h)]
cropped_resized_mask = np.reshape(cropped_resized_mask, newshape=(w, h, 1))
predicted_mask_path = img_path.replace('original_2D', 'mask_original_2D', 2)
# save txt file
predicted_mask_txt_path = predicted_mask_path.replace('.png', '.txt', 1)
np.savetxt(predicted_mask_txt_path, cropped_resized_mask, fmt='%.6f')
after execution, I receive the error: ValueError: Expected 1D or 2D array, got 3D array at #save txt file
I know that np.save txt only works for 1D and 2D arrays but I don't know how in view of this I should modify this box of code so that it works. Please help me
I have an array (of features extracted from an image) with the dimension (1,2048,7,7) and a word embedding (from fastText encoding) with the dimension (1,300). I want to concatenate these arrays to get an array with dimension (1,2348,7,7). How do I go about this?
It is unfortunate that concatenate doesn't do broadcasting. Fortunately, assignment does do broadcasting, so you can just build your result.
result = np.empty((1, 2348, 7, 7))
result[:,:2048] = image
result[:,2048:,:,:] = fast_text_encoding
I am trying to reshape separate arrays of three components each Y - U - V into a YUV array. The size of this array input 64x64 and then will be resized to 224x224. How should I do it correctly?
This is for my input data before training a CNN network. So, the following is the way I use to do it.
orgY = np.loadtxt(path)
orgU = np.loadtxt(path)
orgV = np.loadtxt(path)
originalYUV = np.dstack((orgY, orgU, orgV))
originalYUV = array(originalYUV).reshape(1, 224, 224, 3)
When I run it, this shows me: ValueError: cannot reshape array of size 1 into shape (1,224,224,3). How should I fix it? I am afraid the code is wrong when I use np.dstack as I even cannot call width = originalYUV.shape[0]
I have a numpy array Data of 1D of size [36*64]. Basically, I have 36, 8*8 images stored in a 1D array. Each image is stored in Height(8)*Width(8) format.
For e.g.: ith image is stored from Data[i*8*8 : (i*8*8 + 8*8)].
Now I want to make a tile of images from the given 36 images, i.e. 6 images stacked on top of each other. Example.
Basically, I want to transform my 1D Numpy array into a 2D array of images in the above mentioned format.
I would prefer answers with just using Numpy methods.
To convert your 1D array to 2D use reshape as shown with an example:
# Creating 36 images each of shape 8x8
initial_1D = np.random.randn(2304).reshape(36, 8, 8)
Collage can be formed using PIL. For clear understanding refer here Making a collage in PIL
If I understand you correctly, you can do it like this
# make example data
a = np.linspace(0, 36*64-1, 36*64)
print(a[:64])
print(a.shape)
# reshape 1D to 3D array
b = a.reshape(-1, 8, 8)
# look at first "image"
print(b[0])
If I did not understand you correctly, you need to put the -1, 8, 8 in a different order.
I have 4554 images in my numpy array X_train with the shape of the array as follows.
print(np.shape(X_train))
(4554,) # TOtal numbe of images
X_train[0].shape
(120, 120, 4) # Each image is 120x120 with 4 channels.
Now I want to reshape the array into (4554, 120, 120, 4), so that when I print
print(np.shape(X_train)
It gives me the shape (4554, 120, 120, 4) instead of (4554,).
I tried the following reshape method but it gives me error.
X_train=X_train.reshape((X_train.shape[0],X_train[0].shape))
Error: TypeError: 'tuple' object cannot be interpreted as an integer
You're looking for the numpy.stack() method.
If you have a list of 3d matrices, you can make a 4d matrix like so:
numpy.stack(your_list_of_training_data, axis=0)
See the documentation here for an explanation: https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.stack.html
To convert 2D array to 4D, try to flat 2D array first and then reshape as below , hope it works.
num_images = 4554
X_train_flat = [img.flatten() for img in X_train]
X_train_flat = np.array(X_train_flat)
X_train = X_train_flat.reshape(num_images, 120, 120, 4)