批量重采样、边界填充、二值化

import cv2
import os
import sys

from PIL import Image

# def edge(filepath,destpath):
#     PathDir = os.listdir(filepath)
#     for allDir in PathDir:
#         print(allDir)
#         child = os.path.join(filepath,allDir)
#         dest = os.path.join(destpath,allDir)
#         if not os.path.exists(destpath):
#             os.makedirs(destpath)
#
#         if os.path.isfile(child):
#             img = cv2.imread(child)
#             if child is None:
#                 print('Faile')
#                 sys.exit()
#
#
#         #dat = cv2.flip(img,0)
#         retal , dst =cv2.threshold(dat,0,255,cv2.THRESH_BINARY)
#         cv2.imwrite(dest,dst)



# def rename(picpathlist):
#     for i in range(0,len(picpathlist)):
#         pic_rename = picpathlist[i].rsplit("\\",1)[0]
#         os.rename(picpathlist[i],pic_rename + "\\" + str(int(i)+1) + ".jpg")
#         print(pic_rename + "\\" + str(int(i)+1) + ".jpg")

def cut(filepath,destpath):      #重采样
    pathDir = os.listdir(filepath)

    for allDir in pathDir:
        print(allDir)
        child = os.path.join(filepath, allDir)
        dest = os.path.join(destpath,allDir)

        if os.path.isfile(child):
            img = cv2.imread(child)
            if child is None:
                print('faile')
                sys.exit()

            w = 384
            h = 384

            img1 = cv2.resize(img,(w,h),interpolation=cv2.INTER_NEAREST)  #最近邻

            cv2.imwrite(dest,img1)

def interpolation(destpath):     #边界填充
    pathDir = os.listdir(destpath)

    for allDir in pathDir:
        print(allDir)
        #child = os.path.join(filepath,allDir)
        dest = os.path.join(destpath,allDir)

        if os.path.isfile(dest):
            img = cv2.imread(dest)
            if dest is None:
                print('faile')
                sys.exit()

            top_size, bottom_size, left_size, right_size = [64,64,64,64]
            #edge1 = cv2.copyMakeBorder(img,top_size, bottom_size, left_size, right_size,cv2.BORDER_CONSTANT, value=0)
            reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,
                                           borderType=cv2.BORDER_REFLECT_101)

            cv2.imwrite(dest,reflect101)

def image_binarization(destpath):
    pathDir = os.listdir(filepath)
    for allDir in pathDir:
        dest = os.path.join(destpath,allDir)

        if os.path.isfile(dest):
            image = cv2.imread(dest)
            print(dest)
        n,s = os.path.splitext(dest)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        retval, dst = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)   #dst二值化图像
        cv2.imwrite(dest,dst)



if __name__ == "__main__":
    filepath = r'E:\1\yuan'
    destpath = r'E:\1\yuan512'
    cut(filepath,destpath)
    interpolation(destpath)
    image_binarization(destpath)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值