我是python和opencv的初学者。基本上,我在Google周围搜索,发现了3套代码。现在,我只是试图将它们放在一起以实现我的目标,即“从图像A裁剪随机形状的多边形,然后将图像A的随机多边形的部分粘贴到图像B中”。我正在使用Brezier曲线和其他方法来获得带有曲线的随机形状的多边形。之后,将x和y的值放入二维numpy数组中。然后将坐标数组(ROI /顶点)传递给boundingRectangnle()。如果顶点是整数,则此代码可正常运行。但是,当顶点是浮点数时,boundingRectangnle()会抛出错误消息:
cv2.error:OpenCV(4.1.0) /io/opencv/modules/imgproc/src/shapedescr.cpp:743:错误: (-215:断言失败)npoints> = 0 &&(深度== CV_32F ||深度== CV_32S)在函数“ pointSetBoundingRect”中
这是我获得随机形状多边形的顶点的方法:
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()
这是从图像A绘制随机形状的多边形的代码:
img = cv2.imread('cat.jpg',0)
#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]])
pts = np.array(pts).astype(np.int64)
print (pts.ndim)
print (pts.shape)
print (pts.size)
print (pts.dtype)
## (1) Crop the bounding rect
rect = cv2.boundingRect(pts)
x,y,w,h = rect
croped = img[y:y+h, x:x+w].copy()
## (2) make mask
pts = pts - pts.min(axis=0)
mask = np.zeros(croped.shape[:2], np.uint8)
cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)
## (3) do bit-op
dst = cv2.bitwise_and(croped, croped, mask=mask)
## (4) add the white background
bg = np.ones_like(croped, np.uint8)*255
cv2.bitwise_not(bg,bg, mask=mask)
dst2 = bg+ dst
cv2.imwrite("croped.png", croped)
cv2.imwrite("mask.png", mask)
cv2.imwrite("dst.png", dst)
cv2.imwrite("dst2.png", dst2)
代码的第一部分可以正常工作。当为'boundingRectangle()提供一个整数的numpy数组时,第二部分(裁剪图片)也可以正常工作
pts = np.array([[120,456], [456,678], [875,345], [980,234], [943,123], [45,786], [126,50]])
但是遇到这个错误
cv2.error:OpenCV(4.1.0) /io/opencv/modules/imgproc/src/shapedescr.cpp:743:错误: (-215:断言失败)npoints> = 0 &&(深度== CV_32F ||深度== CV_32S)在函数“ pointSetBoundingRect”中
当数组元素为浮点数时。
pts = np.array([[0.14837693,0.03585092],
[0.14529922,0.03757408],
[0.14255947,0.03953773],
[0.14015068,0.04173684],
[0.14837693,0.03585092]])
如何克服这个问题?我没有义务遵循这种方法来完成它。因此,非常感谢新技术和建议。预先感谢。