opencv 图像锐化

一利用指针

#include 
#include 
#include 
#include 
void  sharpen(const cv::Mat &image,cv::Mat &result)
{
    result.create(image.size(),image.type());
    int ichannels=image.channels();

    for(int i=1;i1;i++)
    {
        const uchar* previous=
                image.ptr<const uchar>(i-1);
        const uchar* current=
                image.ptr<const uchar>(i);
        const uchar* next=
                image.ptr<const uchar>(i+1);

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

        for(int j=ichannels;j<(image.cols-1)*ichannels;j++)
        {
        //带入锐化的计算公式
            *output=cv::saturate_cast<uchar>
            (5*current[j]-current[j-ichannels]
                   -current[j+ichannels]-previous[j]-next[j]);
            output++;
        }
    }
    //讲边缘设置为0
    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));
}

二利用滤波函数filter2D

#include 
#include 
#include 
void sharpen2D(const cv::Mat &image,cv::Mat &result)
{
    cv::Mat kernel(3,3,CV_32F,cv::Scalar(0));

    kernel.at<float>(0,1)=-1;
    kernel.at<float>(1,0)=-1;
    kernel.at<float>(2,1)=-1;
    kernel.at<float>(1,2)=-1;
    kernel.at<float>(1,1)=5;

    cv::filter2D(image,result,image.depth(),kernel);
}

你可能感兴趣的:(opencv,c++)