2014-06-06 16 views
6

Dzięki za pomoc z góry.OpenCV z formułą Laplacian do wykrywania obrazu jest rozmazany lub nie w iOS

Mam wiele R & D i wyszukiwania, ale nie mogę znaleźć żadnego rozwiązania do wykrywania rozmycia obrazu, czy nie.

Użyłem tego https://github.com/BloodAxe/OpenCV-Tutorial i wykorzystywane do wykrywania rozmycie laplasjan formuła, ale nie może dostać wykrywanie rozmycia w obrazie

- (void) checkForBurryImage: (UIImage *) obraz {

cv::Mat matImage = [image toMat]; 
cv::Mat matImageGrey; 
cv::cvtColor(matImage, matImageGrey, CV_BGRA2GRAY); 

cv::Mat dst2 =[image toMat]; 
cv::Mat laplacianImage; 
dst2.convertTo(laplacianImage, CV_8UC1); 
cv::Laplacian(matImageGrey, laplacianImage, CV_8U); 
cv::Mat laplacianImage8bit; 
laplacianImage.convertTo(laplacianImage8bit, CV_8UC1); 
//------------------------------------------------------------- 
//------------------------------------------------------------- 
unsigned char *pixels = laplacianImage8bit.data; 
//------------------------------------------------------------- 
//------------------------------------------------------------- 
// unsigned char *pixels = laplacianImage8bit.data; 
int maxLap = -16777216; 

for (int i = 0; i < (laplacianImage8bit.elemSize()*laplacianImage8bit.total()); i++) { 
    if (pixels[i] > maxLap) 
     maxLap = pixels[i]; 
} 

int soglia = -6118750; 

printf("\n maxLap : %i",maxLap); 


if (maxLap < soglia || maxLap == soglia) { 
    printf("\n\n***** blur image *****"); 
}else 
    printf("\nNOT a blur image"); } 

Użyłem tego samego kodu co Android i działa poprawnie, ale w iOS jego wartość zawsze jest dodatnia, więc myślę, że nie działa,

Proszę podać mi pomysł lub link lub sugestię.

+0

Dostałeś odpowiedniego rozwiązania tego prob? Mam do czynienia z tym samym problemem. – Prakash

Odpowiedz

3

Służy

-(BOOL) checkForBurryImage:(cv::Mat) matImage {// Output:(cv::Mat &) outputFrame { 

cv::Mat finalImage; 


cv::Mat matImageGrey; 
cv::cvtColor(matImage, matImageGrey, CV_BGRA2GRAY); 
matImage.release(); 

cv::Mat newEX; 
const int MEDIAN_BLUR_FILTER_SIZE = 15; // odd number 
cv::medianBlur(matImageGrey, newEX, MEDIAN_BLUR_FILTER_SIZE); 
matImageGrey.release(); 

cv::Mat laplacianImage; 
cv::Laplacian(newEX, laplacianImage, CV_8U); // CV_8U 
newEX.release(); 

cv::Mat laplacianImage8bit; 
laplacianImage.convertTo(laplacianImage8bit, CV_8UC1); 
laplacianImage.release(); 
cv::cvtColor(laplacianImage8bit,finalImage,CV_GRAY2BGRA); 
laplacianImage8bit.release(); 

int rows = finalImage.rows; 
int cols= finalImage.cols; 
char *pixels = reinterpret_cast<char *>(finalImage.data); 
int maxLap = -16777216; 
for (int i = 0; i < (rows*cols); i++) { 
    if (pixels[i] > maxLap) 
     maxLap = pixels[i]; 
} 

int soglia = -6118750;  

pixels=NULL; 
finalImage.release(); 

BOOL isBlur = (maxLap < kBlurThreshhold)? YES : NO; 
return isBlur; 

}

+0

Dlaczego mamy "int soglia = -6118750;" w metodzie? –

+0

jaka jest wartość kBlurThreshold? –

0

Poniższa metoda wykorzystuje OpenCV:

- (BOOL) isImageBlurry:(UIImage *) image { 
    // converting UIImage to OpenCV format - Mat 
    cv::Mat matImage = [self convertUIImageToCVMat:image]; 
    cv::Mat matImageGrey; 
    // converting image's color space (RGB) to grayscale 
    cv::cvtColor(matImage, matImageGrey, CV_BGR2GRAY); 

    cv::Mat dst2 = [self convertUIImageToCVMat:image]; 
    cv::Mat laplacianImage; 
    dst2.convertTo(laplacianImage, CV_8UC1); 

    // applying Laplacian operator to the image 
    cv::Laplacian(matImageGrey, laplacianImage, CV_8U); 
    cv::Mat laplacianImage8bit; 
    laplacianImage.convertTo(laplacianImage8bit, CV_8UC1); 

    unsigned char *pixels = laplacianImage8bit.data; 

    // 16777216 = 256*256*256 
    int maxLap = -16777216; 
    for (int i = 0; i < (laplacianImage8bit.elemSize()*laplacianImage8bit.total()); i++) { 
     if (pixels[i] > maxLap) { 
      maxLap = pixels[i]; 
     } 
    } 
    // one of the main parameters here: threshold sets the sensitivity for the blur check 
    // smaller number = less sensitive; default = 180 
    int threshold = 100;   

    return (maxLap <= threshold); 
} 

KonwersjaUIImagedoOpenCV::Mat

- (cv::Mat)convertUIImageToCVMat:(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 (color channels + alpha) 

    CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,     // Pointer to data 
                cols,      // Width of bitmap 
                rows,      // Height of bitmap 
                8,       // Bits per component 
                cvMat.step[0],    // Bytes per row 
                colorSpace,     // Colorspace 
                kCGImageAlphaNoneSkipLast | 
                kCGBitmapByteOrderDefault); // Bitmap info flags 

    CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage); 
    CGContextRelease(contextRef); 

    return cvMat; 
} 
0

Użyj tego:

Laplacian(gray, laplacianImage, CV_64F); 
    Scalar mean, stddev; //0:1st channel, 1:2nd channel and 2:3rd channel 
    meanStdDev(laplacianImage, mean, stddev, Mat()); 
    double variance = stddev.val[0] * stddev.val[0]; 

    double threshold = 2900; 

    if (variance <= threshold) { 
     BLURRR 
    } 
    else{ 
     NOT BLURR 
    } 
Powiązane problemy