zoukankan      html  css  js  c++  java
  • 边缘检测

    import cv2
    import numpy as np
    import math
    import random
    img = cv2.imread('D:/pythonob/imageinpaint/img/zidan.jpg',1)
    imgInfo = img.shape
    height = imgInfo[0]
    width = imgInfo[1]
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    imgG = cv2.GaussianBlur(gray,(3,3),0)#高斯滤波
    dst1 = cv2.Canny(img,50,50)#API实现
    #源码实现
    #sobel算法-->算子模板:竖直方向上的算子[1 2 1,0 0 0 ,-1 -2 -1]——>卷积得到数值方向梯度b
    # 和水平方向上的算子[1 0 -1,2 0 2, 1 0 -1]——>卷积得到水平梯度值a
    #对a,b进行开方得到值:f = sqrt(a*a,b*b)
    #f > 判决值-->边缘
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    dst2 = np.zeros((height,width,1),np.uint8)  
    for i in range(0,height-2):
    for j in range(0,width-2):
    gy = gray[i,j]*1 + gray[i,j+1]*2 + gray[i,j+2]*1 - gray[i+2,j]*1 - gray[i+2,j+1]*2 - gray[i+2,j+2]*1#卷积
    gx = gray[i,j]*1 + gray[i+1,j]*2 + gray[i+2,j]*1 - gray[i,j+2]*1 - gray[i+1,j+2]*2 - gray[i+2,j+2]*1
    grad = math.sqrt(gx*gx + gy*gy)
    if grad>80:
    dst2[i,j] = 255
    else:
    dst2[i,j] = 0
    cv2.imshow('dst',img)
    cv2.imshow('dst1',dst1)
    cv2.imshow('dst2',dst2)
    cv2.waitKey(0)

    原图:


    效果图,其中dst1为API效果,dst2为源码实现:

  • 相关阅读:
    欧拉函数
    新博客地址
    socket编程
    文件操作
    python安装扩展”unable to find vcvarsall.bat“的解决办法
    PYTHON以及插件安装
    梯式结构
    PHPSTORM配置
    CSRF攻击
    js的一些奇葩用法
  • 原文地址:https://www.cnblogs.com/cxxBoo/p/11459742.html
Copyright © 2011-2022 走看看