我正在使用CIDetector来检测人脸,然后在每张脸的下半部分使用OpenCV来检测任何微笑的大小。我正在使用下面的代码来创建 OpenCV 可以执行检测的 cv::mat,因为您可以看到图像经历了 -> Cropped CIImage
-> NSBitmapImage
-> CGImage
-> cv::mat
CIImage
的步骤。
- (void)OpenCVdetectSmilesIn:(CIFaceFeature *)faceFeature usingImage:ciFrameImage
{
CGRect lowerFaceRectFull = faceFeature.bounds;
lowerFaceRectFull.size.height *=0.5;
CIImage *lowerFaceImageFull = [ciFrameImage imageByCroppingToRect:lowerFaceRectFull];
NSBitmapImageRep* rep = [[[NSBitmapImageRep alloc] initWithCIImage:lowerFaceImageFull] autorelease];
CGImage *lowerFaceImageFullCG = rep.CGImage;
//TODO: find alternative method of creating cv::mat from ciFrameImage.
std::vector<cv::Rect> smileObjects;
cv::Mat frame_gray;
CGColorSpaceRef colorSpace = CGImageGetColorSpace(lowerFaceImageFullCG);
CGFloat cols = lowerFaceRectFull.size.width;
CGFloat rows = lowerFaceRectFull.size.height;
cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels
CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to backing data
cols, // Width of bitmap
rows, // Height of bitmap
8, // Bits per component
cvMat.step[0], // Bytes per row
colorSpace, // Colorspace
kCGImageAlphaNoneSkipLast |
kCGBitmapByteOrderDefault); // Bitmap info flags
CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), lowerFaceImageFullCG);
CGContextRelease(contextRef);
cvtColor( cvMat, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
smileCascade.detectMultiScale( frame_gray, smileObjects, 1.1, 0, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(30, 30) );
它正在工作,但我时不时地在NSBitmapImageRep
行中崩溃,但我找不到重现崩溃的模式。我不确定这一点,但某种"本能"告诉我,我从 CIImage 创建 cv::mat 映像的方法既混乱又低效 - 更不用说我怀疑 CPU 不必要地用于 NSBitmapImageRep 步骤,而所有其他步骤都保留在 GPU 中......我说的对吗?
有谁知道从裁剪框架创建 cv::mat 的更好方法?
请注意,ciFrameImage 来自示例缓冲区委托方法:
CIImage *ciFrameImage = [CIImage imageWithCVImageBuffer:cvFrameBuffer options:settings];
找到了摆脱崩溃的解决方案:使用 createCGImage:fromRect
跳过NSBitmapImageRef
步骤:
- (void)OpenCVdetectSmilesIn:(CIFaceFeature *)faceFeature usingImage:ciFrameImage
{
CGRect lowerFaceRectFull = faceFeature.bounds;
lowerFaceRectFull.size.height *=0.5;
CIImage *lowerFaceImageFull = [ciFrameImage imageByCroppingToRect:lowerFaceRectFull];
// Create the context and instruct CoreImage to draw the output image recipe into a CGImage
if( self.context == nil ) {
self.context = [CIContext contextWithCGContext:nil options: nil];
}
CGImageRef lowerFaceImageFullCG = [_context createCGImage:lowerFaceImageFull fromRect:lowerFaceRectFull];
确保
CGImageRelease(lowerFaceImageFullCG);
完成后。