我有一个形状轮廓cnt
,我需要在2D数组中找到它,我有一个target_index变量,它用于查找所需的区域,但是我需要寻找{{1} }轮廓。
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)
。
答案 0 :(得分:1)
除非您将自己限制为某些多边形,否则我认为使用np.where
很难做到这一点。
以下是使用matplotlib
的{{1}}对象解决问题(适应this solution)的方法:
Path
然后查看结果:
import numpy as np
from matplotlib.path import Path
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)
cnt = [[550, 42],
[600, 42],
[690, 273],
[640, 273]]
p = Path(cnt)
grid = p.contains_points(source)
mask = grid.reshape(20, 20)
哪个给:
在import matplotlib.pyplot as plt
plt.imshow(mask)
中使用更多的点以获得更高分辨率的结果。
答案 1 :(得分:1)
根据您的问题判断,您正在申请身体变形,对我而言,此选项最方便,因为您可以为您创建任何轮廓。
# Left hand contour
pt1 = (int_12, int_13)
pt2 = (int_17, int_16)
pt3 = (int_18, int_19)
pt4 = (int_14, int_15)
lh_cnt = np.array([pt1, pt2, pt3, pt4])
offset = int(hand_lenght / 28)
for x in destination:
inside_lh = cv2.pointPolygonTest(lh_cnt, (x[0], x[1]), False)
elif inside_lh > 0:
x[0] = x[0] - offset
# Warping
grid_x, grid_y = np.mgrid[0:self.width, 0:self.height]
grid_z = griddata(source, destination, (grid_x, grid_y), method='cubic')
map_x = np.append([], [ar[:,0] for ar in grid_z]).reshape(self.width, self.height).astype('float32')
map_y = np.append([], [ar[:,1] for ar in grid_z]).reshape(self.width, self.height).astype('float32')
warped_image = cv2.transpose(cv2.remap(img, map_x, map_y, cv2.INTER_LANCZOS4))