格式類型:
RBGA - 包含Alpha通道
RGB
BGR
Gray - 灰度圖
首先理解一張圖片的組成,每張圖片都是由無數(shù)個有序排列的帶有顏色的像素點(diǎn)(Pixel)組成的,這些Pixel的排列都可以理解為一個二維數(shù)組,每個Pixel都是一個顏色點(diǎn),即包含RGBA的Pixel。
什么是RGBA呢
R - Red
G - Green
B - Blue
A - Alpha 透明度
1、將UIImage轉(zhuǎn)為RGBA格式
- (unsigned char *)getRGBAWithImage:(UIImage *)image
{
int RGBA = 4;
CGImageRef imageRef = [image CGImage];
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char *) malloc(width * height * sizeof(unsigned char) * 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);
return rawData;
}
繼續(xù)上面的工作,獲取這張圖片中某個Pixel的RGBA值
- (void)getRGBAFromImage:(UIImage *)image atX:(int)xx andY:(int)yy
{
int RGBA = 4;
CGImageRef imageRef = [image CGImage];
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
// 從image的data buffer中取得影像,放入格式化后的rawData中
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char *)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坐標(biāo)轉(zhuǎn)成一維數(shù)組
unsigned long byteIndex = (bytesPerRow * yy) + (bytesPerPixel * xx);
// 取得RGBA位的數(shù)據(jù)
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、將圖片轉(zhuǎn)為BGR格式
轉(zhuǎn)換為BGR格式,顧名思義,BGR與RGBA,我們只需要把RGBA數(shù)據(jù)取到,去掉Alpha,再調(diào)換一下RGB的順序就可以了,下面是具體調(diào)換代碼:
- (unsigned char *)getBGRWithImage:(UIImage *)image
{
int RGBA = 4;
int RGB = 3;
CGImageRef imageRef = [image CGImage];
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char *) malloc(width * height * sizeof(unsigned char) * 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);
unsigned char * tempRawData = (unsigned char *)malloc(width * height * 3 * sizeof(unsigned char));
for (int i = 0; i < width * height; i ++) {
NSUInteger byteIndex = i * RGBA;
NSUInteger newByteIndex = i * RGB;
// Get RGB
CGFloat red = rawData[byteIndex + 0];
CGFloat green = rawData[byteIndex + 1];
CGFloat blue = rawData[byteIndex + 2];
//CGFloat alpha = rawData[byteIndex + 3];// 這里Alpha值是沒有用的
// Set RGB To New RawData
tempRawData[newByteIndex + 0] = blue; // B
tempRawData[newByteIndex + 1] = green; // G
tempRawData[newByteIndex + 2] = red; // R
}
return tempRawData;
}
什么是Gray灰度?
灰度(Gray scale)數(shù)字圖像是每個像素只有一個采樣顏色的圖像。這類圖像通常顯示為從最暗黑色到最亮的白色的灰度
1、將一張圖片轉(zhuǎn)為灰度圖
- (unsigned char *)getGrayWithImage:(UIImage *)image
{
int GRAY = 1;
// 獲取灰度圖
CGImageRef imageRef = [image CGImage];
int width = image.size.width;
int height = image.size.height;
unsigned char *rawData = (unsigned char *) malloc(width * height * sizeof(unsigned char));
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);
return rawData;
}