【问题标题】:dsp on an UIImage [duplicate]UIImage上的dsp [重复]
【发布时间】:2012-10-04 17:34:28
【问题描述】:

可能重复:
How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?

假设我有一个 UIImage,我想获取它的 rgb 矩阵,以便对其进行一些处理,而不是更改它,只需获取 UIImage 数据,这样我就可以使用我的 C 算法在上面。 您可能知道,所有的数学运算都是在图像 rgb 矩阵上完成的。

【问题讨论】:

  • @userxxx 而不是随机插入不相关的代码,让您的问题达到 SO 的质量标准。没有其他办法。
  • 在你这样做之前,使用搜索!
  • @H2CO3 那么,你能教我怎么做吗?我想知道我必须改变什么才能使我成为“质量”?这让我很感兴趣。
  • @user1280535 你必须发布一个关于特定问题的特定问题(据我所知,这个问题很好)。您的问题应该用良好且易于理解的英语写成。然后你应该确保你的问题不能使用简单的谷歌搜索或 StackOverflow 上的搜索来回答(这个可以)。为了完成这一切,不欢迎离题或垃圾邮件(自我推销和广告)非问题。

标签: objective-c ios


【解决方案1】:

基本过程是使用CGBitmapContextCreate 创建位图上下文,然后将图像绘制到该上下文中并使用CGBitmapContextGetData 获取内部数据。这是一个例子:

UIImage *image = [UIImage imageNamed:@"MyImage.png"];

//Create the bitmap context:
CGImageRef cgImage = [image CGImage];
size_t width = CGImageGetWidth(cgImage);
size_t height = CGImageGetHeight(cgImage);
size_t bitsPerComponent = 8;
size_t bytesPerRow = width * 4;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGContextRef context = CGBitmapContextCreate(NULL, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
//Draw your image into the context:
CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);
//Get the raw image data:
unsigned char *data = CGBitmapContextGetData(context);

//Example how to access pixel values:
size_t x = 0;
size_t y = 0;
size_t i = y * bytesPerRow + x * 4;
unsigned char redValue = data[i];
unsigned char greenValue = data[i + 1];
unsigned char blueValue = data[i + 2];
unsigned char alphaValue = data[i + 3];
NSLog(@"RGBA at (%i, %i): %i, %i, %i, %i", x, y, redValue, greenValue, blueValue, alphaValue);

//Clean up:
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
//At this point, your data pointer becomes invalid, you would have to allocate
//your own buffer instead of passing NULL to avoid this.

【讨论】:

    猜你喜欢
    • 2013-05-07
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多