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

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

  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为例:

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

最新文章

  1. ansible-playbook
  2. JQuery 快速入门一篇通
  3. mongoDB怎样拷贝一个collection从一个数据库到另一个在同一个主机上
  4. linux命令:ln
  5. Django views 中的 shortcut function
  6. codeforces A. Rook, Bishop and King 解题报告
  7. data属性
  8. C#利用lambda在函数中创建内部函数
  9. iOS 关闭自动锁屏
  10. 让你一分钟认识电子身份验证系统EID
  11. iphone匹配邮箱的正则表达式
  12. nolock引发
  13. 用CSS实现“表格布局”
  14. vue表单详解——小白速会
  15. Cocos2D-ObjC:在RPG游戏中混合Swift代码
  16. Int和String互转的方法
  17. ELK之filebeat
  18. .NET工程师 技能清单
  19. 【BZOJ2216】Lightning Conductor(动态规划)
  20. 虚拟主机wordpress文件上传大小限制更改

热门文章

  1. java 汉字转拼音
  2. VS2015+MySql EF的配置问题
  3. News feed
  4. VS的工程宏,比如$(SolutionDir) 的含义及查找
  5. Spring配置机制的优缺点 - Annotation vs XML
  6. ffmpeg 命令详解
  7. [LeetCode]题解(python):073-Set Matrix Zeroes
  8. poj 3358
  9. 读书笔记: 深入浅出node.js
  10. 从51跳新唐cortex学习3——细说新唐两种定时器