OpenCV官方文檔學習記錄(11)

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

製作自己的filter,

主要使用filter2D函數:

先上代碼:

 1 #include "opencv2/opencv.hpp" 2 #include<iostream> 3 #include<string> 4  5 using namespace std; 6 using namespace cv; 7  8 int main(void) 9 {10     Mat src,dst;11 12     Mat kernel;13     Point anthor;14     double delta;15     int kernel_size;16     int ddepth;17     string win_name="filter2D_DEMO";18 19     int c;20 21     src=imread("lena.jpg");22 23     if(!src.data)24         return -1;25     namedWindow(win_name,CV_WINDOW_AUTOSIZE);26 27     anthor=Point(-1,-1);28     delta=0;29     ddepth=-1;30 31     int ind=0;32     while(true)33     {34         c=waitKey(500);35         if((char)c==27)36             break;37         kernel_size=3+2*(ind%5);38         kernel=Mat::ones(kernel_size,kernel_size,CV_32F)/(float)(kernel_size*kernel_size);39 40         filter2D(src,dst,ddepth,kernel,anthor,delta,BORDER_DEFAULT);41         imshow(win_name,dst);42         ++ind;43     }44 45     return 0;46 }

 

然後是CMakeLists.txt

1 cmake_minimum_required(VERSION 2.8)2 project( filter2D_demo1.cpp )3 find_package( OpenCV REQUIRED )4 add_executable( Main filter2D_demo1.cpp )5 target_link_libraries( Main ${OpenCV_LIBS} )

 

運行結果:

說實話這是動態,所以看不到各個結果了。

 

說明以下各個變數作用:

anthor是錨點定義,(-1,-1)是預設在kernel中央。

kernel在這裡使用的是奇數矩陣,並且需要除去他本身行列數的乘積,類似這樣

 

ddepth:結果矩陣大小,預設-1就是與原矩陣一樣(文檔這樣說

ddepth: The depth of dst. A negative value (such as −1) indicates that the depth is the same as the source.)

 

delta:在每個被卷積的像素上附加的值,預設為0;

 

BORDER_DEFAULT:這個參數是預設值,具體沒說,文檔後面可能會提到。

 

重新寫了一個分開的映像:

 1 #include "opencv2/opencv.hpp" 2 #include<iostream> 3 #include<string> 4  5 using namespace std; 6 using namespace cv; 7  8 void showImg(const string &win_name,const Mat &img) 9 {10     namedWindow(win_name,CV_WINDOW_AUTOSIZE);11     imshow(win_name,img);12 }13 14 int main(void)15 {16     Mat src,dst;17 18     Mat kernel;19     Point anthor;20     double delta;21     int kernel_size;22     int ddepth;23 24     src=imread("lena.jpg");25     showImg("Src",src);26 27     if(!src.data)28         return -1;29 30     anthor=Point(-1,-1);31     delta=0;32     ddepth=-1;33 34     int ind=0;35 36     //#Img137     kernel_size=3+2*(ind%5);38     kernel=Mat::ones(kernel_size,kernel_size,CV_32F)/(float)(kernel_size*kernel_size);39     filter2D(src,dst,ddepth,kernel,anthor,delta,BORDER_DEFAULT);40     showImg("Dst1",dst);41 42     ++ind;43     //#Img244     kernel_size=3+2*(ind%5);45     kernel=Mat::ones(kernel_size,kernel_size,CV_32F)/(float)(kernel_size*kernel_size);46     filter2D(src,dst,ddepth,kernel,anthor,delta,BORDER_DEFAULT);47     showImg("Dst2",dst);48 49     ++ind;50     //#Img351     kernel_size=3+2*(ind%5);52     kernel=Mat::ones(kernel_size,kernel_size,CV_32F)/(float)(kernel_size*kernel_size);53     filter2D(src,dst,ddepth,kernel,anthor,delta,BORDER_DEFAULT);54     showImg("Dst3",dst);55 56     waitKey();57 58     return 0;59 }

結果:

 

以上。

OpenCV官方文檔學習記錄(11)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.