日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

opencv学习笔记(六)---图像梯度

發(fā)布時(shí)間:2025/4/14 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 opencv学习笔记(六)---图像梯度 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

圖像梯度的算法有很多方法:sabel算子,scharr算子,laplacian算子,sanny邊緣檢測(cè)(下個(gè)隨筆)。。。

這些算子的原理可參考:https://blog.csdn.net/poem_qianmo/article/details/25560901

下面是我的一些理解:


sabel算子:

sobel算子主要用于獲得數(shù)字圖像的一階梯度,常見(jiàn)的應(yīng)用和物理意義是邊緣檢測(cè)。

函數(shù)

Python: cv2.Sobel(src, ddepth, dx, dy[, dst[, ksize[, scale[, delta[, borderType]]]]]) → dst? (參數(shù)就不一一說(shuō)了,常用的就那幾個(gè),其他默認(rèn)即可)

?

Parameters:

?

  • src – Source image.
  • dst – Destination image of the same size and the same number of channels as src .
  • ddepth – Destination image depth.
  • xorder – Order of the derivative x.
  • yorder – Order of the derivative y.
  • ksize – Size of the extended Sobel kernel. It must be 1, 3, 5, or 7.
  • scale – Optional scale factor for the computed derivative values. By default, no scaling is applied. See getDerivKernels() for details.
  • delta – Optional delta value that is added to the results prior to storing them in dst .
  • borderType – Pixel extrapolation method. See borderInterpolate() for details.

?

?

原理

?

算子使用兩個(gè)33的矩陣(圖1)算子使用兩個(gè)33的矩陣(圖1)去和原始圖片作卷積,分別得到橫向G(x)和縱向G(y)的梯度值,如果梯度值大于某一個(gè)閾值,則認(rèn)為該點(diǎn)為邊緣點(diǎn)

?

Gx方向的相關(guān)模板:

?

?

Gy方向的相關(guān)模板:

?

具體計(jì)算如下:

圖像的每一個(gè)像素的橫向及縱向灰度值通過(guò)以下公式結(jié)合,來(lái)計(jì)算該點(diǎn)灰度的大小:

?

通常,為了提高效率使用不開(kāi)平方的近似值:

#sobels算子 img = cv.imread("E:/pictures/lena.jpg",cv.IMREAD_UNCHANGED) sobelx= cv.Sobel(img,cv.CV_64F,1,0) #cv.CV_64F將像素值轉(zhuǎn)換為double型,不然計(jì)算后為負(fù)值的像素會(huì)被截?cái)酁? sobelx = cv.convertScaleAbs(sobelx) #轉(zhuǎn)換為uint8類(lèi)型(x方向) sobely = cv.Sobel(img,cv.CV_64F,0,1) sobely = cv.convertScaleAbs(sobely) sobelxy11 = cv.Sobel(img,cv.CV_64F,1,1) #直接x,y方向一起計(jì)算(效果不好,應(yīng)分開(kāi)計(jì)算,再求權(quán)重和) sobelxy11 = cv.convertScaleAbs(sobelxy11) sobelxy = cv.addWeighted(sobelx,0.5,sobely,0.5,0) #圖像權(quán)重和 cv.imshow("orginal",img) #dst = cv.addWidget(src1,alpha,src2,beta,gamma) cv.imshow("sobelx",sobelx) #src1 圖一 alpha->圖一的權(quán)重 src2->圖二 beta->圖二的權(quán)重 gamma->修正值 cv.imshow("sobely",sobely) #dst = src1*alpha+src2*beta+gamma cv.imshow("sobelxy",sobelxy) cv.imshow("sobelxy11",sobelxy11) cv.waitKey() cv.destroyAllWindows()

用sobel函數(shù)同時(shí)對(duì)想x,y方向檢測(cè)的效果并不好,一般不用。


scharr算子:

函數(shù):

Python: cv2.Scharr(src, ddepth, dx, dy[, dst[, scale[, delta[, borderType]]]]) → dst

