简单指针使用

哈哈,别人都说指针很难,所以我心目中就一直坚守着不要使用指针的原则,天知道...

有时候使用指针不可避免,比如今天看到拉普拉斯的锐化算子:sharpened_pixel= 5*current-left-right-up-down;

void Sharpbyalgorithm()
{
    Mat image = imread("../123.jpg", 1);
    Mat result;
    result.create(image.size(),image.type());
    int nchannels = image.channels();
    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 = nchannels; i < (image.cols - 1)*nchannels; i++)
        {
            *output++ = saturate_cast<uchar>(5 * current[i] - current[i - nchannels] - current[i + nchannels] - previous[i] - next[i]);
        }
    }
    //把未处理的像素设为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));   //最后一列
    imwrite("result.bmp", result);
}


看到current[i - nchannels]时,我有点萌萌的;然后做了个小实验,原来指针和数组之间是这样对应的:

    int *ptr;
    int p[] = { 1,2,3,4,5,6,7,8,9 };
    ptr = &p[0];       //将数组的首地址赋给指针

  int a = ptr[i] ;//通过指针访问数组中第i+1位的数据

哈哈,我知道这是c语言最最基础的部分,也是最最简单的部分,可是也算是我勇敢的踏入了使用指针的第一步。给自己个赞!

特别说明:

saturate_cast 溢出保护

可以看到,opencv中有大量的saturate_cast使用,是为了防止像素内值的溢出。

大致处理方式如下:

if(data<0)      data = 0;  

if(data>255)   data = 255;

这就是cv::saturate_cast<uchar>函数的作
用。如果输入参数是浮点数,就会得到最接近的整数。可以在调用这
个函数时显式地指定其他数据类型,以确保结果在该数据类型定义的
范围之内。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值