关于图像处理中的卷积运算,这里有两份简明扼要的介绍:文一文二

其中,可能的一种卷积运算代码如下:

  1. - (UIImage*)applyConvolution:(NSArray*)kernel
  2. {
  3. CGImageRef inImage = self.CGImage;
  4. CFDataRef m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
  5. CFDataRef m_OutDataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
  6. UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
  7. UInt8 * m_OutPixelBuf = (UInt8 *) CFDataGetBytePtr(m_OutDataRef);
  8. int h = CGImageGetHeight(inImage);
  9. int w = CGImageGetWidth(inImage);
  10. int kh = [kernel count] / 2;
  11. int kw = [[kernel objectAtIndex:0] count] / 2;
  12. int i = 0, j = 0, n = 0, m = 0;
  13. for (i = 0; i < h; i++) {
  14. for (j = 0; j < w; j++) {
  15. int outIndex = (i*w*4) + (j*4);
  16. double r = 0, g = 0, b = 0;
  17. for (n = -kh; n <= kh; n++) {
  18. for (m = -kw; m <= kw; m++) {
  19. if (i + n >= 0 && i + n < h) {
  20. if (j + m >= 0 && j + m < w) {
  21. double f = [[[kernel objectAtIndex:(n + kh)] objectAtIndex:(m + kw)] doubleValue];
  22. if (f == 0) {continue;}
  23. int inIndex = ((i+n)*w*4) + ((j+m)*4);
  24. r += m_PixelBuf[inIndex] * f;
  25. g += m_PixelBuf[inIndex + 1] * f;
  26. b += m_PixelBuf[inIndex + 2] * f;
  27. }
  28. }
  29. }
  30. }
  31. m_OutPixelBuf[outIndex]     = SAFECOLOR((int)r);
  32. m_OutPixelBuf[outIndex + 1] = SAFECOLOR((int)g);
  33. m_OutPixelBuf[outIndex + 2] = SAFECOLOR((int)b);
  34. m_OutPixelBuf[outIndex + 3] = 255;
  35. }
  36. }
  37. CGContextRef ctx = CGBitmapContextCreate(m_OutPixelBuf,
  38. CGImageGetWidth(inImage),
  39. CGImageGetHeight(inImage),
  40. CGImageGetBitsPerComponent(inImage),
  41. CGImageGetBytesPerRow(inImage),
  42. CGImageGetColorSpace(inImage),
  43. CGImageGetBitmapInfo(inImage)
  44. );
  45. CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
  46. CGContextRelease(ctx);
  47. UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
  48. CGImageRelease(imageRef);
  49. CFRelease(m_DataRef);
  50. CFRelease(m_OutDataRef);
  51. return finalImage;
  52. }

