《opencv2》 遍曆映像與鄰網域作業

來源:互聯網
上載者:User

在影像處理中,通過當前位置的相鄰像素計算新的像素值是很常見的操作,當鄰域包含映像的前幾行貨下幾行是,你就需要同時掃描映像的若干行。


本代碼是對映像進行銳利化,它基於拉普拉斯運算元,眾所周知,將一副映像減去它經過拉普拉斯濾波之後的映像,這幅映像的邊緣部分將得到放大,即為細節部分更加銳利,這個銳利化運算元的計算方式為:

sharpene_pixel = 5*current - left - right - up - down;


left,right, up, down分別為當前像素緊挨著的,左右上下。


代碼如下:

#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/core/mat.hpp> #include<cv.h> using namespace cv;
void sharpen(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++ = saturate_cast<uchar>(5*current[i]-current[i-(1*image.channels())] - current[i+(1*image.channels())] - previous[i] - next[i]);    //銳利化運算元計算公式
  } 
 }
 if(image.channels() == 1)              //當映像為單通道映像時,即灰階映像,將未處理的像素設定為0
 {
  result.row(0).setTo(Scalar(0));
  result.row(result.rows-1).setTo(Scalar(0));
  result.col(0).setTo(Scalar(0));
  result.col(result.cols-1).setTo(Scalar(0));
 }else if(image.channels() == 3)             //當映像為rgb映像時
 {
  result.row(0).setTo(Scalar(0, 0, 0));
  result.row(result.rows-1).setTo(Scalar(0, 0, 0));
  result.col(0).setTo(Scalar(0, 0, 0));
  result.col(result.cols-1).setTo(Scalar(0, 0, 0));
 } }
int main()
{  Mat image = imread("lena.png");
 Mat result;
 sharpen(image, result);
 namedWindow("image");
 namedWindow("result");
 imshow("image", image);
 imshow("result", result);
 waitKey(0);
 return 0;
}

聯繫我們

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