我试图找到一个图像的轮廓,并将它们(用一些透明度)覆盖在另一个图像上。然而,当我执行下面的代码时,我得到了一个不希望的效果(见下图)。
img = cv2.imread('image1.bmp',cv2.IMREAD_GRAYSCALE)
contours, hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
im = cv2.imread('image2.jpg')
im_copy = im.copy()
for j,cont in enumerate(contours):
im_copy = cv2.drawContours(im_copy, [cont], -1, (0, 0, 255), -1)
im = cv2.addWeighted(im_copy, 0.4, im, 1 - 0.2, 0)
im = cv2.drawContours(im, [cont], -1, (0, 0, 255), 0)
cv2.imwrite(f'projects/{prj.name}/logs/repeated_img/{file}', im)
字符串
的数据
我调用drawContours()
两次以获得半透明填充和实心边框。显然,其中一个轮廓似乎比另一个更不透明。我怀疑drawContours对同一轮廓调用了两次,所以我尝试了以下更改:
im_copy = cv2.drawContours(im_copy, [cont], j, (0, 0, 255), -1)
im = cv2.addWeighted(im_copy, 0.4, im, 1 - 0.2, 0)
im = cv2.drawContours(im, [cont], j, (0, 0, 255), 0)
型
这在尝试绘制第二个轮廓时会产生以下错误:
(-215:Assertion failed) 0 <= contourIdx && contourIdx < (int)last in function 'drawContours'
型
轮廓将绘制在不同的颜色取决于一些条件,所以我需要能够独立绘制它们。
是什么产生了这种不良影响?
编辑
回答注解,使用下面的代码转换为rbga
im = cv2.imread('image2.jpg')
im = cv2.cvtColor(im, cv2.COLOR_BGR2BGRA)
im_copy = im.copy()
for j,cont in enumerate(contours):
im_copy = cv2.drawContours(im_copy, [cont], -1, (0, 0, 255,128), -1)
im = cv2.drawContours(im, [cont], -1, (0, 0, 255, 255), 0)
型
结果如下图所示:
的
3条答案
按热度按时间f45qwnt81#
下面是在Python/OpenCV中实现这一点的一种方法。为了演示,我将在最大的区域上绘制一个部分透明的红色轮廓。我通过在输入上绘制一次全红色,然后与原始输入混合来实现这一点。我看不出有什么办法可以直接画出部分透明的轮廓。
输入:
x1c 0d1x的数据
字符串
结果如下:
的
qkf9rpyu2#
该问题是由addWeight()引起的,因为每个轮廓调用一次。这导致一些轮廓与原始图像的权重超过1,因此一些区域看起来更透明。我用以下方法解决了这个问题:
字符串
bqjvbblv3#
更高效的this answer实现:
字符串