如何使用open填充整个内部轮廓

2024-10-03 04:26:07 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的图片中,我试图用“jupiter”这个名字来遮住内部的设计带

enter image description here

enter image description here

我想要的结果是

enter image description here

我尝试过使用RETR永EXTERNAL in和fillPoly,但它只会使二值图像的“白色”部分(带)变黑,而不是完全如我所愿。在

我要怎样即兴创作才能让它完全变黑?在

enter image description here

enter image description here


Tags: in图像图片名字externaljupiter白色retr
1条回答
网友
1楼 · 发布于 2024-10-03 04:26:07

你可以使用掩蔽技术来完成你的工作。这是我的代码:

import cv2
import os
import numpy as np
import matplotlib.pyplot as plt
import copy
%matplotlib inline

#                                         -
I = cv2.imread('E:\\Mukul\\others\\stof.png') #input image
#I.shape

I_cnt = np.where(I[:,:,2] == 255) #location of your bounding box region
I_mask = np.zeros_like(I[:,:,2])  # mask for the input image

I_mask[list(I_cnt[0]), list(I_cnt[1])] = 255
plt.imshow(I_mask, cmap = 'gray')

bounding box region

^{pr2}$

masked bounding box

因为我们希望边界框区域是黑色的,所以我们将使用cv2.bitwise_not()反转图像,然后使用cv2.bitwise_and()来获得所需的输出图像。在

I_mask1 = cv2.bitwise_not(I_mask) 
out = cv2.bitwise_and(I_mask1, I[:,:,2])
plt.imshow(out,cmap = 'gray')

output image

我们可以使用以下代码直接将I_cnt[0](包含x坐标的数组)和I_cnt[1](包含y坐标的数组)转换为(x,y)坐标的数组,而不是使用上面的行来查找二进制掩码的轮廓:

temp_list = []
for a, b in zip(I_cnt[0], I_cnt[1]):
     temp_list.append([a, b])
ctr = np.array(temp_list).reshape((-1,1,2)).astype(np.int32)

I_mask2 = np.zeros_like(I[:,:,2])
I_mask2[list(I_cnt[0]), list(I_cnt[1])] = 255
plt.imshow(I_mask2, cmap = 'gray')


cv2.fillConvexPoly(I_mask1, ctr, 255)
plt.imshow(I_mask2,cmap = 'gray')

相关问题 更多 >