I have data in a numpy vector that looks like this:
[[[1119 15]]
[[1125 27]]
[[1129 43]]
[[1131 62]]
[[1131 87]]
[[1141 234]]
...]
These are supposed to be a set of points that I can use to represent a curve, but instead each point [int, int] seems to be encapsulated inside another vector. IE: I have [[1 1]] instead of [1 1].
This data was given to me by an opencv function cv2.approxPolyDP
after I fed it a `contour', and I need to work with it. I think the function basically has given me what it thinks is a set of curves, but here each curve only contains one point [int int] which doesn't really make sense. A curve with one point is not a curve, it's a point.
Is there any way to convert [[int int]] to [int int] in this case?
Look at the shape of this array. It probably is (n, 1, 2)
.
reshape
it to (n,2)
. x.reshape(-1,2)
is a handy shortcut, saving you the work of determining n
. squeeze
also gits rid of the singular dimension.
Probably it is not optimal solution but you can do this:
import numpy as np
# example
a = np.array( [ [[1119, 15]], [[1125, 27]], [[1129, 43]] ] )
# convert
a = np.array( [ x[0] for x in a ] )
print a
[[1119 15]
[1125 27]
[1129 43]]
EDIT:
import numpy as np
a = np.array( [ [[1119, 15]], [[1125, 27]], [[1129, 143]] ] )
size = len(a)
a = a.reshape([size,2])
print a
[[1119 15]
[1125 27]
[1129 43]]
You can check if the second parameter in approxPolyDP is too big. Notice that the following code doesn't make it small all the time:
epsilon = 0.1*arcLength(contour,True)
polygon = approxPolyDP(contour, epsilon, True)
When the edge of contour gets noisy, the arc length of the contour returned by arcLength can be very very big, which yields a still very big epsilon after being multiplied by 0.1 and thus make approxPolyDP simplify the entire contour into one single point.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.