OpenCV的。图像以灰度而不是RGB的形式返回

时间:2013-08-17 14:34:30

标签: ios opencv uiimage

我正在iOS上用OpenCV做一些实验。我尝试在检测到的脸上放置一个png图像。我试图将图像放在一个被剔除的脸上:enter image description here

但我明白了。:enter image description here

我使用此函数将UIImage转换为cvMat。:

- (cv::Mat)cvMatFromUIImage:(UIImage *)image
{
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
CGFloat cols = image.size.width;
CGFloat rows = image.size.height;

cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels

CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                 // Pointer to    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), image.CGImage);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);

return cvMat;
}

我在这里添加图片。:

- (void)processImage:(cv::Mat&)image;
{
 cv::vector<cv::Rect> faces;
 cv::Mat frame_gray;

cvtColor(image, frame_gray, CV_BGRA2GRAY);
equalizeHist(frame_gray, frame_gray);

face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE,   cv::Size(100, 100));

for(unsigned int i = 0; i < faces.size(); ++i)
{
    rectangle(image, cv::Point(faces[i].x, faces[i].y),
              cv::Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height),
              cv::Scalar(0,255,255));
    CGPoint pos;
    pos.x = faces[i].x;
    pos.y = faces[i].y;

    cv::Rect roi( cv::Point( pos.x , pos.y ), cv::Size( faces[i].width , faces[i].height   ));
    cv::Mat destinationROI = image( roi );
    cv::Mat smallImage = [self faceIntoImageView:pos size:CGSizeMake(faces[i].width,   faces[i].height)];
    smallImage.copyTo(image(roi));

 }
 }

-(cv::Mat)faceIntoImageView:(CGPoint)position size:(CGSize)size
{

UIImage* face = [UIImage imageNamed:@"Face1.png"];
face = [self imageWithImage:face scaledToSize:size];
return [self cvMatFromUIImage:face];
}

1 个答案:

答案 0 :(得分:3)

您应该交换B和R频道。

    - (cv::Mat)cvMatFromUIImage:(UIImage *)image
    {
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
    CGFloat cols = image.size.width;
    CGFloat rows = image.size.height;

    cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels

    CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                 // Pointer to    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), image.CGImage);
    CGContextRelease(contextRef);
    CGColorSpaceRelease(colorSpace);

//-------swap channels
std::vector<Mat> ch;
cv::split(cvMat,ch);
std::swap(ch[0],ch[2]);
cv::merge(ch,cvMat);
//-------
    return cvMat;
    }