【发布时间】:2020-02-08 23:24:18
【问题描述】:
我有一个形状轮廓cnt,我需要在二维数组中找到它,我有一个target_index变量,它用于查找所需区域,但我需要在其中查找cnt轮廓.
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()
cnt = [[550, 42],
[600, 42],
[690, 273],
[640, 273]]
# Need to use cnt here
target_index = np.where(np.logical_and(destination[:,1]==789,destination[:,0]>=421))
destination[target_index]
scope = destination[target_index]
scope[:,0] = scope[:,0] + 10
destination[target_index] = scope
destination[target_index]
# Remap
grid_x, grid_y = np.mgrid[0:800, 0:800]
grid_z = griddata(source, destination, (grid_x, grid_y), method='cubic')
map_x = np.append([], [ar[:,1] for ar in grid_z]).reshape(800,800).astype('float32')
map_y = np.append([], [ar[:,0] for ar in grid_z]).reshape(800,800).astype('float32')
warped_image = cv2.remap(img, map_x, map_y, cv2.INTER_CUBIC)
cv2.drawContours(warped_image,[cnt],0,(0,0,0),2)
可以使用其他方法,但首选np.where。
【问题讨论】:
-
cnt看起来像 4 个角点,定义一个钝角梯形? “在二维数组中找到形状轮廓”是什么意思?你希望得到什么? -
@kwinkunks 我想找到轮廓内的所有点
-
啊,好吧...我会使用
shapely或geopandas。但看起来matplotlib也可能有办法做到这一点。例如。检查stackoverflow.com/questions/21339448/… -
@kwinkunks 没谈过二维数组和np.where