Related
I am trying to convert an 8-by-8 numpy array of binary format (0 represents black and 1 represents white). This is what I run:
from PIL import Image
data = im.fromarray(array) # array is an 8*8 binary numpy
data.save('dummy_pic.png')
But in the output I get a fully black square. Could anyone give me a hand please?
Black square is probably very dark gray, because you may have np.array with uint8 datatype, which has range of 0-255, not 0-1 like binary array. Try changing array datatype to bool, or scale values to 0-255 range.
Here is code snippet in which binary array is generated and displayed. If you scale by smaller value, circle will become slightly darker.
from PIL import Image
import numpy as np
# Generating binary array which represents circle
radius = 0.9
size = 100
x,y = np.meshgrid(np.linspace(-1,1,size),np.linspace(-1,1,size))
f = np.vectorize(lambda x,y: ( 1.0 if x*x + y*y < radius*radius else 0.0))
array = f(x,y)
# Solution 1:
# convert np.array to bool datatype
array = (array).astype(np.bool)
# Solution 2:
# scale values to uint8 maximum 255
array = ((array) * 255).astype(np.uint8)
img = Image.fromarray(array)
display(img)
Result: White circle
I am using Windows-Machine-Learning to convert my VideoFrame to a TensorFloat _input (shape: 1,3,256,192; RGB channels + image), load that into my onnx model and receive as _output another TensorFloat object (shape: 1,17,64,48; 17 detected objects + image).
Now my question: If I want to access that TensorFloat _output, currently the only way I know is to use _output.data.GetAsVectorView, which gives me a long 1d Vector and try to reorder that and figure out how the dimensions are ordered in there? Is there a clear rule that I can follow to understand how the 4D tensor is encoded in the 1D Vector? Alternatively, can I somehow access the different dimensions directly from the _output TensorFloat object, since using "Shape" shows me that it is a multidimensional array?
Please refer to the layout of Windows ML tensors here:
https://learn.microsoft.com/en-us/uwp/api/windows.ai.machinelearning.tensorfloat?view=winrt-20348
A tensor is a multi-dimensional array of values. A float tensor is a tensor of 32-bit floating point values.
The layout of tensors is row-major, with tightly packed contiguous data representing each dimension. The total size of a tensor is the product of the size of each dimension.
Consider:
Shape: [D1][D2][D3]...[DN]
Strides: [S1][S2][S3]...[SN]
Location: [A1][A2][A3]...[AN],
and you wish to compute the index at Location.
Then, you can assume that:
Sn = Dn+1 * Dn+2 * ... * Dn, (for n = 1...N-1)
SN = 1
So:
index = A1*S1 + A2*S2 + A3*S3 + ... + AN*SN
I want to convert each 2D array into RGB image and return this RGB image to another function how can I do that. I tried to do that by PIL and plt but it didn't work with me.I am trying for more than 2 weeks to find how I can do it.
Any help would be appreciated.
for c in [cD5,cD4,cD3,cD2,cD1]:
x = np.linspace(0, 3844, len(c))
f = interp1d(x, c)
result.append(f(common_x))
normalized_result = preprocessing.normalize(result)
I think this is solved here, remember if you want a RGB image you need 3 channels and that means a matrix with shape (NxMx3).
Convert 2d array to collored image in python
Cheers!
Edit:
This is an example of how you can stack your 2D arrays into a 3D one with the shape you need.
import numpy as np
#some random arrays i just created for test
r = np.array([3,3,3])
g = np.array([6,6,6])
b = np.array([9,9,9])
bl = np.array([12,12,12])
#create the stacked arrays
stacked = np.dstack((r,g,b,bl))
#check the shape
print(np.shape(stacked))
I want to compute the morlet wavelet of each row of 480X480 image. I have to save the output of the transform of each row which is a 2d array(matrix).
Then i will be taking the average all 480 2d matrices i have to get one final plot of the average.
clc;
close all;
clear all;
I=imread('lena.jpg');
J=rgb2gray(I);
%K=J(1:480)
%coefs = cwt(K,1:128,'morl','plot');
coefs = cell(480,1);
for i = 1:480
K=J(i,:);
coefs(i) = cwt(K,1:128,'morl');
end
Here i want to take the avg of the 480 coeff matrices. Here am getting the error
Conversion to cell from double is not possible.
Error in soilwave (line 12) coefs(i) = cwt(K,1:128,'morl');
Could anyone suggest a better method or tweaks to this.
Cell arrays are practical if you need to store elements that have inconsistent format or dimensions, but for what you are trying to do, a 3D array is easier to work with. Here is what I would do:
Preassign a 3D array:
coefs = zeros(128, size(J, 2), size(J,1));
then compute and populate the stack:
for ii = 1:size(J, 1)
K=J(ii,:);
coefs(:,:,ii) = cwt(K,1:128,'morl');
end
Finally, compute the mean along the third dimension:
MeanCoeff=mean(coefs, 3);
I am trying to create a 3D array of size 1000x1000x1000 with all the elements (corresponding to voxels) being zero and then assign a random value in the 2000 to 2001 range instead of 0 to some specific elements in the array and finally store it as a binary file.
The array named "coord" is the Nx3 matrix coordinates (x,y,z) of the points that I need them to be assigned the random value in the 3D array.))
I should mention that all the x,y,z values of the coordinate matrix are floating point numbers with: 0<=x<=1000 0<=y<=1000 0<=z<=1000
My aim is to export the 3D matrix in a binary format (other than MATLAB's default binary format) so that I can use it with other programs.
Here is what I've been up to so far:
load coord;
a=coord(:,1);
b=coord(:,2);
c=coord(:,3);
d=rand(1000,1)*2000;
dd = 0:2:1000;
[xq,yq,zq] = meshgrid(dd,dd,dd);
vq = griddata3(a,b,c,d,xq,yq,zq,'nearest');
h=figure;
plot3(a,b,c,'ro')
%=========================================%
fid=fopen('data.bin','w');
fwrite(fid,vq,'single');
fclose(fid);
In the above code a, b and c are the coordinates of each point and d is the corresponding intensity values for the desired range. While it is possible to create a 3D mesh (using meshgrid) and then interpolate the intensity values for mesh points (using griddata3), the final result (vq) would not be the actual points (ai,bi,ci) and corresponding intensities , but rather an interpolated set of points which is pretty useful for visualization purposes (for instance if you like to fit a 3D surface which fits through actual data).
I am simply trying to find a way to store the actual data-points and their intensities into a file and export it.
Any help is highly appreciated.
If you want to save to files that will allow importing into a visualization software, a series of Tiff files will most likely be convenient, i.e.
maxValue = 2000; % this is the maximum signal that can possibly occur
% according to your code
for z = 1:size(vq,3)
%# convert slice z to 16 bit
currentSlice = vq(:,:,z);
currentSlice = uint16(round(currentSlice/maxValue))
%# save to file
imwrite(currentSlice, sprintf('testImg_z%04i.tif',z),'tif');
end
Note that if you create a double array of dimensions 1000x1000x1000, you'll need 8GB of contiguous RAM.
How about something like:
%# 3D array
voxels = zeros([1000 1000 1000]);
%# round points coordinates, and clamp to valid range [1,1000]
load coords
coords = round(coords);
coords = min(max(coords,1),1000);
%# convert to linear indices
idx = sub2ind(size(voxels), coords(:,1), coords(:,2), coords(:,3));
%# random values in the 2000 to 2001 range
v = rand(size(idx)) + 2000;
%# assign those values to the chosen points
voxels(idx) = v;