zoukankan      html  css  js  c++  java
  • 图像梯度计算

    图像梯度-Sobel算子

    从右到左,从下到上

    img=cv2.imread('pie.png',cv2.IMREAD_GRAYSCALE)
    cv2.imshow('img',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    dst = cv2.Sobel(src, ddepth, dx, dy, ksize)

    • ddepth:图像的深度
    • dx和dy分别表示水平和竖直方向
    • ksize是Sobel算子的大小

    白到黑是正数,黑到白就是负数了,所有的负数会被截断成0,所以要取绝对值

    sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
    sobelx = cv2.convertScaleAbs(sobelx)
    cv_show(sobelx,'sobelx')
    sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
    sobely = cv2.convertScaleAbs(sobely)  
    cv_show(sobely,'sobely')

     

    分别计算x和y,再求和

    sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5,0)
    cv_show(sobelxy,'sobelxy')

    不建议直接计算

    img=cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE)
    cv_show(img,'img')

    img = cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE)
    sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
    sobelx = cv2.convertScaleAbs(sobelx)
    sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
    sobely = cv2.convertScaleAbs(sobely)
    sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5,0)
    cv_show(sobelxy,'sobelxy')

    直接计算效果不好

    img = cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE)
    
    sobelxy=cv2.Sobel(img,cv2.CV_64F,1,1,ksize=3) 
    sobelxy = cv2.convertScaleAbs(sobelxy)
    cv_show(sobelxy,'sobelxy')

    图像梯度-Scharr算子

    图像梯度-laplacian算子

    二阶梯度,即为一阶导

    #不同算子的差异
    img = cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE)
    sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
    sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
    sobelx = cv2.convertScaleAbs(sobelx)   
    sobely = cv2.convertScaleAbs(sobely)  
    sobelxy =  cv2.addWeighted(sobelx,0.5,sobely,0.5,0)  
    
    scharrx = cv2.Scharr(img,cv2.CV_64F,1,0)
    scharry = cv2.Scharr(img,cv2.CV_64F,0,1)
    scharrx = cv2.convertScaleAbs(scharrx)   
    scharry = cv2.convertScaleAbs(scharry)  
    scharrxy =  cv2.addWeighted(scharrx,0.5,scharry,0.5,0) 
    
    laplacian = cv2.Laplacian(img,cv2.CV_64F)
    laplacian = cv2.convertScaleAbs(laplacian)   
    
    res = np.hstack((sobelxy,scharrxy,laplacian))
    cv_show(res,'res')

  • 相关阅读:
    第二章
    第一章
    unity--实现新手引导功能
    golang MissingContentLength error
    遇到一个golang time.Tick的坑
    grpc client连接池及负载均衡实现
    Pytorch学习-线性回归
    Pytorch学习-自动求导
    Pytorch学习-线性代数实现
    天池Python训练营笔记—Python基础进阶:从函数到高级魔法方法
  • 原文地址:https://www.cnblogs.com/exciting/p/10390021.html
Copyright © 2011-2022 走看看