IOS中提取图像的一般类是UIImage,其中图像原始数据的存储方式为RGBARGBARGBA(A-alpha),进行一般的图像处理需要对其进行图像原始数据提取,具体代码为:
//功能:将IOS中的UIImage图像数据转换为rgbrgbrgb格式
//参数说明
//(UIImage*)image -- UIImage图像数据
//rgb_data :(unsigned char*)rgb_data -- 转换后的rgbrgb格式的图像数据
- (void)getRGBAsFromImage:(UIImage*)image rgb_data :(unsigned char*)rgb_data //height * width * 3
{
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
for (int i = 0 ; i < height * width; ++i)
{
rgb_data[i * 3 + 0] = rawData[i * 4 + 0]; //r
rgb_data[i * 3 + 1] = rawData[i * 4 + 1]; //g
rgb_data[i * 3 + 2] = rawData[i * 4 + 2]; //b
//rawData[i * 4 + 3]; alpha
}
free(rawData);
}
//实例
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *img = [UIImage imageNamed:@"1420268351640.jpg"];
UIImageView *imgV= [[UIImageView alloc]initWithImage:img];
unsigned char *rawData = (unsigned char*) calloc(imgWH * 3, sizeof(unsigned char));
[self getRGBAsFromImage:img rgb_data:rawData];
int vertex[8];
int text_input[4];
text_input[0] = 83;
text_input[1] = 557;
text_input[2] = 586;
text_input[3] = 693;
NSLog(@"imgH %f,imgW %f",imgV.frame.size.height,imgV.frame.size.width);
card_detect_IO(rawData,imgV.frame.size.height , imgV.frame.size.width, text_input, vertex);
int top_left_x, top_left_y, top_right_x, top_right_y, bottom_right_x, bottom_right_y, bottom_left_x, bottom_left_y;
top_left_x = vertex[0];
top_left_y = vertex[1];
top_right_x = vertex[2];
top_right_y = vertex[3];
bottom_right_x = vertex[4];
bottom_right_y = vertex[5];
bottom_left_x = vertex[6];
bottom_left_y = vertex[7];
for(int i = 0 ; i<8 ;i++)
{
NSLog(@"%d ",vertex [i]);
}
free(rawData);
}
//功能:将unsigned char(rgbrgb)图像原始数据转换为UIImage格式
//参数说明:
// 输入参数: rawData rgb彩色图像原始数据(24位存储一个像素,rgbrgb存储格式)
// height rgb图像高度
// width rgb图像宽度
// 返回参数: UIImage* 返回UIIImage格式
- (UIImage*)imageFromRGB:(unsigned char*)rawData width:(int)width height:(int)height
{
const size_t bufferLength = width * height * 3;
NSData *data = [NSData dataWithBytes:rawData length:bufferLength];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
// Creating CGImage from cv::Mat
CGImageRef imageRef = CGImageCreate(width, //width
height, //height
8, //bits per component
24, //bits per pixel
width * 3, //bytesPerRow
colorSpace, //colorspace
kCGBitmapByteOrderDefault ,// bitmap info
provider, //CGDataProviderRef
NULL, //decode
false, //should interpolate
kCGRenderingIntentDefault //intent
);
// Getting UIImage from CGImage
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
return finalImage;
}