【问题标题】:Image becomes blur when applying CIFilter应用 CIFilter 时图像变得模糊
【发布时间】:2016-12-31 11:10:55
【问题描述】:

我正在使用 iOS 应用程序从相机中裁剪矩形图像。我正在使用 CIDetector 来获取矩形特征并使用 CIFilter 来裁剪矩形图像。但是在应用过滤器之后,结果图像质量变得很差。

下面是我的代码。

我正在从以下委托方法获取视频捕获输出

 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{


             UIImage *image = [self imageFromSampleBuffer:sampleBuffer];

        // Convert into CIIImage to find the rectfeatures.

             self.sourceImage = [[CIImage alloc] initWithCGImage:[self imageFromSampleBuffer:sampleBuffer].CGImage options:nil];

        }



    - (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
    {

        CVPixelBufferRef pb = CMSampleBufferGetImageBuffer(sampleBuffer);
        CIImage *ciimg = [CIImage imageWithCVPixelBuffer:pb];

        // show result
        CIContext *context = [CIContext contextWithOptions:nil];
        CGImageRef ref = [context createCGImage:ciimg fromRect:ciimg.extent];

        UIImage *image = [UIImage imageWithCGImage:ref scale:1.0 orientation:(UIImageOrientationUp)];

        CFRelease(ref);

        return (image);
    }

我正在后台运行一个 NSTimer,它将开始每隔 0.2 秒从捕获的源图像中检测矩形特征

- (void)performRectangleDetection:(CIImage *)image{

            if(image == nil)
                return;

            NSArray *rectFeatures = [self.rectangleDetector featuresInImage:image];


            if ([rectFeatures count] > 0 ) {

                [self capturedImage:image];

            }
        }




-(void)capturedImage:(CIImage *)image
        {
            NSArray *rectFeatures = [self.rectangleDetector featuresInImage:image];
                    CIImage *resultImage = [image copy];
                    for (CIRectangleFeature *feature in rectFeatures) {


                        resultImage = [image imageByApplyingFilter:@"CIPerspectiveCorrection"
                                               withInputParameters:@{@"inputTopLeft":[CIVector vectorWithCGPoint:feature.topLeft] ,
                                                                     @"inputTopRight": [CIVector vectorWithCGPoint:feature.topRight],
                                                                     @"inputBottomLeft": [CIVector vectorWithCGPoint:feature.bottomLeft],
                                                                     @"inputBottomRight": [CIVector vectorWithCGPoint:feature.bottomRight]}];



                    }


        UIImage *capturedImage = [[UIImage alloc] initWithCIImage: resultImage];
        UIImage *finalImage = [self imageWithImage:capturedImage scaledToSize:capturedImage.size];

        }

发送到该方法后会获取finalImage

 - (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {

            UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
            [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
            UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

            return newImage;
        }

最终的图像质量有时会变得模糊。那是因为过滤器还是因为相机输出图像?请帮我解决这个问题。

【问题讨论】:

    标签: ios avcapturesession cifilter cidetector


    【解决方案1】:

    您可能没有使用正确的比例因子重新创建最终图像。

    UIImage *finalImage = [UIImage imageWithCGImage:resultImage
                                              scale:original.scale
                                        orientation:original.imageOrientation];
    

    如果这不能解决问题,请提供更多来自相机输入的代码示例,以及您如何将最终的 CIImage 从过滤器转换为 UIImage。

    【讨论】:

    • 我正在从相机委托方法获取源 CGImage - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection 我将此 CIImage 发送到上述方法使用矩形特征获取裁剪图像。但结果 CGImage 有时会模糊。
    • 您能否编辑您的问题并提供有关如何从 captureOutput 转换以及如何查看最终 CGImage 的实际代码示例?这里的问题可能不是过滤器本身,而是您的预处理/后处理方式
    • 我已经编辑了我的问题。请查一下,让我知道问题出在哪里。谢谢
    • imageFromSampleBuffer 中,您正在使用 scale:1.0 来初始化图像。这会导致视网膜屏幕出现问题。另外,为什么你需要在你的 captureImage 上应用调整大小?
    【解决方案2】:

    使用以下方法裁剪图片

       -(UIImage*)cropImage:(UIImage*)image withRect:(CGRect)rect {
          CGImageRef cgImage = CGImageCreateWithImageInRect(image.CGImage, rect);
          UIImage *cropedImage = [UIImage imageWithCGImage:cgImage];
          return cropedImage;
       }
    

    【讨论】:

    • 我也尝试过这种方法从原始图像中裁剪矩形图像,但我从 CIDetector 获得的矩形特征没有裁剪出确切的矩形。它只裁剪图像的一部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 2018-03-22
    相关资源
    最近更新 更多