二值图上色

比赛需要将二值图转成coco格式,还没跑通

中间有一步需要将二值图上色

代码如下

import cv2
from skimage import measure, color
import os.path
import glob
def set_color(path,outdir):
    img = cv2.imread(path)
    # img_copy = img.copy()
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # img_gauss = cv2.GaussianBlur(img_gray, (5, 5), 1)
    img_temp = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)[1]
    labels = measure.label(img_temp)
    dst = color.label2rgb(labels, bg_label=0)    # bg_label=0要有,不然会有警告
    # cv2.imshow("666", dst)
    cv2.waitKey(0)
    # cv2.destroyAllWindows()
    x = os.path.basename(path).split(".")[0]
    cv2.imwrite(outdir + x + '.png', dst)


path = "D:\\dataset\\context\\test_AB\\bbb\\*.png"
out = "D:\\dataset\\context\\test_AB\\annotations\\"
for jpgfile in glob.glob(path):
    set_color(jpgfile,out)

原图与结果

(彩图是全黑的可以在arcgis查看)

 参考Python + Skimage + OpenCV 使用技巧 实现连通区域染色_闪电丶教主的博客-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值