方法的参数kernel是卷积运算中的卷积核,下面是几种滤镜的卷积核:

  1. #pragma mark -
  2. #pragma mark - Basic Convolutions
  3. /* Reference :
  4. * http://docs.gimp.org/en/plug-in-convmatrix.html
  5. */
  6. - (UIImage *)sharpen
  7. {
  8. //  double dKernel[5][5] = {
  9. //      {0,  0.0, -1.0,  0.0, 0},
  10. //      {0, -1.0,  5.0, -1.0, 0},
  11. //      {0,  0.0, -1.0,  0.0, 0}
  12. //    };
  13. double dKernel[5][5] = {
  14. {0, 0.0, -0.2,  0.0, 0},
  15. {0, -0.2, 1.8, -0.2, 0},
  16. {0, 0.0, -0.2,  0.0, 0}
  17. };
  18. NSMutableArray *kernel = [[[NSMutableArray alloc] initWithCapacity:5] autorelease];
  19. for (int i = 0; i < 5; i++) {
  20. NSMutableArray *row = [[[NSMutableArray alloc] initWithCapacity:5] autorelease];
  21. for (int j = 0; j < 5; j++) {
  22. [row addObject:[NSNumber numberWithDouble:dKernel[i][j]]];
  23. }
  24. [kernel addObject:row];
  25. }
  26. return [self applyConvolution:kernel];
  27. }
  28. - (UIImage *)edgeEnhance
  29. {
  30. double dKernel[5][5] = {
  31. {0,  0.0,  0.0,  0.0, 0},
  32. {0, -1.0,  1.0,  0.0, 0},
  33. {0,  0.0,  0.0,  0.0, 0}
  34. };
  35. NSMutableArray *kernel = [[[NSMutableArray alloc] initWithCapacity:5] autorelease];
  36. for (int i = 0; i < 5; i++) {
  37. NSMutableArray *row = [[[NSMutableArray alloc] initWithCapacity:5] autorelease];
  38. for (int j = 0; j < 5; j++) {
  39. [row addObject:[NSNumber numberWithDouble:dKernel[i][j]]];
  40. }
  41. [kernel addObject:row];
  42. }
  43. return [self applyConvolution:kernel];
  44. }
  45. - (UIImage *)edgeDetect
  46. {
  47. double dKernel[5][5] = {
  48. {0,  0.0,  1.0,  0.0, 0},
  49. {0,  1.0, -4.0,  1.0, 0},
  50. {0,  0.0,  1.0,  0.0, 0}
  51. };
  52. NSMutableArray *kernel = [[[NSMutableArray alloc] initWithCapacity:5] autorelease];
  53. for (int i = 0; i < 5; i++) {
  54. NSMutableArray *row = [[[NSMutableArray alloc] initWithCapacity:5] autorelease];
  55. for (int j = 0; j < 5; j++) {
  56. [row addObject:[NSNumber numberWithDouble:dKernel[i][j]]];
  57. }
  58. [kernel addObject:row];
  59. }
  60. return [self applyConvolution:kernel];
  61. }
  62. - (UIImage *)emboss
  63. {
  64. double dKernel[5][5] = {
  65. {0, -2.0, -1.0,  0.0, 0},
  66. {0, -1.0,  1.0,  1.0, 0},
  67. {0,  0.0,  1.0,  2.0, 0}
  68. };
  69. NSMutableArray *kernel = [[[NSMutableArray alloc] initWithCapacity:5] autorelease];
  70. for (int i = 0; i < 5; i++) {
  71. NSMutableArray *row = [[[NSMutableArray alloc] initWithCapacity:5] autorelease];
  72. for (int j = 0; j < 5; j++) {
  73. [row addObject:[NSNumber numberWithDouble:dKernel[i][j]]];
  74. }
  75. [kernel addObject:row];
  76. }
  77. return [self applyConvolution:kernel];
  78. }

在此基础上,我Google了下Photoshop中对照片进行黑白处理的简单步骤:

  1. 去色
  2. 调整对比度
  3. 高斯模糊
  4. 浮雕效果
  5. 边缘检测
  6. 调整对比度
  7. 调整亮度
  8. 反相

我按步骤实现了相应代码:

  1. return [[[[[[[[originImage desaturate]
  2. changeContrastByFactor:1.5]
  3. gaussianBlur:1.3] emboss]
  4. edgeDetect]
  5. changeContrastByFactor:1.5]
  6. changeBrightnessByFactor:1.5]
  7. invert];

