Sobel運算元是像素圖像邊緣檢測中最重要的運算元之一,在機器學習、數字媒體、計算機視覺等信息科技領域起著舉足輕重的作用。在技術上,它是一個離散的一階差分運算元,用來計算圖像亮度函式的一階梯度之近似值。在圖像的任何一點使用此運算元,將會產生該點對應的梯度矢量或是其法矢量。
基本介紹
- 中文名:索貝爾運算元
- 外文名:Sobel operator
- 別稱:一階梯度運算元
- 提出者:Irwin Sobel
- 提出時間:二十世紀中後期
- 套用學科:數學,圖像處理
- 適用領域範圍:圖像處理,模式識別,機器視覺等
由來
核心公式
運算元描述
.NET代碼
unsignedcharComputeSobel(unsignedcharul,//upperleftunsignedcharum,//uppermiddleunsignedcharur,//upperrightunsignedcharml,//middleleftunsignedcharmm,//middle(unused)unsignedcharmr,//middlerightunsignedcharll,//lowerleftunsignedcharlm,//lowermiddleunsignedcharlr,//lowerrightfloatfScale){shortHorz=ur+2*mr+lr-ul-2*ml-ll;shortVert=ul+2*um+ur-ll-2*lm-lr;shortSum=(short)(fScale*(abs((int)Horz)+abs((int)Vert)));if(Sum<0){return0;}elseif(Sum>0xff){return0xff;}return(unsignedchar)Sum;}for(Times=0;Times<128&&iThreshold!=iNewThreshold;Times++){iThreshold=iNewThreshold;lP1=0;lP2=0;lS1=0;lS2=0;for(i=iMinGray;i<iThreshold;i++){lP1+=Histogram*i;lS1+=Histogram;}iMean1Gray=lP1/lS1;for(i=iThreshold;i<iMaxGray;i++){lP2+=Histogram*i;lS2+=Histogram;}iMean2Gray=lP2/lS2;iNewThreshold=(iMean1Gray+iMean2Gray)/2;}//補充Sobel運算元的矩陣表達式:Sobel1=[-1-2-1;000;121];//檢測垂直邊沿的Sobel運算元Sobel2=[10-1;20-2;10-1];//檢測水平邊沿的Sobel運算元
C代碼
/* Sobel templatea00 a01 a02a10 a11 a12a20 a21 a22*/unsigned char a00, a01, a02;unsigned char a10, a11, a12;unsigned char a20, a21, a22;void MySobel(IplImage* gray, IplImage* gradient){CvScalar color ;for (int i=1; i<gray->height-1; ++i){for (int j=1; j<gray->width-1; ++j){a00 = cvGet2D(gray, i-1, j-1).val[0];a01 = cvGet2D(gray, i-1, j).val[0];a02 = cvGet2D(gray, i-1, j+1).val[0];a10 = cvGet2D(gray, i, j-1).val[0];a11 = cvGet2D(gray, i, j).val[0];a12 = cvGet2D(gray, i, j+1).val[0];a20 = cvGet2D(gray, i+1, j-1).val[0];a21 = cvGet2D(gray, i+1, j).val[0];a22 = cvGet2D(gray, i+1, j+1).val[0];// x方向上的近似導數 卷積運算double ux = a20 * (1) + a10 * (2) + a00 * (1)+ (a02 * (-1) + a12 * (-2) + a22 * (-1));// y方向上的近似導數 卷積運算double uy = a02 * (1) + a01 * (2) + a00 * (1)+ a20 * (-1) + a21 * (-2) + a22 * (-1);color.val[0] = sqrt(ux*ux + uy*uy);cvSet2D(gradient, i, j, color);}}}//注釋:該程式需要在安裝Opencv軟體下運行。Matlabps=imread('D:\14.jpg'); %讀取圖像subplot(1,3,1)imshow(ps);title('原圖像');ps=rgb2gray(ps);[m,n]=size(ps); %用Sobel微分運算元進行邊緣檢測pa = edge(ps,'sobel');subplot(1,3,2);imshow(pa);title('Sobel邊緣檢測得到的圖像');