2020-12-22

UIImage与RGBA、BGR、GrayScale

Evans_Xiao已关注

0.0762017.04.12 11:09:38字数 277阅读 1,916

格式类型:

RBGA - 包含Alpha通道

RGB

BGR

Gray - 灰度图

首先理解一张图片的组成,每张图片都是由无数个有序排列的带有颜色的像素点(Pixel)组成的,这些Pixel的排列都可以理解为一个二维数组,每个Pixel都是一个颜色点,即包含RGBA的Pixel。

什么是RGBA呢

R - Red

G - Green

B - Blue

A - Alpha 透明度

1、将UIImage转为RGBA格式

-(unsignedchar*)getRGBAWithImage:(UIImage*)image{intRGBA=4;CGImageRef imageRef=[image CGImage];size_t width=CGImageGetWidth(imageRef);size_t height=CGImageGetHeight(imageRef);CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();unsignedchar*rawData=(unsignedchar*)malloc(width*height*sizeof(unsignedchar)*RGBA);NSUInteger bytesPerPixel=RGBA;NSUInteger bytesPerRow=bytesPerPixel*width;NSUInteger bitsPerComponent=8;CGContextRef context=CGBitmapContextCreate(rawData,width,height,bitsPerComponent,bytesPerRow,colorSpace,kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big);CGContextDrawImage(context,CGRectMake(0,0,width,height),imageRef);CGColorSpaceRelease(colorSpace);CGContextRelease(context);returnrawData;}

继续上面的工作,获取这张图片中某个Pixel的RGBA值

-(void)getRGBAFromImage:(UIImage*)image atX:(int)xx andY:(int)yy{intRGBA=4;CGImageRef imageRef=[image CGImage];size_t width=CGImageGetWidth(imageRef);size_t height=CGImageGetHeight(imageRef);// 从image的data buffer中取得影像,放入格式化后的rawData中CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();unsignedchar*rawData=(unsignedchar*)malloc(height*width*RGBA);NSUInteger bytesPerPixel=RGBA;NSUInteger bytesPerRow=bytesPerPixel*width;NSUInteger bitsPerComponent=8;CGContextRef context=CGBitmapContextCreate(rawData,width,height,bitsPerComponent,bytesPerRow,colorSpace,kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big);CGColorSpaceRelease(colorSpace);// 清空CGContextRef再绘制CGContextClearRect(context,CGRectMake(0.0,0.0,width,height));CGContextDrawImage(context,CGRectMake(0,0,width,height),imageRef);CGContextRelease(context);// 将XY坐标转成一维数组unsignedlongbyteIndex=(bytesPerRow*yy)+(bytesPerPixel*xx);// 取得RGBA位的数据CGFloat red=rawData[byteIndex];CGFloat green=rawData[byteIndex+1];CGFloat blue=rawData[byteIndex+2];CGFloat alpha=rawData[byteIndex+3];// 利用RGB计算灰阶的亮度值CGFloat gray=(red+green+blue)/3;// 输出NSLog(@"%@",[NSString stringWithFormat:@"%.2f",red]);NSLog(@"%@",[NSString stringWithFormat:@"%.2f",green]);NSLog(@"%@",[NSString stringWithFormat:@"%.2f",blue]);NSLog(@"%@",[NSString stringWithFormat:@"%.2f",alpha]);NSLog(@"%@",[NSString stringWithFormat:@"%.2f",gray]);free(rawData);}

2、将图片转为BGR格式

转换为BGR格式,顾名思义,BGR与RGBA,我们只需要把RGBA数据取到,去掉Alpha,再调换一下RGB的顺序就可以了,下面是具体调换代码:

-(unsignedchar*)getBGRWithImage:(UIImage*)image{intRGBA=4;intRGB=3;CGImageRef imageRef=[image CGImage];size_t width=CGImageGetWidth(imageRef);size_t height=CGImageGetHeight(imageRef);CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();unsignedchar*rawData=(unsignedchar*)malloc(width*height*sizeof(unsignedchar)*RGBA);NSUInteger bytesPerPixel=RGBA;NSUInteger bytesPerRow=bytesPerPixel*width;NSUInteger bitsPerComponent=8;CGContextRef context=CGBitmapContextCreate(rawData,width,height,bitsPerComponent,bytesPerRow,colorSpace,kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big);CGContextDrawImage(context,CGRectMake(0,0,width,height),imageRef);CGColorSpaceRelease(colorSpace);CGContextRelease(context);unsignedchar*tempRawData=(unsignedchar*)malloc(width*height*3*sizeof(unsignedchar));for(inti=0;i

什么是Gray灰度?

灰度(Gray scale)数字图像是每个像素只有一个采样颜色的图像。这类图像通常显示为从最暗黑色到最亮的白色的灰度

1、将一张图片转为灰度图

-(unsignedchar*)getGrayWithImage:(UIImage*)image{intGRAY=1;// 获取灰度图CGImageRef imageRef=[image CGImage];intwidth=image.size.width;intheight=image.size.height;unsignedchar*rawData=(unsignedchar*)malloc(width*height*sizeof(unsignedchar));CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceGray();NSUInteger bytesPerPixel=GRAY;NSUInteger bytesPerRow=bytesPerPixel*width;NSUInteger bitsPerComponent=8;CGContextRef context=CGBitmapContextCreate(rawData,width,height,bitsPerComponent,bytesPerRow,colorSpace,0);CGContextDrawImage(context,CGRectMake(0,0,width,height),imageRef);CGColorSpaceRelease(colorSpace);CGContextRelease(context);returnrawData;}

参考资料

彩笔的笔记本

你可能感兴趣的:(2020-12-22)