以下是将UIImage
转换为cv::Mat
+ (cv::Mat)cvMatFromUIImage:(UIImage *)image {
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
CGFloat cols, rows;
if (image.imageOrientation == UIImageOrientationLeft || image.imageOrientation == UIImageOrientationRight) {
cols = image.size.height;
rows = image.size.width;
} else {
cols = image.size.width;
rows = image.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);
CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
CGContextRelease(contextRef);
cv::Mat cvMatTest;
cv::transpose(cvMat, cvMatTest);
if (image.imageOrientation == UIImageOrientationLeft || image.imageOrientation == UIImageOrientationRight) {
} else {
return cvMat;
}
cvMat.release();
cv::flip(cvMatTest, cvMatTest, 1);
return cvMatTest;
}
此代码适用于cv::Mat
至UIImage
+ (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat {
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);
CGImageRef imageRef = CGImageCreate(cvMat.cols, // Width
cvMat.rows, // Height
8, // Bits per component
8 * cvMat.elemSize(), // Bits per pixel
cvMat.step[0], // Bytes per row
colorSpace, // Colorspace
kCGImageAlphaNone | kCGBitmapByteOrderDefault, // Bitmap info flags
provider, // CGDataProviderRef
NULL, // Decode
false, // Should interpolate
kCGRenderingIntentDefault); // Intent
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
return image;
}
我将1080 * 1920(1.5 mb)的图像转换为cv::Mat
,经过一些预处理后我将其转换为UIImage
,这样我的图像大小为2517 * 1527(6 mb)
即使在图像处理之后我也不想增加图像尺寸。请指导我在哪里做错了
//裁剪行动
cv::Mat undistorted = cv::Mat( cvSize(maxWidth,maxHeight), CV_8UC4);
cv::Mat original = [MMOpenCVHelper cvMatFromUIImage:_adjustedImage];
cv::warpPerspective(original, undistorted, cv::getPerspectiveTransform(src, dst), cvSize(maxWidth, maxHeight));
答案 0 :(得分:1)
您的代码未调整图像大小。它必须是预处理的意外副作用,您不在此详述。
UIImage* image = self.testImage;
NSLog(@"original UIImage: %.0f %.0f", image.size.width,image.size.height);
cv::Mat matImage = [self cvMatFromUIImage:image];
NSLog(@"cv matImage: %d %d", matImage.cols,matImage.rows);
UIImage* newImage = [self UIImageFromCVMat:matImage];
NSLog(@"new UIImage: %.0f %.0f", newImage.size.width,newImage.size.height);
original UIImage: 720 960 cv matImage: 720 960 new UIImage: 720 960
修改
根据您的扩展问题,看起来好像warpPerspective
的输出大小为cvSize(maxWidth,maxHeight)
- 这就是您将获得的大小。如果您希望以输入大小结束,可以在转换回resize()
之前UIImage
。或者只是将输出mat
设置为与输入相同的大小。