I'm trying to port my C++ opencv App to iOs, but I'm always getting this error :
OpenCV Error: Unsupported format or combination of formats () in cvFloodFill, file /Users/user/slave/ios_framework/src/opencv/modules/imgproc/src/floodfill.cpp, line 618
libc++abi.dylib: terminate called throwing an exception
Here's my code :
- (cv::MatND)calc_Hist
{
using namespace cv;
UIImage *temp = [UIImage imageNamed:@"file.png"];
Mat src = [self cvMatFromUIImage:temp];
Mat hsv;
Mat mask;
MatND hist;
int lo = 151; int up = 170;
cv::Point seed = cv::Point( 1029, 270 );
int newMaskVal = 255;
Scalar newVal = Scalar( 120, 120, 120 );
int connectivity = 8;
int h_bins = 30; int s_bins = 32;
int histSize[] = { h_bins, s_bins };
float h_range[] = { 0, 179 };
float s_range[] = { 0, 255 };
const float* ranges[] = { h_range, s_range };
int channels[] = { 0, 1 };
cvtColor(src, hsv, CV_BGR2HSV);
int flags = connectivity + (newMaskVal << 8 ) + cv::FLOODFILL_FIXED_RANGE + cv::FLOODFILL_MASK_ONLY;
cv::Mat mask2 = cv::Mat::zeros( src.rows + 2, src.cols + 2, CV_8UC1 );
UIImage *imgs = [self UIImageFromCVMat:hsv];
[_viewer setImage:imgs];
floodFill( src, mask2, seed, newVal, 0, cv::Scalar( lo, lo, lo ), cv::Scalar( up, up, up), flags );
mask = mask2( cv::Range( 1, mask2.rows - 1 ), cv::Range( 1, mask2.cols - 1 ) );
calcHist( &hsv, 1, channels, mask, hist, 2, histSize, ranges, true, false );
normalize( hist, hist, 0, 255, cv::NORM_MINMAX, -1, cv::Mat() );
return hist;
}
This function works fine on my Mac, so I first thought the image is not converted properly, but it shows the image if I display it in a UIImageView.
Here's the conversion method
- (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;
}
Is the iOS framework reduced in any way compared to the "normal" one? I searched with google but I didn't find anything to solve my problem.
The error occurs when a function encounters a type of matrix that the function cannot handle. Examine type of the input matrix for floodFill with src.type(). According to OpenCV manual
image – Input/output 1- or 3-channel, 8-bit or floating-point image. It is modified by the function unless the FLOODFILL_MASK_ONLY flag is set (in the second variant of the function; see below)