I've got an image 2d array with each pixel containing rgb value (bgr in opencv i guess) and i'm trying to get the a new 2d array which has the sum of each pixel instead.
e.g.
start image:
shape: (1080,1920,3)
[[[255,255,255], [0,0,0]],
[[0,120,255], [0,255,0]]]
result:
shape: (1080,1920,1)
[[[765],[0]],
[[375],[255]]]
I'm sure there's a simple Numpy solution that I just do not know yet...
Any help would be greatly appreciated!
mono = rgb.sum(axis=2)
That produces a shape (1080,1920). If you really need it to have a third dimension, you can use reshape.
By the way, if you're really trying to produce monochrome, this is not the way to do it. There's a formula to convert RGB to mono, and OpenCV has tools to do it.
Are you sure it's a 2-d array? Usually image arrays are 3-d, with shape (height, width, n_channels). If you have an array like that, you can use the sum method on an array, summing across the channel axis.
eg.
In [1]: a = np.random.randint(0, 10, (2, 3, 4))
In [2]: a
Out[2]:
array([[[5, 1, 7, 0],
[7, 3, 1, 5],
[5, 7, 0, 2]],
[[5, 2, 0, 9],
[4, 7, 4, 4],
[0, 7, 1, 3]]])
In [3]: a.sum(axis=-1)
Out[3]:
array([[13, 16, 14],
[16, 19, 11]])
Related
I have a 3d array in shape (288,512,512) that 288 is the number of images and 512*512 is the width and height of the image. Also, there is a mask array of this image in shape (512,512,288). How can I convert the shape of the mask array to the shape of this image array? I reshaped the mask array in shape (288,512,512), but I plot any mask from this array, not found a correlation between this mask and the its corresponding image.
Reshape just keeps the bytes in the same order. You actually need to move pixels around.
mask = np.transpose(mask,(2,0,1))
You don't want to reshape the array, but rather swap the interpretation of the dimensions. Depending on which axis of the mask corresponds to rows, you will want either
mask.transpose()
OR
mask.transpose(2, 0, 1)
Here is a simple example that shows what reshape and transpose do to a small 2D array to help you understand the difference:
>>> x = np.arange(10).reshape(2, 5)
>>> x
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> x.reshape(5, 2)
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
>>> x.transpose()
array([[0, 5],
[1, 6],
[2, 7],
[3, 8],
[4, 9]])
As you can see, reshape changes the sizes of the axes, while transpose alters the strides. Usually, when you combine them, data ends up getting copied somewhere along the way.
I have created the large numpy array using numpy.memmap like below.
import numpy as np
memmapData = np.memmap('test.memmap', dtype='uint8', mode='w+', shape=(10000, 10000, 3))
Now how to read the array based on some XY coordinates. After reading, I need to draw a rectangle.
(i.e) X1=0,Y1=1000 X2=X1+500,Y2=Y1+500. So now how can I read the numpy array from test.memmap by above coordinates. In this how do I need to use numpy.memmap attribute offset .
Why do you need to use the offset? Can't you just index it like a regular array?
In [221]: memmapData = np.memmap('test.memmap', dtype='uint8', mode='w+', shape=
...: (10, 10, 3))
In [222]: memmapData[...]=np.arange(100).reshape(10,10)[:,:,None]
In [223]: memmapData[0,5,:]
Out[223]: memmap([5, 5, 5], dtype=uint8)
In [224]: memmapData[0,5:,:]
Out[224]:
memmap([[5, 5, 5],
[6, 6, 6],
[7, 7, 7],
[8, 8, 8],
[9, 9, 9]], dtype=uint8)
offset is a parameter you can supply when creating the map. It's fixed. You don't fiddle with it to access elements.
I'm working on a problem with image processing, and my data is presented as a 3-dimensional NumPy array, where the (x, y, z) entry is the (x, y) pixel (numerical intensity value) of image z. There are 100000 images and each image is 25x25. Thus, the data matrix is of size 25x25x10000. I am trying to convert this into a 2-dimensional matrix of size 10000x625, where each row is a linearization of the pixels in the image. For example, suppose that instead the images were 3x3, we would have the following:
1 2 3
4 5 6 ------> [1, 2, 3, 4, 5, 6, 7, 8, 9]
7 8 9
I am attempting to do this by calling data.reshape((10000, 625)), but the data is no longer aligned properly after doing so. I have tried transposing the matrix in valid stages of reshaping, but that does not seem to fix it.
Does anyone know how to fix this?
If you want the data to be aligned you need to do data.reshape((625, 10000)).
If you want a different layout try np.rollaxis:
data_rolled = np.rollaxis(data, 2, 0) # This is Shape (10000, 25, 25)
data_reshaped = data_rolled.reshape(10000, 625) # Now you can do your reshape.
Numpy needs you to know which elements belong together during reshaping, so only "merge" dimensions that belong together.
The problem is that you aren't respecting the standard index order in your reshape call. The data will only be aligned if the two dimensions you want to combine are in the same position in the new array ((25, 25, 10000) -> (625, 10000)).
Then, to get the shape you want, you can transpose. It's easier to visualize with a smaller example -- when you run into problems like this, always try out a smaller example in the REPL if you can.
>>> a = numpy.arange(12)
>>> a = a.reshape(2, 2, 3)
>>> a
array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]]])
>>> a.reshape(4, 3)
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
>>> a.reshape(4, 3).T
array([[ 0, 3, 6, 9],
[ 1, 4, 7, 10],
[ 2, 5, 8, 11]])
No need to rollaxis!
Notice how the print layout that numpy uses makes this kind of reasoning easier. The differences between the first and the second step are only in the bracket positions; the numbers all stay in the same place, which often helps when you want to think through shape issues.
I was wondering how I can attach two 3d numpy arrays in python?
For example, I have one with shape (81,81,61) and I would like to get instead a (81,81,122) shape array by attaching the original array to itself in the z direction.
One way is to use np.dstack which concatenates the arrays along the third axis (d is for depth).
For example:
>>> a = np.arange(8).reshape(2,2,2)
>>> np.dstack((a, a))
array([[[0, 1, 0, 1],
[2, 3, 2, 3]],
[[4, 5, 4, 5],
[6, 7, 6, 7]]])
>>> np.dstack((a, a)).shape
(2, 2, 4)
You could also use np.concatenate((a, a), axis=2).
How can I find the dimensions of a matrix in Python. Len(A) returns only one variable.
Edit:
close = dataobj.get_data(timestamps, symbols, closefield)
Is (I assume) generating a matrix of integers (less likely strings). I need to find the size of that matrix, so I can run some tests without having to iterate through all of the elements. As far as the data type goes, I assume it's an array of arrays (or list of lists).
The number of rows of a list of lists would be: len(A) and the number of columns len(A[0]) given that all rows have the same number of columns, i.e. all lists in each index are of the same size.
If you are using NumPy arrays, shape can be used.
For example
>>> a = numpy.array([[[1,2,3],[1,2,3]],[[12,3,4],[2,1,3]]])
>>> a
array([[[ 1, 2, 3],
[ 1, 2, 3]],
[[12, 3, 4],
[ 2, 1, 3]]])
>>> a.shape
(2, 2, 3)
As Ayman farhat mentioned
you can use the simple method len(matrix) to get the length of rows and get the length of the first row to get the no. of columns using len(matrix[0]) :
>>> a=[[1,5,6,8],[1,2,5,9],[7,5,6,2]]
>>> len(a)
3
>>> len(a[0])
4
Also you can use a library that helps you with matrices "numpy":
>>> import numpy
>>> numpy.shape(a)
(3,4)
To get just a correct number of dimensions in NumPy:
len(a.shape)
In the first case:
import numpy as np
a = np.array([[[1,2,3],[1,2,3]],[[12,3,4],[2,1,3]]])
print("shape = ",np.shape(a))
print("dimensions = ",len(a.shape))
The output will be:
shape = (2, 2, 3)
dimensions = 3
m = [[1, 1, 1, 0],[0, 5, 0, 1],[2, 1, 3, 10]]
print(len(m),len(m[0]))
Output
(3 4)
The correct answer is the following:
import numpy
numpy.shape(a)
Suppose you have a which is an array. to get the dimensions of an array you should use shape.
import numpy as np
a = np.array([[3,20,99],[-13,4.5,26],[0,-1,20],[5,78,-19]])
a.shape
The output of this will be
(4,3)
You may use as following to get Height and Weight of an Numpy array:
int height = arr.shape[0]
int width = arr.shape[1]
If your array has multiple dimensions, you can increase the index to access them.
You simply can find a matrix dimension by using Numpy:
import numpy as np
x = np.arange(24).reshape((6, 4))
x.ndim
output will be:
2
It means this matrix is a 2 dimensional matrix.
x.shape
Will show you the size of each dimension. The shape for x is equal to:
(6, 4)
A simple way I look at it:
example:
h=np.array([[[[1,2,3],[3,4,5]],[[5,6,7],[7,8,9]],[[9,10,11],[12,13,14]]]])
h.ndim
4
h
array([[[[ 1, 2, 3],
[ 3, 4, 5]],
[[ 5, 6, 7],
[ 7, 8, 9]],
[[ 9, 10, 11],
[12, 13, 14]]]])
If you closely observe, the number of opening square brackets at the beginning is what defines the dimension of the array.
In the above array to access 7, the below indexing is used,
h[0,1,1,0]
However if we change the array to 3 dimensions as below,
h=np.array([[[1,2,3],[3,4,5]],[[5,6,7],[7,8,9]],[[9,10,11],[12,13,14]]])
h.ndim
3
h
array([[[ 1, 2, 3],
[ 3, 4, 5]],
[[ 5, 6, 7],
[ 7, 8, 9]],
[[ 9, 10, 11],
[12, 13, 14]]])
To access element 7 in the above array, the index is h[1,1,0]