首先感谢 AmyLiao 关于OpenCV的文章
实现效果
一、OpenCV在iOS中的安装与环境配置
请参考//www.greatytc.com/p/5b0600a618e9
二、矩阵掩码
首先创建两个UIImageView
@property (weak, nonatomic) IBOutlet UIImageView *imageView; @property (weak, nonatomic) IBOutlet UIImageView *imgview;
然后实现功能
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<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)); // 右 }
在viewDidLoad中实现下面代码
self.imgview.image = [UIImage imageNamed:@"1.jpg"]; Mat myImage, result; UIImage *img = [UIImage imageNamed:@"1.jpg"]; UIImageToMat(img, myImage); Sharpen(myImage, result, 5); self.imageView.image = MatToUIImage(result);