opencv學習筆記 13 自定義線性濾波

2021-10-21 04:04:55 字數 3020 閱讀 9807

卷積是影象處理中乙個操作,是kernel在影象的每個畫素上的操作。

kernel本質上乙個固定大小的矩陣陣列,其中心點稱為錨點(anchor point)

卷積如何工作

把kernel放到畫素陣列之上,求錨點周圍覆蓋的畫素乘積之和(包括錨點),用來替換錨點覆蓋下畫素點值稱為卷積處理。數學表達如下:

常見運算元

robert運算元

sobel運算元

拉普拉斯運算元

定義運算元的方式

mat robertkernelx =

(mat_<

int>(2

,2)<<1,

0,0,

-1);

自定義卷積模糊

filter2d方法filter2d(

mat src, //輸入影象

mat dst, // 模糊影象

int depth, // 影象深度32/8

mat kernel, // 卷積核/模板

point anchor, // 錨點位置

double delta // 計算出來的畫素+delta

)其中 kernel是可以自定義的卷積核

#include

#include

using

namespace std;

using

namespace cv;

intmain

(int argc,

char

** ar**)

namedwindow

("input"

, window_autosize)

;imshow

("input"

, src);

mat robertkernelx =

(mat_<

int>(2

,2)<<1,

0,0,

-1);

filter2d

(src, dst1,-1

, robertkernelx,

point(-

1,-1

),0,

0);

mat robertkernely =

(mat_<

int>(2

,2)<<0,

1,-1

,0);

filter2d

(src, dst2,-1

, robertkernely,

point(-

1,-1

),0,

0);

mat sobelkernelx =

(mat_<

int>(3

,3)<

,0,1

,-2,

0,2,

-1,0

,1);

filter2d

(src, dst3,-1

, sobelkernelx,

point(-

1,-1

),0,

0); mat sobelkernely =

(mat_<

int>(3

,3)<

,-2,

-1,0

,0,0

,1,2

,1);

filter2d

(src, dst4,-1

, sobelkernely,

point(-

1,-1

),0,

0); mat laplaciankernel =

(mat_<

int>(3

,3)<<0,

-1,0

,-1,

4,-1

,0,-

1,0)

;filter2d

(src, dst5,-1

, laplaciankernel,

point(-

1,-1

),0,

0);//imshow("output1", dst1);

//imshow("output2", dst2);

//imshow("output3", dst3);

//imshow("output4", dst4);

imshow

("output5"

, dst5)

;int ksize =0;

int c =0;

int index =0;

while

(true

) ksize =4+

(index %8)

*2+1

; mat kernel = mat::

ones

(size

(ksize, ksize)

, cv_32f)/(

float

)(ksize * ksize)

;filter2d

(src, dst6,-1

, kernel,

point(-

1,-1

)); index++

;imshow

("custom filter"

, dst6);}

waitkey(0

);return0;

}

opencv學習筆記14(自定義線性濾波)

cv exports w void filter2d inputarray src,outputarray dst,int ddepth,inputarray kernel,point anchor point 1,1 double delta 0,int bordertype border def...

13 自定義類STOCK

stock.h ifndef stock00 h define stock00 h include string class stock class declaration 作用域 為整個類的常量,不能有型別名 static const int four 4 作用域 為整個類的常量,該常量在靜態儲存...

opencv自定義卷積核

include opencv2 imgproc imgproc.hpp include opencv2 highgui highgui.hpp using namespace cv mat get blur kernel int kernel size 獲得歸一化濾波的卷積核 int main in...