2012-01-06 9 views
7

He intentado el desenfoque gaussiano y he comprobado todas las preguntas en Stackoverflow, pero ninguna de ellas resolvió el problema. Por favor, ayúdenme si hay alguna otra forma de desenfocar imágenes que no sean el algoritmo de desenfoque gaussiano. El tamaño de mi imagen es de 768x1024 y los bucles se repiten durante 2 * 1024 * 768 veces y esto no es posible.Desenfocar un UIImage en el cambio del control deslizante

CGContextRef NYXImageCreateARGBBitmapContext(const size_t width, const size_t height, const size_t bytesPerRow) 
{ 
/// Use the generic RGB color space 
/// We avoid the NULL check because CGColorSpaceRelease() NULL check the value anyway, and worst case scenario = fail to create context 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

/// Create the bitmap context, we want pre-multiplied ARGB, 8-bits per component 
CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8/*Bits per component*/, bytesPerRow, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); 

CGColorSpaceRelease(colorSpace); 

return bmContext; 
} 



-(UIImage*)blurredImageUsingGaussFactor:(NSUInteger)gaussFactor andPixelRadius:(NSUInteger)pixelRadius 
{ 
CGImageRef cgImage = self.CGImage; 
const size_t originalWidth = CGImageGetWidth(cgImage); 
const size_t originalHeight = CGImageGetHeight(cgImage); 
const size_t bytesPerRow = originalWidth * 4; 
CGContextRef context = NYXImageCreateARGBBitmapContext(originalWidth, originalHeight, bytesPerRow); 
if (!context) 
    return nil; 

unsigned char *srcData, *destData, *finalData; 

size_t width = CGBitmapContextGetWidth(context); 
size_t height = CGBitmapContextGetHeight(context); 
size_t bpr = CGBitmapContextGetBytesPerRow(context); 
size_t bpp = CGBitmapContextGetBitsPerPixel(context)/8; 
CGRect rect = {{0.0f, 0.0f}, {width, height}}; 

CGContextDrawImage(context, rect, cgImage); 

// Now we can get a pointer to the image data associated with the bitmap 
// context. 
srcData = (unsigned char*)CGBitmapContextGetData(context); 
if (srcData != NULL) 
{ 
    size_t dataSize = bpr * height; 
    finalData = malloc(dataSize); 
    destData = malloc(dataSize); 
    memcpy(finalData, srcData, dataSize); 
    memcpy(destData, srcData, dataSize); 

    int sums[gaussFactor]; 
    size_t i, /*x, y,*/ k; 
    int gauss_sum = 0; 
    size_t radius = pixelRadius * 2 + 1; 
    int *gauss_fact = malloc(radius * sizeof(int)); 

    for (i = 0; i < pixelRadius; i++) 
    { 
     gauss_fact[i] = 1 + (gaussFactor * i); 
     gauss_fact[radius - (i + 1)] = 1 + (gaussFactor * i); 
     gauss_sum += (gauss_fact[i] + gauss_fact[radius - (i + 1)]); 
    } 
    gauss_fact[(radius - 1)/2] = 1 + (gaussFactor*pixelRadius); 
    gauss_sum += gauss_fact[(radius - 1)/2]; 

    unsigned char *p1, *p2, *p3; 

    for (size_t y = 0; y < height; y++) 
    { 
     for (size_t x = 0; x < width; x++) 
     { 
      p1 = srcData + bpp * (y * width + x); 
      p2 = destData + bpp * (y * width + x); 

      for (i = 0; i < gaussFactor; i++) 
       sums[i] = 0; 

      for (k = 0; k < radius ; k++) 
      { 
       if ((y - ((radius - 1) >> 1) + k) < height) 
        p1 = srcData + bpp * ((y - ((radius - 1) >> 1) + k) * width + x); 
       else 
        p1 = srcData + bpp * (y * width + x); 

       for (i = 0; i < bpp; i++) 
        sums[i] += p1[i] * gauss_fact[k]; 

      } 
      for (i = 0; i < bpp; i++) 
       p2[i] = sums[i]/gauss_sum; 
     } 
    } 
    for (size_t y = 0; y < height; y++) 
    { 
     for (size_t x = 0; x < width; x++) 
     { 
      p2 = destData + bpp * (y * width + x); 
      p3 = finalData + bpp * (y * width + x); 

      for (i = 0; i < gaussFactor; i++) 
       sums[i] = 0; 

      for(k = 0; k < radius ; k++) 
      { 
       if ((x - ((radius - 1) >> 1) + k) < width) 
        p1 = srcData + bpp * (y * width + (x - ((radius - 1) >> 1) + k)); 
       else 
        p1 = srcData + bpp * (y * width + x); 

       for (i = 0; i < bpp; i++) 
        sums[i] += p2[i] * gauss_fact[k]; 

      } 
      for (i = 0; i < bpp; i++) 
      { 
       p3[i] = sums[i]/gauss_sum; 
      } 
     } 
    } 
} 

size_t bitmapByteCount = bpr * height; 

///////Here was the problem.. you had given srcData instead of destData.. Rest all 
//were perfect... 
CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, destData, bitmapByteCount, NULL); 

