美文网首页
OpenCV for iOS 学习笔记(二)—— 图像矩阵的掩

OpenCV for iOS 学习笔记(二)—— 图像矩阵的掩

作者: FLNuo | 来源:发表于2016-10-18 14:38 被阅读182次

环境配置 :

OpenCV在xCode中的安装与环境配置

矩阵掩码操作:根据掩码矩阵(也称作核)重新计算图像中每个像素的值。掩码矩阵中的值表示近邻像素值(包括该像素自身的值)对新像素值有多大影响。从数学观点看,我们用自己设置的权值,对像素邻域内的值做了个加权平均。

运行效果:

原图 处理后

代码示例:

void Sharpen(const Mat& myImage, Mat& Result, int n) { 

        CV_Assert(myImage.depth() == CV_8U);  

        Result.create(myImage.size(),myImage.type()); 

        const int nChannels =       myImage.channels();      

        for(int j = 1 ; j < myImage.rows-1; ++j)    {       

                 const uchar* previous = myImage.ptr(j - 1);   

                 const uchar* current  = myImage.ptr(j ); 

                 const uchar* next    = myImage.ptr(j + 1);

                 uchar* output = Result.ptr(j); 

                 for(int i= nChannels; i < nChannels*(myImage.cols-1); ++i)        {        

                          *output++ = saturate_cast(n * 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));      // 右

}

调用方式:

Mat myImage, result;

UIImage *img = [UIImage imageNamed:@"imageName"];

UIImageToMat(img, myImage);   // 将UIImage对象转换成 Mat

Sharpen(myImage, result, n);   // myImage:预处理对象 result:处理结果 n:可以随便填试试 建议在 5+-n

UIImage *img2 = MatToUIImage(result);     // 将Mat转换成 UIImage 对象

参考资料: 矩阵的掩码操作

相关文章

网友评论

      本文标题: OpenCV for iOS 学习笔记(二)—— 图像矩阵的掩

      本文链接:https://www.haomeiwen.com/subject/loctuttx.html