应用CIFilter时图像变得模糊

时间:2016-08-24 10:48:31

标签: ios avcapturesession cifilter cidetector

我正在iOS应用中从相机裁剪矩形图像。我正在使用CIDetector获取rect功能并使用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;
        }

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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

使用以下方法裁剪图像

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