OpenCV - How to apply adaptive threshold to an image on iOS

前端 未结 2 1111
生来不讨喜
生来不讨喜 2021-02-13 17:34

I am trying to apply adaptive thresholding to an image of an A4 paper as shown below:

I use the c

相关标签:
2条回答
  • 2021-02-13 18:01

    You can use OpenCV framework and implement below code

     +(UIImage *)blackandWhite:(UIImage *)processedImage
        {
            cv::Mat original = [MMOpenCVHelper cvMatGrayFromAdjustedUIImage:processedImage];
    
            cv::Mat new_image = cv::Mat::zeros( original.size(), original.type() );
    
            original.convertTo(new_image, -1, 1.4, -50);
            original.release();
    
            UIImage *blackWhiteImage=[MMOpenCVHelper UIImageFromCVMat:new_image];
            new_image.release();
    
            return blackWhiteImage;
        }
    
    + (cv::Mat)cvMatGrayFromAdjustedUIImage:(UIImage *)image
    {
        cv::Mat cvMat = [self cvMatFromAdjustedUIImage:image];
        cv::Mat grayMat;
        if ( cvMat.channels() == 1 ) {
            grayMat = cvMat;
        }
        else {
            grayMat = cv :: Mat( cvMat.rows,cvMat.cols, CV_8UC1 );
            cv::cvtColor( cvMat, grayMat, cv::COLOR_BGR2GRAY );
        }
        return grayMat;
    }
    
    + (cv::Mat)cvMatFromAdjustedUIImage:(UIImage *)image
    {
        CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
        CGFloat cols = image.size.width;
        CGFloat 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);
        return cvMat;
    }
    
    
    + (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;
    }
    

    its working for me check the output for your document

    0 讨论(0)
  • 2021-02-13 18:08

    Try this:

    + (UIImage *)processImageWithOpenCV:(UIImage*)inputImage {
        cv::Mat cvImage = [inputImage CVMat];
        threshold(cvImage, cvImage, 128, 255, cv::THRESH_BINARY);
        return [UIImage imageWithCVMat:cvImage];
    }
    

    Result image:

    0 讨论(0)
提交回复
热议问题