Parameters:
  • src – Source image.
  • dst – Destination image of the same size and the same number of channels as src .
  • ddepth – Destination image depth.
  • xorder – Order of the derivative x.
  • yorder – Order of the derivative y.
  • scale – Optional scale factor for the computed derivative values. By default, no scaling is applied. See getDerivKernels() for details.
  • delta – Optional delta value that is added to the results prior to storing them in dst .
  • borderType – Pixel extrapolation method. See borderInterpolate() for details.

?

scharr算子是對(duì)sabel算子的增強(qiáng),可以看到上圖中很多細(xì)小的邊緣都沒(méi)檢測(cè)到,那么scharr算子就是解決這個(gè)問(wèn)題的,它比sabel算子更精確,速度和復(fù)雜程度卻一樣,只是因?yàn)橛玫暮瞬灰粯?/span>

是scharr 的卷積核,他的原理和sabel算子一樣。

?

#scharr算子 scharr算子是對(duì)sabel算子的增強(qiáng) scharr算子等價(jià)于ksize=-1的sabel算子 img = cv.imread("E:/pictures/lena.jpg",cv.IMREAD_GRAYSCALE) scharrx= cv.Scharr(img,cv.CV_64F,1,0) #scharrx算子要滿(mǎn)足dx>=0&&dy>=0&&dx+dy=1 scharrx = cv.convertScaleAbs(scharrx) scharry = cv.Scharr(img,cv.CV_64F,0,1) scharry = cv.convertScaleAbs(scharry) scharrxy = cv.addWeighted(scharrx,0.5,scharry,0.5,0) #sabel算子和 scharr算子的比較 sobelx= cv.Sobel(img,cv.CV_64F,1,0) sobelx = cv.convertScaleAbs(sobelx) sobely = cv.Sobel(img,cv.CV_64F,0,1) sobely = cv.convertScaleAbs(sobely) sobelxy = cv.addWeighted(sobelx,0.5,sobely,0.5,0) cv.imshow("orginal",img) cv.imshow("sobelxy",sobelxy) cv.imshow("scharrxy",scharrxy) cv.waitKey() cv.destroyAllWindows()

?

 


laplacian算子:

Laplace算子和Sobel算子一樣,屬于空間銳化濾波操作,只不過(guò)是用的二階微分,看官網(wǎng)的一些解釋:

The function calculates the Laplacian of the source image by adding up the second x and y derivatives calculated using the Sobel operator:

This is done when ksize > 1 . When ksize == 1 , the Laplacian is computed by filtering the image with the following aperture:

cv.Laplace(src, dst, ksize=3) → None

Parameters:

  • src – Source image.
  • dst – Destination image of the same size and the same number of channels as src .
  • ddepth – Desired depth of the destination image.
  • ksize – Aperture size used to compute the second-derivative filters. See getDerivKernels() for details. The size must be positive and odd.
  • scale – Optional scale factor for the computed Laplacian values. By default, no scaling is applied. See getDerivKernels() for details.
  • delta – Optional delta value that is added to the results prior to storing them in dst .
  • borderType – Pixel extrapolation method. See borderInterpolate() for details.

?

?

#拉普拉斯算子 img = cv.imread("E:/pictures/erode1.jpg",cv.IMREAD_GRAYSCALE) r = cv.Laplacian(img,cv.CV_64F) r = cv.convertScaleAbs(r) #關(guān)鍵代碼就這兩行,拉普拉斯算子不用再求x,y的權(quán)重和,因?yàn)檫@個(gè)函數(shù)都計(jì)算好了 cv.imshow("orginal",img) cv.imshow("laplacian",r) cv.waitKey() cv.destroyAllWindows()

  

?


Canny邊緣檢測(cè):

Python: cv2.Canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient]]]) → edges

threshold1和threshold2是兩個(gè)閾值,越小檢測(cè)效果越好

?

import cv2 as cv import numpy as npimg = cv.imread("E:/pictures/lena.jpg") result1 = cv.Canny(img,50,100) result2 = cv.Canny(img,100,200) cv.imshow("orginal",img) cv.imshow("result1",result1) cv.imshow("result2",result2) cv.waitKey() cv.destroyAllWindows()

?

 

?

轉(zhuǎn)載于:https://www.cnblogs.com/ling2000/p/10588294.html

總結(jié)

以上是生活随笔為你收集整理的opencv学习笔记(六)---图像梯度的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。