Objective c 基于图像的dsp

Objective c 基于图像的dsp,objective-c,ios,Objective C,Ios,可能重复: 假设我有一个UIImage,我想得到它的rgb矩阵,为了对它进行一些处理,而不是改变它,只需要得到UIImage数据,这样我就可以在上面使用我的C算法。 您可能知道,所有的数学运算都是在图像rgb矩阵上完成的。基本过程是使用CGBitmapContextCreate创建位图上下文,然后将图像绘制到该上下文中,并使用CGBitmapContextGetData获取内部数据。下面是一个例子: UIImage *image = [UIImage imageNamed:@"MyImage.

可能重复:

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

基本过程是使用
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.

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