2013-04-20 120 views
2

请看看下面的代码锐化图像 - 访问相邻像素

#include <iostream> 
#include <opencv2\core\core.hpp> 
#include <opencv2\highgui\highgui.hpp> 

using namespace std; 
using namespace cv; 

void Sharpen(Mat &,Mat); 

int main() 
{ 
    Mat image,result; 

    try 
    { 
     image = imread("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg"); 

     if(!image.data) 
     { 
      throw 1; 
     } 
    } 
    catch(int i) 
    { 
     cout << "Image is unable to reae" << endl; 
    } 

    Sharpen(image,result); 
    waitKey(0); 
} 

void Sharpen(Mat &image,Mat result) 
{ 
    //result = image.clone(); 
    result.create(image.size(), image.type()); 

    //For all rows except first and last 
    for(int i=1;i<image.rows-1;i++) 
    { 
     const uchar *previous = image.ptr<const uchar>(i-1); 
     const uchar *next = image.ptr<const uchar>(i+1); 
     const uchar *current = image.ptr<const uchar>(i); 

     uchar *output = result.ptr<uchar>(i); 

     //For all columns except first and last 
     for(int a=1;a<image.cols-1;a++) 
     { 
      *output++ = cv::saturate_cast<uchar>(5*current[a]-current[a-1]-current[a+1]-previous[a]-next[a]); 
     } 

    } 

    result.row(0).setTo(cv::Scalar(0)); 
    result.row(result.rows-1).setTo(cv::Scalar(0)); 
    result.col(0).setTo(cv::Scalar(0)); 
    result.col(result.cols-1).setTo(cv::Scalar(0)); 

    namedWindow("Original"); 
    imshow("Original",image); 

    namedWindow("Duplicate"); 
    imshow("Duplicate",result); 
} 

在这里,我一个试图做的是,试图锐化图像。以下是图像锐化的公式。

resultPixel = 5*currentPixel-previousPixel-nextPixel-upperPixel-belowPixel 

不管怎样,下面是输出我得到

enter image description here

正如你所看到的,我没有得到预期的结果。什么做错了?请帮忙!

回答

3

你的第一个问题是,每个像素有3个字节,所以你只是遍历三分之一的图像。你的另一个问题是你不能做* output ++,因为你想跳过第一个像素。把这两样东西放在一起会产生下面的代码,它给出你正在寻找的结果。

#include <iostream> 
#include <opencv2\core\core.hpp> 
#include <opencv2\highgui\highgui.hpp> 

using namespace std; 
using namespace cv; 

void Sharpen(Mat &image,Mat& result) 
{ 
    result.create(image.size(), image.type()); 

    //For all rows except first and last 
    for(int i=1;i<image.rows-1;i++) 
    { 
     const uchar *previous = image.ptr<const uchar>(i-1); 
     const uchar *next = image.ptr<const uchar>(i+1); 
     const uchar *current = image.ptr<const uchar>(i); 

     uchar *output = result.ptr<uchar>(i); 

     //For all columns except first and last 
     for(int a=3;a<(image.cols-1)*3;a++) 
     { 
      output[a] = cv::saturate_cast<uchar>(5*current[a]-current[a-1]-current[a+1]-previous[a]-next[a]); 
     } 

    } 

    result.row(0).setTo(cv::Scalar(0)); 
    result.row(result.rows-1).setTo(cv::Scalar(0)); 
    result.col(0).setTo(cv::Scalar(0)); 
    result.col(result.cols-1).setTo(cv::Scalar(0)); 

    namedWindow("Original"); 
    imshow("Original",image); 

    namedWindow("Duplicate"); 
    imshow("Duplicate",result); 

} 

int main() 
{ 
    Mat image,result; 

    try 
    { 
     image = imread("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg"); 

     if(!image.data) 
     { 
      throw 1; 
     } 
    } 
    catch(int i) 
    { 
     cout << "Image is unable to reae" << endl; 
    } 

    Sharpen(image,result); 
    waitKey(0); 
} 
+0

太棒了!谢谢!我想我的方式只适用于一个频道的图像(灰度)不是吗? – 2013-04-20 16:51:51