CGImageRef blurredImageRef = CGImageCreate(width, height, CGBitmapContextGetBitsPerComponent(context), CGBitmapContextGetBitsPerPixel(context), CGBitmapContextGetBytesPerRow(context), CGBitmapContextGetColorSpace(context), CGBitmapContextGetBitmapInfo(context), dataProvider, NULL, true, kCGRenderingIntentDefault); 

CGDataProviderRelease(dataProvider); 
CGContextRelease(context); 
if (destData) 
    free(destData); 
if (finalData) 
    free(finalData); 

UIImage* retUIImage = [UIImage imageWithCGImage:blurredImageRef]; 

CGImageRelease(blurredImageRef); 

return retUIImage; 
} 
+0

enviar el código para su aplicación gaussiano - de esa manera puedes obtener ayuda para solucionarlo –

+0

ok, solo publico mi código. – Leena

+0

Marque esta cuestión de forma: http://stackoverflow.com/questions/1356250/iphone-blur-uiimage –

Respuesta

15

He hecho una pequeña extensión de StackBlur a UIImage. StackBlur está cerca de GaussianBlur pero mucho más rápido.

contrólela: https://github.com/tomsoft1/StackBluriOS


pequeña nota ... hay un error tipográfico en la que acaba de Léame, "normalizado" para "normalizar"

+0

gracias es realmente funcionando bien y sin problemas .... – Leena

+0

Me ayudó mucho toooo.Thanks – Meet

+1

funciona muy bien, es lo que sabes de algún implementaciones StackBlur en IOS que se encargan de la transparencia? – mmc

2

No estoy seguro acerca de cómo desenfocar una imagen. Esto puede ayudar si quiere blur an UIImageView or any view.

UIView *myView = self.theImageView; 
    CALayer *layer = [myView layer]; 
    [layer setRasterizationScale:0.25]; 
    [layer setShouldRasterize:YES]; 

Puede deshacer mediante el establecimiento de la escala de rasterización de nuevo a 1.

[layer setRasterizationScale:1.0]; 

ACTUALIZACIÓN:

El código de ejemplo a continuación de Apple incluye un borrón/efecto agudo. (usando Open GL) Vea si ayuda, http://developer.apple.com/library/ios/#samplecode/GLImageProcessing/Introduction/Intro.html

+0

quiero guardar mi imagen borrosa. – Leena

+0

k, se agregó un enlace de código de muestra. A ver si eso te ayuda. – sElanthiraiyan

+0

lo he intentado también pero no es tan bueno como el desenfoque gaussiano – Leena

1

Lo que probablemente quiera es un algoritmo de Desenfoque de caja. Es aproximadamente 10 veces más rápido que el desenfoque gaussiano y produce buenos resultados. Tengo el código ejecutándose en Android, pero aún no lo he portado a iOS. Aquí está el source.

Solo demorará unos 10 minutos en llegar al puerto de iOS. Las funciones funcionarán como están, solo necesita acceder a los bytes de la imagen (como lo hace en el código fuente anterior) y alimentarlos a las funciones.

+0

ok lo intento, gracias por la ayuda. – Leena

+0

Desenfoque de cuadro está disponible como parte de Core Image. – Abizern

+1

cuadro de desenfoque sólo está disponible para Mac comprobar este enlace: -http: //developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CoreImageFilterReference/Reference/reference.html#//apple_ref/doc/uid/ TP30000136-SW69 – Leena

Cuestiones relacionadas