iPhone中从OpenCV返回的UIImage太大(6 MB)
我在检查iPhone上OpenCV应用程序的分配情况时发现,摄像头拍摄的照片大约需要300kb,应用程序转换的照片需要6MB(大20倍) 这不奇怪吗?看看我用来从cvMat转换UIImage的函数:iPhone中从OpenCV返回的UIImage太大(6 MB),opencv,uiimage,allocation,mat,Opencv,Uiimage,Allocation,Mat,我在检查iPhone上OpenCV应用程序的分配情况时发现,摄像头拍摄的照片大约需要300kb,应用程序转换的照片需要6MB(大20倍) 这不奇怪吗?看看我用来从cvMat转换UIImage的函数: + (UIImage *)UIImageFromCVMat:(const cv::Mat&)cvMat withOrientation:(UIImageOrientation) orientation{ NSData *data = [NSData dataWithBytes:cvM
+ (UIImage *)UIImageFromCVMat:(const cv::Mat&)cvMat withOrientation:(UIImageOrientation) orientation{
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 - ok
8 * cvMat.elemSize(), // Bits per pixel - ok
cvMat.step[0], // Bytes per row - ok
colorSpace, // Colorspace - ok
kCGImageAlphaNone | kCGBitmapByteOrderDefault, // Bitmap info flags
provider, // CGDataProviderRef
NULL, // Decode
false, // Should interpolate
kCGRenderingIntentDefault); // Intent
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef scale:1.0 orientation:orientation];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
return image;
}
我不明白为什么生成的图像如此大=/
有什么想法吗
干杯,预计尺码会更大。磁盘上的图像可能被压缩为jpg格式,根据压缩级别的不同,其大小可能会变小(如果图像压缩过多,则质量会降低) 当图像存储在矩阵中时,它不会被压缩。下面是一个估算
图像大小的公式:
image_size_in_bytes = image_width x image_height x number_of_channels x bit_depth
假设我们有一个RGB图像,大小为1280 x 960。这在jpeg格式的磁盘上可能需要大约220 KB的存储空间(它的大小取决于用于存储图像的压缩级别)。将图像加载到UIImage时,它将使用大约17倍的内存:
1280 x 960 x 3 x 8 = 3,686,400 Bytes = 3.69 MB
感谢Alexey,这解释了为什么它占用了这么多空间,但问题是:在转换为cv::Mat并处理图像之前,我已经有了两个UIImage,每个大约300kb。但是当它们从cv::Mat转换为UIImages时,会占用太多空间。如何压缩新图像,使其不占用(约)6MB,而只占用(约)300kb?实际上,我刚刚在这里找到了一个解决方案:,让我们看看它是否有效=)是的,这取决于您在UIImage中存储数据的方式。很高兴您找到了解决方案,您还可以查看