以下是我用于使用OpenCV将CVMat
转换为UIImage
的代码。使用此代码一切正常,没有崩溃。但是颜色变了。
-(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
{
cvtColor(cvMat, cvMat, CV_BGR2RGB);
NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
CGColorSpaceRef colorSpace;
if (cvMat.elemSize() == 1) {
colorSpace = CGColorSpaceCreateDeviceGray();
} else {
colorSpace = CGColorSpaceCreateDeviceRGB();
}
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
// Creating CGImage from cv::Mat
CGImageRef imageRef = CGImageCreate(cvMat.cols, //width
cvMat.rows, //height
8, //bits per component
8 * cvMat.elemSize(), //bits per pixel
cvMat.step[0], //bytesPerRow
colorSpace, //colorspace
kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
provider, //CGDataProviderRef
NULL, //decode
false, //should interpolate
kCGRenderingIntentDefault //intent
);
// Getting UIImage from CGImage
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
return finalImage;
}
作为一种解决方法,我在代码中评论了cvtColor()
函数。它现在能够保留原始颜色,但现在问题是应用程序有时会随机崩溃。我甚至尝试使用UIImageFromCvMat()
中的opencv2/imgcodecs/ios.h
,但崩溃方式相同。
所以,如果我想防止崩溃,我必须在代码中保留cvtColor(),但它会改变实际颜色。我在这里做错了吗?
修改
我刚刚发现,当我在方法中传递特定的cvMat用于UIImage转换时会发生这种情况。我正在发布我的图像处理代码,我正在调用Mat - > UIImage
当我传递grayImage(cvMat)时它工作正常,但是当我通过croppedImage(cvMat)进行转换时它会崩溃。
-(UIImage*)processImage:(UIImage*)sourceImage{
cv::Mat processMat = [self cvMatFromUIImage:sourceImage];
cv::Mat grayImage;
cvtColor(processMat, grayImage, CV_BGR2GRAY);
/// Generate grad_x and grad_y
cv::Mat grad_x, grad_y;
cv::Mat abs_grad_x, abs_grad_y;
/// Gradient X
//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
cv::Sobel( grayImage, grad_x, CV_16S, 1, 0, 3, 1 , 0);
convertScaleAbs( grad_x, abs_grad_x );
/// Gradient Y
//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel( grayImage, grad_y, CV_16S, 0, 1, 3, 1, 0);
convertScaleAbs( grad_y, abs_grad_y );
// Total Gradient (approximate)
cv::Mat grad;
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours( grad, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_TC89_KCOS);
double maxArea = 0;
size_t maxIndex=0;
for ( size_t i=0; i<contours.size(); ++i )
{
double area = cv::contourArea(contours[i]);
if(area>maxArea){
maxIndex = i;
maxArea = area;
}
}
cv::Rect brect = cv::boundingRect(contours[maxIndex]);
cv::Mat croppedImage = processMat(brect);
//It Works when I pass any other cvMat like grayImage
//UIImage *result2 = [self UIImageFromCVMat:grayImage]; // MatToUIImage(grayImage);
//This line causes Crash..
UIImage *result = [self UIImageFromCVMat:croppedImage]; // MatToUIImage(grayImage);
//Tried to Comment this part
croppedImage.release();
grad.release();
grayImage.release();
grad_x.release();
grad_y.release();
processMat.release();
return result;
}
答案 0 :(得分:1)
再次将垫子从BGR2RGB转换为RGB2BGR,它将起作用。这次事故背后的原因不知道,但尝试这种方式是有效的。
在cvtColor(cvMat, cvMat, CV_RGB2BGR)
之后添加行cvtColor(cvMat, cvMat, CV_BGR2RGB);
。