从 CIImage 创建垫子的最佳方法?
best way to create a mat from a CIImage?
我正在使用 CIDetector 来检测人脸,然后在每张脸的下半部分使用 OpenCV 来检测任何笑容的大小。我正在使用下面的代码创建 OpenCV 可以对其执行检测的 cv::mat,正如您所看到的图像经历了步骤 CIImage
-> Cropped CIImage
-> NSBitmapImage
-> CGImage
-> cv::mat
.
- (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
行中遇到崩溃,但我找不到重现崩溃的模式。我对此不确定,但某种 "instinct" 告诉我,我从 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);
完成后。
我正在使用 CIDetector 来检测人脸,然后在每张脸的下半部分使用 OpenCV 来检测任何笑容的大小。我正在使用下面的代码创建 OpenCV 可以对其执行检测的 cv::mat,正如您所看到的图像经历了步骤 CIImage
-> Cropped CIImage
-> NSBitmapImage
-> CGImage
-> cv::mat
.
- (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
行中遇到崩溃,但我找不到重现崩溃的模式。我对此不确定,但某种 "instinct" 告诉我,我从 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);
完成后。