Bad argument (image must have CV_8UC3 type) in grabCut

I am using grabCut algorithm using the following code:

cv::Mat img=[self cvMatFromUIImage:image];

cv::Rect rectangle(10,10,300,150);

cv::Mat result; // segmentation (4 possible values)
cv::Mat bgModel,fgModel; // the models (internally used)
// GrabCut segmentation
cv::grabCut(img,    // input image
            result,      // segmentation result
            rectangle,   // rectangle containing foreground
            bgModel,fgModel, // models
            3,           // number of iterations
            cv::GC_INIT_WITH_RECT); // use rectangle
// Get the pixels marked as likely foreground
cv::compare(result,cv::GC_PR_FGD,result,cv::CMP_EQ);
// Generate output image
cv::Mat foreground(img.size(),CV_8UC3,
                   cv::Scalar(255,255,255));
result=result&1;
img.copyTo(foreground, result);
            result);

image=[self UIImageFromCVMat:foreground];
ImgView.image=image;

The code to convert UIImage to Mat image looks like this

- (cv::Mat)cvMatFromUIImage:(UIImage *)imge
{
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(imge.CGImage);
    CGFloat cols = imge.size.width;
    CGFloat rows = imge.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), imge.CGImage);
    CGContextRelease(contextRef);
    CGColorSpaceRelease(colorSpace);

    return cvMat;
}

But I got the error

OpenCV Error: Bad argument (image must have CV_8UC3 type) in grabCut.

If I change cv::Mat cvMat(rows, cols, CV_8UC4); line to cv::Mat cvMat(rows, cols, CV_8UC3); then I get <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 342 bytes/row..

I am confused here for what to do.

Any help please

Answers


The Problem seems to be, that the image you get has an alpha channels, while grabcut expects a rgb image without an alpha channel. So you need to get rid of the additional channel.

You can do this for example with this function:

cv::cvtColor(img , img , CV_RGBA2RGB);

Does this solve your Problem?


Need Your Help

CRM 2011 SDK undefined when call JQuery only through Outlook Client (Web client works fine)

dynamics-crm-2011 dynamics-crm crm

I have come across an issue with MS CRM 2011. Simply the scenario is as follows.

ASCII Value of blank character in file name

c# ascii indexof

I have many Windows 7 data file names that appears to contain a blank space just before the extension. Ultimately, I want to eliminate the blank spaces, but my immediate problem is finding them. I

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.