我正在尝试使用numpy将一组提供的坐标划分为Python 3中的几个存储桶。我有一个桶网格。见下文:
def partition(image, num_tiles):
"""Divide an image into a (num_tiles x num_tiles) grid and return the
partitioned input."""
# The object to return. Ignore - I am just trying to test 'draw' works currently.
partitioned_image = np.empty((num_tiles, num_tiles), dtype=object)
draw = []
# The input array contains coordinates of the form [xMin, xMax, yMin, yMax].
# This is because these are coordinates for bounding boxes around biological cells.
# When I say 'point(s)', I refer to a [xMn, xMx, yMn, yMx] array(s).
xMin = image[:,0]
xMax = image[:,1]
yMin = image[:,2]
yMax = image[:,3]
# The base to start searching from (not 0,0).
x_base = min(xMin)
y_base = min(yMin)
# max(?Max) - min(?Min) defines the entire range for the variable. Divide this
# range by the number of tiles, which is the number of ticks of the grid.
# E.g. range is 100, want a 10x10 grid, so we step along in steps of 10.
x_step = (max(xMax) - min(xMin)) // num_tiles
y_step = (max(yMax) - min(yMin)) // num_tiles
for i in range(num_tiles):
for j in range(num_tiles):
# Define the bottom-left point of the region of interest (a tile)
x_left = x_base + x_step * i
y_low = y_base + y_step * j
# Define the upper-right point of the region of interest
x_right = x_base + x_step * (i + 1)
y_high = y_base + y_step * (j + 1)
# Every point in image that is within the region gets added to the
# draw list. Remember, each point is of the form [xMn, xMx, yMn, yMx]
result = ((yMin >= y_low) & (yMax < y_high) &
(xMin >= x_left) & (xMax < x_right)).nonzero()[0]
for coordinates in image[result]:
draw.append(coordinates)
# I would want to add the actual points to my partitioned_input array
# here, in the corresponding tile. The above code for draw is *JUST TESTING*.
# Convert draw list to numpy array and check to see we got all the points.
draw = np.asarray(draw)
print(draw.shape == image.shape) # We do not. This is annoying.
# Below is the code for plotting. I just take the average of
# the xMin/yMin and xMax/yMax values for this.
draw_xAvg = np.mean(np.array([draw[:,0], draw[:,1]]), axis=0)
draw_yAvg = np.mean(np.array([draw[:,2], draw[:,3]]), axis=0)
image_xAvg = np.mean(np.array([image[:,0], image[:,1]]), axis=0)
image_yAvg = np.mean(np.array([image[:,2], image[:,3]]), axis=0)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(30, 10))
ax1.set_title('Test', fontsize=30)
ax1.scatter(draw_xAvg, draw_yAvg, s=0.1, c='b')
ax2.set_title('Image', fontsize=30)
ax2.scatter(image_xAvg, image_yAvg, s=0.1, c='r')
ax3.set_title('Overlay (Image)', fontsize=30)
ax3.scatter(image_xAvg, image_yAvg, s=0.1, c='r')
ax3.scatter(draw_xAvg, draw_yAvg, s=0.1, c='b')
# Would return this once I partitioned the input correctly.
# The idea is to have a list per tile of all the points found in that tile.
# All I am doing is checking that I get the right number of points in total.
return partitioned_image
致电代码:
partitioned_cells = partition(cells, 20)
正如您所看到的,我按照与输入大小成比例的步骤逐步输入。这应该是完全正确的,我确实得到了绝大多数的要点,所以代码并没有完全错误,我的逻辑很好。但是,我希望在下面的第三个图中完美重叠:
如果你仔细观察右手图,你可以看到一个明显的网格状红色,蓝色不重叠,特别是在该图的右侧 - 所得到的numpy阵列的大小(12948 v 13804)也确认存在不匹配,红色数量超过蓝色。我在分区中遗漏了一些坐标。
我不知道为什么会这样 - 即使我的边界是包容性的(>=
或<=
),他们仍然无法得到所有的观点。我不明白为什么。有人可以解释或猜测为什么会这样吗?