1.请教这个方法调用的图像参数如何得到:const unsigned char *img。
其实就是如下所说的:@param [in] img 输入的图像数据。 这个如何得到? 已知opencv中是 调用 .data函数)
/*
brief 创建一个图像句柄。
@param [in] img 输入的图像数据。其格式如下:
-- img为一维数组,每k个连续元素(字节)表示一个像素点的颜色取值。k的值由pixelByte参数描述。数组中的值按照“行优先”顺序进行排列,即[row(1)col(1).1,row(1)col(1).2,...row(1)col(1).k,row(1)col(2).1,row(1)col(2).2,...row(1)col(2).k,...row(1)col(n).1,row(1)col(n).2,...row(1)col(n).k,row(2)col(1).1,row(2)col(1).2,...row(2)col(1).k,row(2)col(2).1,row(2)col(2).2,...row(2)col(2).k,...,row(2)col(n).1,row(2)col(n).2,...row(2)col(n).k,...,row(m)col(n).1,row(m)col(n).2,...row(m)col(n).k]表示一个m行n列的图像。该数组的长度必须等于图像高度、宽度和k的乘积,即widthheightk。
-- img为一维数组,数组中包含的是各种图片文件格式的原始二进制数据。比如,可以将SDK所支持的JPEG或者PNG文件,按照二进制格式读入内存,并直接传入本函数的img参数,而剩下的几个参数可为随意值。此时,还需要调用{@link DecodeImage}函数进行图像解码。
@param [in] width img图像的宽度,与图像的cols对应。
@param [in] height img图片的高度,与图像的rows对应。
@param [in] pixelByte img图片每像素对应的字节数,参见{@link PIXEL_BYTE}枚举。
*/
要调用的方法: int CreateImageHandle(const unsigned char *img, int width, int height, PIXEL_BYTE pixelByte);
/// 每个像素对应的字节数
enum PIXEL_BYTE
{
PixelByteNone = -1, ///< 未设定
One = 1, ///< 每个像素1个字节
Two = 2, ///< 每个像素2个字节
Three = 3, ///< 每个像素3个字节
Four = 4, ///< 每个像素4个字节
};
以上这是要传要参数。const unsigned char *img
2.已有的代码:用opencv写的正确代码:
cv::Mat viewGray;
cvtColor(view, viewGray, cv::COLOR_BGR2GRAY); //原图转换为灰度图viewGray.
unsigned char *img = viewGray.data; //直接调用.data就得到了图像数据。
int width = viewGray.cols;
int height = viewGray.rows;
3.我想用原生objective-c写一下,就卡在得到图像数据这一步了。
-(void)captureOutput:(AVCaptureOutput )captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection )connection {
在这个方法中,我是这样写的:
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0); uint8_t *lumaBuffer = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0); //byt * height size_t bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer,0); size_t width = CVPixelBufferGetWidth(pixelBuffer); size_t height = CVPixelBufferGetHeight(pixelBuffer); CGColorSpaceRef grayColorSpace = CGColorSpaceCreateDeviceGray(); CGContextRef context=CGBitmapContextCreate(lumaBuffer, width, height, 8, bytesPerRow, grayColorSpace,0); CGImageRef cgImage = CGBitmapContextCreateImage(context); CVPixelBufferUnlockBaseAddress(pixelBuffer, 0); UIImage *img = [UIImage imageWithCGImage:cgImage]; // UIImage *image = [img rotate:UIImageOrientationRight]; 暂时不用这个旋转 // CGImageRelease(cgImage); CGContextRelease(context); CGColorSpaceRelease(grayColorSpace); // return img;得到img. unsigned char *imageBytes = [self pixelBRGABytesFromImage:img]; // 得到的这个,并不对。求教正解方法 // NSLog(@"image bytes :%@", imageBytes); int hImg = [FacethinkAPIOC CreateImageHandle:imageBytes width:width height:height pixelByte:1]; //调用的这个方法,参数不对。 } - (unsigned char *)pixelBRGABytesFromImage:(UIImage *)image { return [self pixelBRGABytesFromImageRef:image.CGImage]; } - (unsigned char *)pixelBRGABytesFromImageRef:(CGImageRef)imageRef { NSUInteger iWidth = CGImageGetWidth(imageRef); NSUInteger iHeight = CGImageGetHeight(imageRef); NSUInteger iBytesPerPixel = 1;//4 -1 就是灰度图了 NSUInteger iBytesPerRow = iBytesPerPixel * iWidth; NSUInteger iBitsPerComponent = 8; unsigned char *imageBytes = (unsigned char*) malloc(iWidth * iHeight * iBytesPerPixel); CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();//4 -1 就是灰度图了 CGContextRef context = CGBitmapContextCreate(imageBytes, iWidth, iHeight, iBitsPerComponent, iBytesPerRow, colorspace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); CGRect rect = CGRectMake(0 , 0 , iWidth , iHeight); CGContextDrawImage(context , rect ,imageRef); CGColorSpaceRelease(colorspace); CGContextRelease(context); CGImageRelease(imageRef); return imageBytes; }