使用python和opencv提取图像边缘

通过python和opencv提取图像的边缘代码如下:

import cv2
import os
import numpy as np  
import argparse
from time import time

def main():
    if opt.inverse:
        print("==> Mission start! You choosed black background and white line version.")
    else:
        print("==> Mission start! You choosed white background and black line version.")
    if os.path.isdir(opt.path):
        outdir = os.path.join(opt.path, 'edge_det')
        filelist = [os.path.join(opt.path, i) for i in os.listdir(opt.path) if not (i.startswith('.') or os.path.isdir(os.path.join(opt.path, i)))]
    else:
        outdir = os.path.join(os.path.split(opt.path)[0],'edge_det')
        filelist = [opt.path]
    if not os.path.exists(outdir):
        os.mkdir(outdir)
    for afile in filelist:
        process(afile, outdir)

def process(imgpath, outdir):
    sta = time()
    img = cv2.imread(imgpath, 0)
    img = cv2.GaussianBlur(img,(3,3),0)
    canny = cv2.Canny(img, 50, 150)
    outname = os.path.join(outdir, os.path.split(imgpath)[1])
    if opt.inverse:
        cv2.imwrite(outname ,canny)
    else:
        cv2.imwrite(outname ,cv2.bitwise_not(canny))
    print("==> Image:",os.path.split(imgpath)[1],'successfully processed!\t Time:', str(time()-sta),'s')

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--path', default='./blur image.jpg', help='The path of a file or a folder you want to detect the edge.')
    parser.add_argument('--inverse', action="store_true", default=False)
    opt = parser.parse_args()
    opt.path = os.path.expanduser(opt.path)
    main()
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值