如何基于类上的grabcut面膜中的颜色更改颜色

发布于 2025-01-30 05:00:14 字数 804 浏览 1 评论 0原文

这是我的图像:

“在此处输入图像说明”

我有2类狗和卡车,我也有他们的bbox坐标,所以我想创建此图像的掩码,所以我应用了grapcut算法,这是我的代码

img=cv2.imread('dog.jpg')
mask=np.zeros(img.shape[:2],np.uint8)

bgModel=np.zeros((1,65),np.float64)
fgModel=np.zeros((1,65),np.float64)
tmpimage=image
masks=[]
for i in recs:
    cv2.grabCut(img,mask,i,bgModel,fgModel,10,cv2.GC_INIT_WITH_RECT)
    mask2=np.where((mask==2)|(mask==0),0,1).astype('uint8')
    masks.append(mask2)
    #img=image*mask2[:,:,np.newaxis]
finalmask=np.zeros(img.shape[:2],np.uint8)
for i in range(len(masks)):
   finalmask=finalmask+masks[i]
plt.imshow(finalmask)
# plt.colorbar()
#plt.xticks([]),plt.yticks([])
plt.show()

这是最终掩码:

, 做我希望狗的区域具有与卡车区域不同的颜色,这两个图像都具有相同的黄色

here's my image:

enter image description here

i have 2 classes dog and truck i also have their bbox coordinates so i want to create a mask for this image so i applied grapcut algorithm here's my code

img=cv2.imread('dog.jpg')
mask=np.zeros(img.shape[:2],np.uint8)

bgModel=np.zeros((1,65),np.float64)
fgModel=np.zeros((1,65),np.float64)
tmpimage=image
masks=[]
for i in recs:
    cv2.grabCut(img,mask,i,bgModel,fgModel,10,cv2.GC_INIT_WITH_RECT)
    mask2=np.where((mask==2)|(mask==0),0,1).astype('uint8')
    masks.append(mask2)
    #img=image*mask2[:,:,np.newaxis]
finalmask=np.zeros(img.shape[:2],np.uint8)
for i in range(len(masks)):
   finalmask=finalmask+masks[i]
plt.imshow(finalmask)
# plt.colorbar()
#plt.xticks([]),plt.yticks([])
plt.show()

here's the final mask:

enter image description here

what i want to do is i want the area of the dog to have different color than area of the truck in this image both are having the same yellow color

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妖妓 2025-02-06 05:00:14

在您的情况下,finalmask是一个2通道图像。您从plt.imshow(finalMask)获得的输出是二进制图像,其中黄色 - > 255和紫色 - > 0。

我为每个掩码创建了一个带有3通道的掩码,并为每个掩码分配了随机颜色。

代码:

# creating 3-channel mask:
finalmask = np.zeros(img.shape, np.uint8)

for i in range(len(masks)):
    # generating random color 
    # plucked from: https://stackoverflow.com/questions/28999287/generate-random-colors-rgb
    color = tuple(np.random.choice(range(256), size=3))

    # create another mask to place color in white regions
    im = np.zeros(img.shape, np.uint8)
    im[masks[i] == 255] = color

    # add with final mask
    finalmask = finalmask + im

结果:

在这里我创建的样品掩码:

“在此处输入图像说明” &

最终结果:

”在此处输入图像说明”

注意: 如果要为每个蒙版标签进行固定颜色,则可以创建一个字典并从中参考。

In your case finalmask is a 2-channel image. The output you get from plt.imshow(finalmask) is a binary image where yellow -> 255 and purple -> 0.

I have created a mask with 3-channels and assigned random colors for each mask.

Code:

# creating 3-channel mask:
finalmask = np.zeros(img.shape, np.uint8)

for i in range(len(masks)):
    # generating random color 
    # plucked from: https://stackoverflow.com/questions/28999287/generate-random-colors-rgb
    color = tuple(np.random.choice(range(256), size=3))

    # create another mask to place color in white regions
    im = np.zeros(img.shape, np.uint8)
    im[masks[i] == 255] = color

    # add with final mask
    finalmask = finalmask + im

Results:

Here sample masks I created:

enter image description here& enter image description here

End result:

enter image description here

Note: If you want fixed color for each mask label, you can create a dictionary and refer from it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文