Issue
This is my first question here and it is about using numpy library in Python. I have a function which get three dimensional numpy array (np.array) with some x,y coordinates. The logic of that function - reordering this array from smaller to bigger coordinate
import numpy as np
def reorder(points):
# reshaping array
points = points.reshape((4, 2))
# creating empty output array
points_new = np.zeros((4, 1, 2), np.uint8)
# summing the values
add = points.sum(1)
# find difference
diff = np.diff(points, axis=1)
# with the smaller sum will be first, the maximum will be last
points_new[0] = points[np.argmin(add)]
points_new[3] = points[np.argmax(add)]
# the smaller difference will be the second, the max difference - the third
points_new[1] = points[np.argmin(diff)]
points_new[2] = points[np.argmax(diff)]
return points_new
But after using this function
input_data = np.array([[[ 573, 148]], [[ 25, 223]], [[ 153, 1023]], [[ 730, 863]]])
output_data = reorder(data)
I got the strange result
np.array([[[ 25, 223]], [[ 61, 148]], [[153, 255]], [[218, 95]]])
As you can see, in new array output
data
the data was changed, but should be contained the same values as input
data
I rewrite this function by using simple list
def reorder_by_lst(points):
# reshaping array
points = points.reshape((4, 2))
# summing the values
add = points.sum(1)
# find difference
diff = np.diff(points, axis=1)
# with the smaller sum will be first, the maximum will be last
a = points[np.argmin(add)]
d = points[np.argmax(add)]
# the smaller difference will be the second, the max difference - the third
b = points[np.argmin(diff)]
c = points[np.argmax(diff)]
lst = [a, b, c, d]
return np.array(lst)
And after using it
input_data = np.array([[[ 573, 148]], [[ 25, 223]], [[ 153, 1023]], [[ 730, 863]]])
output_data = reorder_by_lst(data)
I got this result
np.array([[ 25, 223], [ 730, 863], [ 573, 148], [ 153, 1023]])
Now the results are the same, only dimensional should be fixed. It seems like a bug, but maybe it is deep feature which I dont know. I need a comment from professional community.
P.S. Python version - 3.10, Numpy version - 1.23.5
Solution
You set your points_new
array to be of type np.unint8
, which is an unsigned 8-bit integer. The range of values is therefore 0 to 255, which can be checked as follows:
>>> np.iinfo(np.uint8)
iinfo(min=0, max=255, dtype=uint8)
Your points
array has values that exceed 255, so they are overflowing and wrapping back around. That is why you are getting "new data". You can check to see what it is actually working with by casting the points
array to be np.uint8
as well.
>>> np.array([[[573, 148]],
[[25, 223]],
[[153, 1023]],
[[730, 863]]],
dtype=np.uint8)
array([[[ 61, 148]],
[[ 25, 223]],
[[153, 255]],
[[218, 95]]], dtype=uint8)
The reason your list version worked is because you never set the return array to be of type np.uint8
, so it defaulted to np.int32
.
Answered By - jared
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.