def XY(a):
XY = np.array([])
for i in a:
if (i[1] > 300) and (i[1] < 800) and (i[0] < 375) and (i[0] > 310):
XY = np.append(XY, [i])
return XY
a = array([[324, 664], [324, 665], [324, 666], [324, 667], [324, 668], [324, 669], [324, 670], [324, 671], [325, 664], [325, 665], [325, 666], [325, 667], [325, 668], [325, 669], [325, 670], [325, 671], [326, 664], [326, 665], [326, 666], [326, 667], [326, 668], [326, 669], [326, 670], [326, 671], [327, 664], [327, 665], [327, 666], [327, 667]])
Shape of the array 'a' is (num ,2). I want return array to be in the same shape like (some number, 2). But this function returns something like that (some number,1). How can I fix it? Thanks in advance.
CodePudding user response:
You need to append to ndarray with the same shape on axis 0
def XY(a):
XY = np.empty([0, 2], dtype=int)
for i in a:
if 300 < i[1] < 800 and 310 < i[0] < 375:
XY = np.append(XY, [i], axis=0)
return XY
arr = XY(np.array([[324, 664], [324, 665], [324, 666], [324, 667], [324, 668], [324, 669], [324, 670], [324, 671], [325, 664], [325, 665], [325, 666], [325, 667], [325, 668], [325, 669], [325, 670], [325, 671], [326, 664], [326, 665], [326, 666], [326, 667], [326, 668], [326, 669], [326, 670], [326, 671], [327, 664], [327, 665], [327, 666], [327, 667]]))
print(arr.shape)
print(arr)
Output
(28, 2)
[[324 664]
[324 665]
[324 666]
[324 667]
[324 668]
[324 669]
[324 670]
[324 671]
[325 664]
[325 665]
[325 666]
[325 667]
[325 668]
[325 669]
[325 670]
[325 671]
[326 664]
[326 665]
[326 666]
[326 667]
[326 668]
[326 669]
[326 670]
[326 671]
[327 664]
[327 665]
[327 666]
[327 667]]