opencv2—(4)遍曆映像和鄰網域作業

來源:互聯網
上載者:User
在影像處理中,對當前位置像素的相鄰像素計算新的像素值是很常見的操作,當鄰域包括映像的前幾行和下幾行時,你就需要同時掃描映像的若干行 本篇介紹的這個例子是對映像進行銳利化,它是基於拉普拉斯運算元的。將一幅映像減去它經過拉普拉斯濾波之後的映像,這幅映像的邊緣部分得到放大,即細節部分得到銳利化,這個銳利化的運算元計算方式如下: sharpened——pixel=5*current-left-right-up-down; //彩色圖片銳利化 void sharpend( const Mat & image , Mat & result ) {         result .create( image .size () , image .type());         for ( int j = 1; j < image .rows-1; j++)        {                const uchar * previous = image .ptr< const uchar >(j - 1);                const uchar * current = image .ptr< const uchar >(j);                const uchar * next = image .ptr< const uchar >(j + 1);                uchar * output = result .ptr< uchar >(j);                for ( int i = 1* image .channels(); i < ( image .cols-1)* image .channels(); i++)               {                      output[i] = cv::saturate_cast< uchar >                                 (5 * current[i] - previous[i]-current[i-3]-current[i+3]-next[i]);               }        }         result .row(0).setTo(cv:: Scalar (0,0,0));         result .row( result .rows-1).setTo(cv:: Scalar (0,0,0));         result .col(0).setTo(cv:: Scalar (0,0,0));         result .col( result .cols - 1).setTo(cv:: Scalar (0,0,0)); } 其中模板函數 cv::saturate_cast用來對計算結果進行截斷; cv:: Scalar (0,0,0)指定像素的三個通道的目標值
當然,對於本例子中的銳利化濾波器,核矩陣為: [ 0 -1 0; -1 5 -1; 0 -1 1]這樣一個3*3 的矩陣,OpenCV定義了一個特殊的函數來完成濾波處理: cv::filter2D 下面是本例的第二種實現方法:
void sharpen2D( const Mat & image , Mat & result ) {         Mat kernel(3, 3, CV_32F , cv:: Scalar (0));        kernel.at< float >(1, 1) = 5.0;        kernel.at< float >(0, 1) = -1.0;        kernel.at< float >(1, 0) = -1.0;        kernel.at< float >(1, 2) = -1.0;        kernel.at< float >(2, 1) = -1.0;        cv::filter2D( image , result , image .depth(), kernel); }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.