可惜效果有点粗糙,照片仍旧以上一篇文章中的Andy为例:

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS中的图像处理(二)——卷积运算的更多相关文章

  1. iOS中的图像处理(三)——混合运算

    有时候,单独对一张图像进行处理是很难或者根本达不到我们想要的效果的.一个好的滤镜效果的诞生,往往要经过很多复杂步骤.细致微调.图片应用效果观察以及很多图层叠加. 我在JSWidget上发现了一些常用混 ...

  2. ios 中的构造方法(二)

    在之前有简单介绍了构造方法的结构,以及构造方法的作用,那么我们现在来讨论一下: 对象的创建分为两步:+ alloc 分配内存空间和 -init 进行初始化 那么在继承自 NSObject 的类当中,我 ...

  3. iOS中的图像处理(一)——基础滤镜

    最近在稍微做一些整理,翻起这部分的代码,发现是两个多月前的了. 这里讨论的是基于RGBA模型下的图像处理,即将变换作用在每个像素上. 代码是以UIImage的category形式存在的: typede ...

  4. [OpenCV-Python] OpenCV 中的图像处理 部分 IV (二)

    部分 IVOpenCV 中的图像处理 OpenCV-Python 中文教程(搬运)目录 16 图像平滑 目标 • 学习使用不同的低通滤波器对图像进行模糊 • 使用自定义的滤波器对图像进行卷积(2D 卷 ...

  5. ios 中生成二维码和相册中识别二维码

    iOS 使用CIDetector扫描相册二维码.原生扫描 原生扫描 iOS7之后,AVFoundation让我们终于可以使用原生扫描进行扫码了(二维码与条码皆可)AVFoundation可以让我们从设 ...

  6. Quartz 2D在ios中的使用简述二:创建画布

    在iOS中使用Quartz画图时,第一步就是要获取画布(图形上下文),然后再画布上做各种操作.先看下CoreGraphics.h这个头文件,就可以知道能够创建多少种上下文类型. #include &l ...

  7. 卷积运算的本质,以tensorflow中VALID卷积方式为例。

    卷积运算在数学上是做矩阵点积,这样可以调整每个像素上的BGR值或HSV值来形成不同的特征.从代码上看,每次卷积核扫描完一个通道是做了一次四重循环.下面以VALID卷积方式为例进行解释. 下面是pyth ...

  8. iOS中的二维数组

    首先我们知道OC中是没有二维数组的,二维数组是通过一位数组的嵌套实现的,但是别忘了我们有字面量,实际上可以和C/C++类似的简洁地创建和使用二维数组.这里总结了创建二维数组的两种方法以及数组的访问方式 ...

  9. iOS中的crash防护(二)KVC造成的crash

      接上篇< iOS中的crash防护(一)unrecognized selector sent to instance> 我们攻克了找不到方法实现的crash,这一篇我这里主要分析一下在 ...

随机推荐

  1. Objective-c 深浅复制

    深浅复制的定义: 浅复制:在复制时,对于被复制对象的每一层都是指针复制. 深复制:在复制时,对于被复制的对象至少有一层是对象复制. 完全复制:在复制时,对于被复制对象的每一层都是完全复制. retai ...

  2. ExtJs004define定义类

    Ext.onReady(function () { //在Ext中如何去定义一个类: Ext.define(className , properties , callback) Ext.define( ...

  3. 如何交叉编译开源库-->编译c-ares库从失败到成功的过程[ocean]

    编译c-ares库从失败到成功的过程c-ares-master: apt-get install libtool   ####https://github.com/c-ares/c-ares ==== ...

  4. iOS __func__标识符

    iOS底层是GCC,所以也支持 __func__标识符,用于打印当前函数名,可以说是排错利器. // data should not be nil nor empty ) {// Exception ...

  5. C#中网站根路径、应用根路径、物理路径、绝对路径,虚拟路径的区别

    C#中网站根路径,请站点的最外一层 /表示 应用根路径 ~/表示,有时候C#程序路径并不是网站路径 物理路径 server.mappath("~/") 是指应用程序放在服务器硬盘的 ...

  6. 数据可视化开源系统(python开发)

    Caravel 是 Airbnb (知名在线房屋短租公司)开源的数据探查与可视化平台(曾用名Panoramix),该工具在可视化.易用性和交互性上非常有特色,用户可以轻松对数据进行可视化分析. 核心功 ...

  7. 高质量程序设计指南C/C++语言——malloc/free使用要点

  8. Jacob

    http://www.cnblogs.com/luckyxiaoxuan/archive/2012/06/13/2548331.html http://blog.csdn.net/qingwangyo ...

  9. VS2012 EF5 连接oracle11.2

    1.安装ODAC 11.2 Release 5 and Oracle Developer Tools for Visual Studio (11.2.0.3.20). 注:支持VS2010和VS201 ...

  10. Apache RewriteCond RewriteRule 入门和Laravel去掉index.php

        Ci删除index.php办法: 创建.htaccess 文件放到网站的根目录下,文件中的内容如下: RewriteEngine onRewriteCond %{REQUEST_FILENAM ...