【opencv/core module】(三)Mask operations on matrices

说在前面

Mask operations on matrices

  • 通过 mask matrix 对图像中每个像素的值重新计算,例如:
    [ 0 − 1 0 − 1 5 − 1 0 − 1 0 ] \left[ \begin{matrix} 0 & -1 & 0 \\ -1 & 5 & -1 \\ 0 & -1 & 0 \\ \end{matrix} \right] 010151010

  • I ( i , j ) = 5 ∗ I ( i , j ) − [ I ( i − 1 , j ) + I ( i + 1 , j ) + I ( i , j − 1 ) + I ( i , j + 1 ) ] I(i,j) = 5*I(i,j) - [I(i-1,j) + I(i+1,j) + I(i,j-1) + I(i,j+1)] I(i,j)=5I(i,j)[I(i1,j)+I(i+1,j)+I(i,j1)+I(i,j+1)]
  • 像素的值记为 I I I,例如中间那个像素的值记为 I ( i , j ) I(i,j) I(i,j)
    在这里插入图片描述

Code

  • 主要流程
    • 第一行第一列的这个像素(下图中深蓝色)开始(第0行/第0列/最后一行/最后一列 无意义,没有前一行和前一列、后一行和后一列)
    • 对该像素的每个通道应用前面的计算公式,如下图,深蓝色表示 I ( i , j ) I(i,j) I(i,j)
      浅蓝色分别为
      I ( i , j − 1 ) I ( i − 1 , j ) I ( i + 1 , j ) I ( i , j + 1 ) \begin{matrix} & I(i,j-1) & \\ I(i-1,j) & & I(i+1,j) \\ & I(i,j+1) & \\ \end{matrix} I(i1,j)I(i,j1)I(i,j+1)I(i+1,j)

在这里插入图片描述

  • 具体表现在程序中的时候,我们使用下面的方法来表示上一行、当前行、下一行;

  • 并且由于使用了uchar *,可以访问到每个像素的每个通道,因此我们使用 current[ i - nChannels ] (即 [当前位置 i - 通道数] 的方式) 来访问前一个像素的相同通道的值

       const uchar* previous = myImage.ptr<uchar>(j - 1);
       const uchar* current  = myImage.ptr<uchar>(j    );
       const uchar* next     = myImage.ptr<uchar>(j + 1);
       //......
       5*current[i] - current[i-nChannels] - current[i+nChannels] 
                        - previous[i] - next[i]
    
  • 因为该算法需要访问前一行、后一行、前一列以及后一列的数据,那么图像的首行、首列、尾行、尾列均没有意义,示例demo中直接将其置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));
                        - 
    
  • 代码
void Sharpen(const Mat& myImage,Mat& Result)
{
    CV_Assert(myImage.depth() == CV_8U);  // accept only uchar images

    const int nChannels = myImage.channels();
    Result.create(myImage.size(),myImage.type());

    for(int j = 1 ; j < myImage.rows-1; ++j)
    {
        const uchar* previous = myImage.ptr<uchar>(j - 1);
        const uchar* current  = myImage.ptr<uchar>(j    );
        const uchar* next     = myImage.ptr<uchar>(j + 1);

        uchar* output = Result.ptr<uchar>(j);

        for(int i= nChannels;i < nChannels*(myImage.cols-1); ++i)
        {
            *output++ = saturate_cast<uchar>(5*current[i]
                         -current[i-nChannels] - current[i+nChannels] 
                         - previous[i] - next[i]);
        }
    }
    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));
}
  • saturate_cast
    opencv中用于防止数值溢出的一个函数
uchar a = saturate_cast<uchar>(-100); // a = 0 (UCHAR_MIN)
short b = saturate_cast<short>(33333.33333); // b = 32767 (SHRT_MAX)

The filter2D function

  • opencv封装的方法
Mat kernel = (Mat_<char>(3,3) <<  0, -1,  0,
                                   -1,  5, -1,
                                    0, -1,  0);
filter2D( src, dst1, src.depth(), kernel );
  • depth()函数:返回Mat中通道的类型(在该例中src.depth() 的值为0,表示CV_8U)

    • CV_8U - 8-bit unsigned integers ( 0…255 )
    • CV_8S - 8-bit signed integers ( -128…127 )
    • CV_16U - 16-bit unsigned integers ( 0…65535 )
    • CV_16S - 16-bit signed integers ( -32768…32767 )
    • CV_32S - 32-bit signed integers ( -2147483648…2147483647 )
    • CV_32F - 32-bit floating-point numbers ( -FLT_MAX…FLT_MAX, INF, NAN )
    • CV_64F - 64-bit floating-point numbers ( -DBL_MAX…DBL_MAX, INF, NAN )

测试结果

上面的为Sharpen函数
下面的为Filter2D函数
在这里插入图片描述

图片输出结果二者差不多
在这里插入图片描述


END-2019.6.26

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值