除此之外,我加载透明图像(pngs).出于某种原因,当我加载图像时,opencv决定忽略一些透明区域,同时尊重他人.我可以通过将我的半透明UIImage转换为IplImage然后返回到UIImage(不对iplimage进行任何 *** 作)并将两个图像保存到本地文件来看到这一点.
这是我用于将UIImage转换为Iplimage并向后转换的代码:
- (Iplimage *)CreateIplimageFromUIImageWithTransparency:(UIImage *)image { // Getting CGImage from UIImage CGImageRef imageRef = image.CGImage; CGcolorSpaceRef colorSpace = CGcolorSpaceCreateDeviceRGB(); // Creating temporal Iplimage for drawing Iplimage *iplimage = cvCreateImage( cvSize(image.size.wIDth,image.size.height),IPL_DEPTH_8U,4 ); // Creating CGContext for temporal Iplimage CGContextRef contextRef = CGBitmapContextCreate( iplimage->imageData,iplimage->wIDth,iplimage->height,iplimage->depth,iplimage->wIDthStep,colorSpace,kCGImageAlphaPremultiplIEdLast|kCGBitmapByteOrderDefault ); // Drawing CGImage to CGContext CGContextDrawImage( contextRef,CGRectMake(0,image.size.wIDth,imageRef ); CGContextRelease(contextRef); CGcolorSpaceRelease(colorSpace); // Creating result Iplimage Iplimage *ret = cvCreateImage(cvGetSize(iplimage),4); cvCvtcolor(iplimage,ret,CV_RGBA2BGRA); cvReleaseImage(&iplimage); return ret;}- (UIImage *)UIImageFromIplimage:(Iplimage *)image { CGcolorSpaceRef colorSpace = CGcolorSpaceCreateDeviceRGB(); NSData *data = [NSData dataWithBytes:image->imageData length:image->imageSize]; CGDataProvIDerRef provIDer = CGDataProvIDerCreateWithCFData((__brIDge CFDataRef)data); CGImageRef imageRef = CGImageCreate(image->wIDth,image->height,image->depth,image->depth * image->nChannels,image->wIDthStep,kCGBitmapByteOrderDefault | kCGImageAlphaLast,provIDer,NulL,false,kCGRenderingIntentDefault); UIImage *ret = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); CGDataProvIDerRelease(provIDer); CGcolorSpaceRelease(colorSpace); return ret;}
什么可能导致这种行为?
谢谢
解决方法 这段代码适合我.我修改了 here的代码从UIImage读书
-(cv::Mat)CVMat{ CGcolorSpaceRef colorSpace = CGImageGetcolorSpace(self.CGImage); CGfloat cols = self.size.wIDth; CGfloat rows = self.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 | kCGImageAlphaPremultiplIEdLast | kCGBitmapByteOrderDefault); // Bitmap info flags CGContextDrawImage(contextRef,rows),self.CGImage); CGContextRelease(contextRef); return cvMat;}
转换回UIImage:
+ (UIImage *)imageWithCVMat:(const cv::Mat&)cvMat{ return [[UIImage alloc] initWithCVMat:cvMat];}- (ID)initWithCVMat:(const 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 kCGImageAlphaPremultiplIEdLast | kCGBitmapByteOrderDefault,// Bitmap info flags provIDer,// CGDataProvIDerRef NulL,// Decode false,// Should interpolate kCGRenderingIntentDefault); // Intent self = [self initWithCGImage:imageRef]; CGImageRelease(imageRef); CGDataProvIDerRelease(provIDer); CGcolorSpaceRelease(colorSpace); return self;}总结
以上是内存溢出为你收集整理的opencv ios透明图像加载没有透明度全部内容,希望文章能够帮你解决opencv ios透明图像加载没有透明度所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)