【转】UIImage的灰化处理

本文介绍了一种使用Objective-C实现的将彩色图像转换为灰度图像的方法。通过解析和处理UIImage,利用CGColorSpace和CGContext进行颜色空间转换,实现了从RGB到灰度的图像处理过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.sina.com.cn/s/blog_7ccde1bf0100v9oj.html

 

 1 - (UIImage *) convertToGreyscale:(UIImage *)i {
 2      int kRed =  1;
 3      int kGreen =  2;
 4      int kBlue =  4;
 5      int colors = kGreen;
 6      int m_width = i.size.width;
 7      int m_height = i.size.height;
 8     uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height *  sizeof(uint32_t));
 9     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
10     CGContextRef context = CGBitmapContextCreate(rgbImage, m_width, m_height,  8, m_width *  4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
11     CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
12     CGContextSetShouldAntialias(context, NO);
13     CGContextDrawImage(context, CGRectMake( 00, m_width, m_height), [i CGImage]);
14     CGContextRelease(context);
15     CGColorSpaceRelease(colorSpace);
16      //  now convert to grayscale
17      uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height);
18      for( int y =  0; y < m_height; y++) {
19          for( int x =  0; x < m_width; x++) {
20                 uint32_t rgbPixel=rgbImage[y*m_width+x];
21                 uint32_t sum= 0,count= 0;
22                  if (colors & kRed) {sum += (rgbPixel>> 24)& 255; count++;}
23                  if (colors & kGreen) {sum += (rgbPixel>> 16)& 255; count++;}
24                  if (colors & kBlue) {sum += (rgbPixel>> 8)& 255; count++;}
25                 m_imageData[y*m_width+x]=sum/count;
26         }
27     }
28     free(rgbImage);
29      //  convert from a gray scale image back into a UIImage
30      uint8_t *result = (uint8_t *) calloc(m_width * m_height * sizeof(uint32_t),  1);
31      //  process the image back to rgb
32       for( int i =  0; i < m_height * m_width; i++) {
33         result[i* 4]= 0;
34          int val=m_imageData[i];
35         result[i* 4+ 1]=val;
36         result[i* 4+ 2]=val;
37         result[i* 4+ 3]=val;
38     }
39      //  create a UIImage
40      colorSpace = CGColorSpaceCreateDeviceRGB();
41     context = CGBitmapContextCreate(result, m_width, m_height,  8, m_width *  sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
42     CGImageRef image = CGBitmapContextCreateImage(context);
43     CGContextRelease(context);
44     CGColorSpaceRelease(colorSpace);
45     UIImage *resultUIImage = [UIImage imageWithCGImage:image];
46     CGImageRelease(image);
47      //  make sure the data will be released by giving it to an autoreleased NSData
48      [NSData dataWithBytesNoCopy:result length:m_width * m_height];
49      return resultUIImage;
50 }

 

转载于:https://www.cnblogs.com/yencain/articles/2514542.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值