Reputation: 133
I am trying to iterate the source array in order to change a few elements in the destination array, but I can’t get the correct indexes, I have incomprehensible offsets. In general, my task is to change the element if the conditions require it
import numpy as np
x = np.linspace(0,1000, 1000/50)
y = np.linspace(0,1000, 1000/50)
X,Y = np.meshgrid(x,y)
source = np.column_stack([X.ravel(), Y.ravel()]).astype(int)
destination = source.copy()
for x, i in np.ndenumerate(source):
# if smth:
destination[i] = np.array([x[0] + 10, x[1]])
I think I should not use the source array, but only iterate over the destination array(сannot be done using standard methods), tell me the correct solution, thanks in advance.
Current output:
[421 789]
[473 789]
[526 789]
[578 789]
[631 789]
[684 789]
Required output:
[431 789]
[483 789]
[536 789]
[588 789]
[641 789]
[694 789]
I’ll explain more simply, I have a grid, it has points, I need to shift the points, say 88, 89, 90, 10 pixels to the right, for this I need to have an array of source and destination (where these points are offset), enumerate most likely it doesn’t suit me, but the usual editing of an array like for x in destination: when editing x gives the desired result, but this does not apply to ndarray
for x, i in enumerate(destination):
inside = cv2.pointPolygonTest(cnt, (destination[x,0],
destination[x,1]), False)
if inside > 0:
cv2.circle(img, (destination[x,0], destination[x,1]), 10,
(255,0,0), 2)
destination[x] = np.array([destination[x,0] + 10, destination[x,1]])
# Contour(cnt)
[[550 42]
[600 42]
[690 273]
[640 273]]
As you understand, I need to shift everything that is circled in blue by 10 pixels
Upvotes: 1
Views: 1446
Reputation: 33137
What you are asking is not clear at all. How to do you want to modify the destination
?
Also, are you sure you need np.ndenumerate
and not enumerate
?
Here is a way to modify it based on the values that are in sources
.
At the position (index) x
, the destination will become equal to the x
element of the first column of source
+ 10 and equal to the x
element of the second column of source
.
for index, value in enumerate(source):
destination[index] = [source[index,0]+10, source[index,1]] # x+10, y same
Upvotes: 1
Reputation: 7353
From data creation to target-region modification, we can divide it into a three step process. The modification can be achieved using numpy
indexing along with conditional region selection by means of numpy.where
and numpy.logical_and
(if necessary).
import numpy as np
x = np.linspace(0,1000, int(1000/50))
y = np.linspace(0,1000, int(1000/50))
X,Y = np.meshgrid(x,y)
source = np.column_stack([X.ravel(), Y.ravel()]).astype(int)
destination = source.copy()
target_index = np.where(np.logical_and(destination[:,1]==789, destination[:,0]>=421))
destination[target_index]
Output:
array([[ 421, 789],
[ 473, 789],
[ 526, 789],
[ 578, 789],
[ 631, 789],
[ 684, 789],
[ 736, 789],
[ 789, 789],
[ 842, 789],
[ 894, 789],
[ 947, 789],
[1000, 789]])
3. Make changes to the target region
scope = destination[target_index]
scope[:,0] = scope[:,0] + 10
destination[target_index] = scope
destination[target_index]
Output:
array([[ 431, 789],
[ 483, 789],
[ 536, 789],
[ 588, 789],
[ 641, 789],
[ 694, 789],
[ 746, 789],
[ 799, 789],
[ 852, 789],
[ 904, 789],
[ 957, 789],
[1010, 789]])
Upvotes: 1