如何基于类上的grabcut面膜中的颜色更改颜色
这是我的图像:
我有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:
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,
finalmask
是一个2通道图像。您从plt.imshow(finalMask)
获得的输出是二进制图像,其中黄色 - > 255和紫色 - > 0。我为每个掩码创建了一个带有3通道的掩码,并为每个掩码分配了随机颜色。
代码:
结果:
在这里我创建的样品掩码:
最终结果:
In your case
finalmask
is a 2-channel image. The output you get fromplt.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:
Results:
Here sample masks I created:
End result: