基于opencv(C++)的图像处理(图像锐化)


前言

上次了解完图片的存储方式之后,接下来就可以设置一些算子来进行图像处理了。

一、图像锐化是什么?

图像锐化(image sharpening)是补偿图像的轮廓,增强图像的边缘及灰度跳变的部分,使图像变得清晰,分为空间域处理和频域处理两类。图像锐化是为了突出图像上地物的边缘、轮廓,或某些线性目标要素的特征。这种滤波方法提高了地物边缘与周围像元之间的反差,因此也被称为边缘增强。

二、代码实现

1.图像锐化算法:

代码如下(示例):

Mat & imgSharpen(const Mat & img, char * arith)       //arith为3*3模板算子
{
    int rows = img.rows;        //原图的行
    int cols = img.cols * img.channels();   //原图的列
    int offsetx = img.channels();       //像素点的偏移量

    static Mat dst = Mat::ones(img.rows-2, img.cols-2, img.type());

    for(int i = 1; i < rows - 1; i++)
    {
        const uchar* previous = img.ptr<uchar>(i - 1);
        const uchar* current = img.ptr<uchar>(i);
        const uchar* next = img.ptr<uchar>(i + 1);
        uchar * output = dst.ptr<uchar>(i-1);
        for(int j = offsetx ; j < cols - offsetx; j++)
        {
            output[j - offsetx] =
            saturate_cast<uchar>( previous[j-offsetx]*arith[0] + previous[j]*arith[1] + previous[j+offsetx]*arith[2] +
                                  current[j-offsetx]*arith[3]  + current[j]*arith[4]  + current[j+offsetx]*arith[5]  +
                                  next[j-offsetx]*arith[6]     + next[j]*arith[7]     + next[j-offsetx]*arith[8] );
        }
    }
    return dst;
}

2.测试代码:

代码如下(示例):

int main()
{
    Mat img = imread("../img/face.jpg");
    namedWindow("原图像", WINDOW_NORMAL);
    imshow("原图像", img);

    cout << "rows = " << img.rows << endl;      //行
    cout << "cols = " << img.cols << endl;      //列
    cout << "channels = " << img.channels() << endl;      //通道

    char arith[9] = {0, -1, 0, -1, 5, -1, 0, -1, 0};       //使用拉普拉斯算子

    Mat dst1 = imgSharpen(img, arith);

    namedWindow("锐化图像", WINDOW_NORMAL);
    imshow("锐化图像", dst1);

    waitKey(0);

    return 0;
}

3.结果:

在这里插入图片描述
可以发现图片的边缘明显增强了。

  • 2
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值