Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从图像中裁剪任意形状的多边形,并将裁剪后的形状粘贴到另一图像的任意位置_Python_Numpy_Opencv - Fatal编程技术网

Python 从图像中裁剪任意形状的多边形,并将裁剪后的形状粘贴到另一图像的任意位置

Python 从图像中裁剪任意形状的多边形,并将裁剪后的形状粘贴到另一图像的任意位置,python,numpy,opencv,Python,Numpy,Opencv,我完全是python和opencv的初学者。基本上,我搜索了一下,找到了3组代码。现在,我正试图将它们放在一起,以实现我的目标,即“从图像A裁剪随机形状的多边形,然后将图像A的随机多边形部分粘贴到图像B中”。我正在使用Brezier曲线和其他方法来获得一个带有曲线的任意形状的多边形。然后将x和y的值放入一个二维numpy数组中。然后将坐标数组(ROI/顶点)传递给boundingRectangnle()。如果顶点是整数,则此代码的工作方式与我预期的相同。但当顶点是浮点数时,boundingRec

我完全是python和opencv的初学者。基本上,我搜索了一下,找到了3组代码。现在,我正试图将它们放在一起,以实现我的目标,即“从图像A裁剪随机形状的多边形,然后将图像A的随机多边形部分粘贴到图像B中”。我正在使用Brezier曲线和其他方法来获得一个带有曲线的任意形状的多边形。然后将x和y的值放入一个二维numpy数组中。然后将坐标数组(ROI/顶点)传递给boundingRectangnle()。如果顶点是整数,则此代码的工作方式与我预期的相同。但当顶点是浮点数时,boundingRectangnle()会抛出错误消息:


cv2.0错误:OpenCV(4.1.0) /io/opencv/modules/imgproc/src/shapedscr.cpp:743:错误: (-215:断言失败)npoints>=0&&(depth==CV_32F|depth== 函数“pointSetBoundingRect”中的CV_32S)


这是我获取随机形状多边形顶点的方式:

bernstein = lambda n, k, t: binom(n,k)* t**k * (1.-t)**(n-k)

def bezier(points, num=200):
    N = len(points)
    t = np.linspace(0, 1, num=num)
    curve = np.zeros((num, 2))
    for i in range(N):
        curve += np.outer(bernstein(N - 1, i, t), points[i])
    return curve

class Segment():
    def __init__(self, p1, p2, angle1, angle2, **kw):
        self.p1 = p1; self.p2 = p2
        self.angle1 = angle1; self.angle2 = angle2
        self.numpoints = kw.get("numpoints", 100)
        r = kw.get("r", 0.3)
        d = np.sqrt(np.sum((self.p2-self.p1)**2))
        self.r = r*d
        self.p = np.zeros((4,2))
        self.p[0,:] = self.p1[:]
        self.p[3,:] = self.p2[:]
        self.calc_intermediate_points(self.r)

    def calc_intermediate_points(self,r):
        self.p[1,:] = self.p1 + np.array([self.r*np.cos(self.angle1),
                                    self.r*np.sin(self.angle1)])
        self.p[2,:] = self.p2 + np.array([self.r*np.cos(self.angle2+np.pi),
                                    self.r*np.sin(self.angle2+np.pi)])
        self.curve = bezier(self.p,self.numpoints)


def get_curve(points, **kw):
    segments = []
    for i in range(len(points)-1):
        seg = Segment(points[i,:2], points[i+1,:2], points[i,2],points[i+1,2],**kw)
        segments.append(seg)
    curve = np.concatenate([s.curve for s in segments])
    return segments, curve

def ccw_sort(p):
    d = p-np.mean(p,axis=0)
    s = np.arctan2(d[:,0], d[:,1])
    return p[np.argsort(s),:]

def get_bezier_curve(a, rad=0.2, edgy=0):
    """ given an array of points *a*, create a curve through
    those points. 
    *rad* is a number between 0 and 1 to steer the distance of
          control points.
    *edgy* is a parameter which controls how "edgy" the curve is,
           edgy=0 is smoothest."""
    p = np.arctan(edgy)/np.pi+.5
    a = ccw_sort(a)
    a = np.append(a, np.atleast_2d(a[0,:]), axis=0)
    d = np.diff(a, axis=0)
    ang = np.arctan2(d[:,1],d[:,0])
    f = lambda ang : (ang>=0)*ang + (ang<0)*(ang+2*np.pi)
    ang = f(ang)
    ang1 = ang
    ang2 = np.roll(ang,1)
    ang = p*ang1 + (1-p)*ang2 + (np.abs(ang2-ang1) > np.pi )*np.pi
    ang = np.append(ang, [ang[0]])
    a = np.append(a, np.atleast_2d(ang).T, axis=1)
    s, c = get_curve(a, r=rad, method="var")
    x,y = c.T
    return x,y, a


def get_random_points(n, scale=0.8, mindst=None, rec=0):
    """ create n random points in the unit square, which are *mindst*
    apart, then scale them."""
    mindst = mindst or 1.0/n
    a = np.random.rand(n,2)
    d = np.sqrt(np.sum(np.diff(ccw_sort(a), axis=0), axis=1)**2)
    if np.all(d >= mindst) or rec>=200:
        return a*scale
    else:
        return get_random_points(n=n, scale=scale, mindst=mindst, rec=rec+1)

figure, ax = plt.subplots()
ax.set_aspect("equal")

rad = 0.2
edgy = 0.05
#for c in np.array([[0,0], [0,1], [1,0], [1,1]]):
for c in np.array([[0,0]]):
    a = get_random_points(n=2, scale=1) + c
    x,y, _ = get_bezier_curve(a,rad=rad, edgy=edgy)

print x
print ("\n\n")
print y
plt.plot(x,y)
plt.show()
代码的第一部分工作没有任何问题。当为“boundingRectangle()提供整数的numpy数组时,第二部分(裁剪图片)也可以正常工作

但是遇到这个错误


cv2.0错误:OpenCV(4.1.0) /io/opencv/modules/imgproc/src/shapedscr.cpp:743:错误: (-215:断言失败)npoints>=0&&(depth==CV_32F|depth== 函数“pointSetBoundingRect”中的CV_32S)


当数组元素是浮点数时

pts = np.array([[0.14837693,0.03585092],
                [0.14529922,0.03757408],
                [0.14255947,0.03953773],
                [0.14015068,0.04173684],
                [0.14837693,0.03585092]])
你如何克服这个问题?我没有义务遵循这种方法来完成它。因此,新技术和建议非常受欢迎。提前谢谢

pts = np.array([[120,456], [456,678], [875,345], [980,234], [943,123], [45,786], [126,50]])
pts = np.array([[0.14837693,0.03585092],
                [0.14529922,0.03757408],
                [0.14255947,0.03953773],
                [0.14015068,0.04173684],
                [0.14837693,0.03585092]])