binarize_allfiles.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:Python_SelfLearning 作者: fukuit 项目源码 文件源码
def convert_and_count(target, ext):
    for root, dirs, files in os.walktarget):
        for f in files:
            fname, fext = os.path.splitext(f)
            if fext == ext:
                img = cv2.imread(os.path.join(root, f), 0)
                blur = cv2.GaussianBlur(img, (5, 5), 0)
                ret, imgb = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
                cv2.imwrite(os.path.join(root, fname + ".bin.png"), imgb)
                cnt = 0
                for val in imgb.flat:
                    if val == 0:
                        cnt += 1
                ratio = cnt / img.size
                msg = "%s:\t%.5f" % (f, ratio)
                print(msg)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号