cv.fillPoly generating zero arrays, not reading inputs - arrays

I have
blank = np.zeros(shape = im.shape, dtype = np.float32)
which generates
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)
I have [label2poly[label]] which is dtype('int32')
[array([[ 716, 1],
[ 710, 281],
[ 727, 322],
[ 756, 369],
[ 793, 399],
[ 863, 406],
[ 952, 416],
[ 978, 412],
[ 416, 1]])]
When I try cv2.fillPoly(blank, [label2poly[label]], 255) it outputs
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)`
where it should have been something like
array([[ 0., 0., 0., ..., 0., 0., 0.],
[ 0., 0., 255., ..., 0., 0., 0.],
[ 0., 0., 255., ..., 0., 0., 0.],
...,
[ 0., 255., 255., ..., 255., 255., 255.],
[ 0., 255., 255., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32)
I am trying to create multiple masks. Would appreciate any help.

Initialization of blank:
blank = np.zeros(shape=[5, 5], dtype=np.float32)
print(blank)
Output:
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
Initialization of label followed by a call to fillPoly() which uses is as a mask to fill blank with 255 at specific positions:
label = np.array([[[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]], dtype=np.int32)
cv2.fillPoly(blank, [label], 255)
print(blank)
Output:
[[ 0. 0. 0. 0. 0.]
[ 0. 255. 255. 255. 0.]
[ 0. 255. 255. 255. 0.]
[ 0. 255. 255. 255. 0.]
[ 0. 0. 0. 0. 0.]]

Related

How to explain in Numpy indexing an array with a list?

I've been trying to use Python lists as part of indices into Numpy arrays, and I'm seeing behavior that I don't understand.
The first example:
>>> a=np.zeros((5,5))
>>> a[0,[2,4]]=1
>>> a
array([[0., 0., 1., 0., 1.]
[0., 0., 0., 0., 0.]
[0., 0., 0., 0., 0.]
[0., 0., 0., 0., 0.]
[0., 0., 0., 0., 0.]])
The second example:
>>> a=np.zeros((5,5))
>>> a[[1,3],[2,4]]=1
>>> a
array([[0., 0., 0., 0., 0.]
[0., 0., 1., 0., 0.]
[0., 0., 0., 0., 0.]
[0., 0., 0., 0., 1.]
[0., 0., 0., 0., 0.]])
In the first example a[0,[2,4]], the first index is a scalar 0, the second a list.
It appears to me that the first index is treated as a specification of row, the second as column, and the first gets broadcast over the second to yield two row/column addresses [0,2] and [0,4].
In the second example a[[1,3],[2,4]], the first index is a list of rows, the second index is a list of columns, and they appear to be combined (broadcast?) to yield two row/column addresses [1,2] and [3,4].
Can someone help me better understand how Numpy array addressing works? I'm not sure what to Google for.

How can I generate a list of node features as Numpy array

For a Graph based machine learning task, I need to input a Numpy array in the following format. It's actually an array of array. I tried several options like vstack, concatenate, etc. but nothing works for me.
This is the format I'm having now.
array([[[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]],
[[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]]], dtype=float32)
The shape of this input is:
(2, 3, 5)
But, I need to have a NumPy array with the shape of:
(2,)
That means the input should be an array of arrays as below.
array([array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 0., 0., 1.]], dtype=float32),
array([[1., 0., 0., 0., 0.],
[1., 0., 0., 0., 0.],
[0., 0., 0., 0., 1.]], dtype=float32)], dtype=object)
Can anyone tell me how to convert the current input (shape: (2, 3, 5)) into the expected input (shape: (2,))?
Thanks

Arbitrary matrix or array size in Swift

I am familiar with creating MxN matrices in Python using NumPy such as:
In [1]: import numpy as np
In [2]: np.zeros((10,5))
Out[2]:
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])
In Swift, SIMD can create small matrices and perform operations on them (see below) but it appears to be limited to a max size of a 4x4 matrix.
import simd
let mat = float2x2([[1,2],[3,4]])
mat[0]*9
Is there a way to define an arbitrary sized matrix of MxN dimensions in Swift and perform operations on that matrix?
If by "matrix", you mean an "array of arrays" then you can use the following syntax:
var matrix = Array(count: 10, repeatedValue: Array(count: 5, repeatedValue: 0.0))
This creates a 10 x 5 matrix of zeros.

plot selected rows of numpy array

Consider a small numpy array:
array([[ 0., 1., 0., 1., 0., 0., 0., 0., 0., 1.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 18., 15., 25., 0., 0., 0.],
[ 0., 0., 0., 23., 19., 20., 20., 0., 0., 0.],
[ 0., 0., 20., 22., 26., 23., 18., 0., 0., 0.],
[ 0., 0., 0., 23., 16., 20., 13., 0., 0., 0.],
[ 0., 0., 0., 0., 18., 20., 18., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 1., 0., 0., 0., 0., 0.]])
I would like to plot, let say from the row number 3 to the row number 6, i.e. a section of my numpy array(I am coming from matlab backgroud). How could I loop this? or How could I plot multiple rows of my numpy array in the same graph?
So far I have tried; I define an arbitrary x:
x = np.arange(0,10)
then If I use
plt.plot(x,data[3,:])
to plot the third row and It does fine. The problem arises if I try:
plt.plot(x,data[3:4,:])
I get the error "x and y must have same first dimension", which I understand because he stacks row number 3 and row number 4 together, so that x and y do not have the same dimension. How can I overcome that?
Thank you
As the error implies, your data.shape = (1,10) is inconsistent with your input x.shape = (10,). To solve this problem you can just transpose your data using .T, i.e.
plt.plot(x, data[3:4,:].T)
Also, keep in mind that data[3:4,:] is the same as data[3,:], you will need to use data[3:5,:] to get the 3rd and 4th rows, for example.
Just a better application of psuedocubi's answer.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,10)
y = np.array(YOUR DATA HERE)
plt.plot(x,a[3:4].T,'r--',label="x vs y1") #CONTAINS YOUR 3RD ROW
plt.plot(x,a[4:5].T,'g--',label="x vs y2") #CONTAINS YOUR 4TH ROW
plt.plot(x,a[5:6].T,'b--',label="x vs y3") #CONTAINS YOUR 5TH ROW
plt.legend(loc='best')
plt.xlabel("x")
plt.ylabel("y")
plt.show()
The x here as been plotted with your own data!
You can try:
for i in range(3):
plt.plot( x , data[ i , : ] )
plt.show()
If you want a range of rows ,for example from 3 to 6 , you can use:
range(3,7,1) , where 1 is the step , 3 is the starting row and 7 is the last row we want to plot (6 ) plus one

sum elements of a list in numpy

I have this list:
sections = [array([ 22.]),
array([ 26., 16., 23., 0., 20., 23., 0., 19.]),
array([ 0., 0., 0., 0., 20., 0., 0., 18., 18., 0., 0.,
0., 23., 20., 20., 15.]),
array([ 20., 0., 0., 18., 0., 13., 0., 0., 0., 0., 0.,
0., 25., 18., 0., 0., 0., 0., 0., 0.]),
array([ 0., 0., 3., 3., 3., 0., 0., 2., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 4., 0.]),
array([ 0., 0., 3., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0.]),
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0.]),
array([ 0., 0.])]
I want to sum the elements of each array and have an output array that looks like this:
[22,127,134,94,15,3,0,0]
If I try to convert the list into array and then iterate over it like this:
sec = np.asarray(sections) # covert list into array
photoel_sp = [] # This will contain the data at different radius
start = 0
for end in np.arange(sec.size):
photoel_sp.append(sum(sec[start:end]))
start = end
I get an error : setting an array element with a sequence. Why is that? I guess I could iterate directly over the list (without convert it into an array), how can I do that? thank you
There are a few ways to do this such as:
sums = [sum(section) for section in sections]
sums = map( sum, sections)

Resources