2016-01-27 1 views
1

J'essaie d'utiliser AdobeLabsUXMagicSelectionView et je suis confronté à 2 problèmes. Je veux "couper" la zone sélectionnée (premier plan) en utilisant ces deux méthodes:Mise en avant de AdobeLabsUXMagicSelectionView (Adobe Creative SDK)

Méthode 1) getForeground:andMatte: Il ne me donne pas le premier plan correct. Lorsque je sélectionne une zone et appelle getForeground:andMatte je me donne au premier plan et à l'arrière-plan (mixte).

Sélection visage de chien enter image description here

coupe visage de chien enter image description here

Documentation dit:

Sinon, si vous n'avez pas besoin de traiter le bitmap sous-jacent directement, et l'intention de utiliser les résultats comme entrées pour CoreGraphics ou CoreImage, vous pouvez appeler:

Méthode 2) Après cela, je suis en train de "couper" que la documentation ne

extension AdobeLabsUXMagicSelectionView { 

    func foregroundCGImage() -> CGImage { 
     let w = size_t(self.image.size.width) 
     let h = size_t(self.image.size.height) 
     let data = UnsafeMutablePointer<UInt8>(malloc(4 * w * h * sizeof(UInt8))) 
     self.readForegroundAndMatteIntoBuffer(data) 

     for var i = 0; i < 4 * w * h; i += 4 { 
      let alpha: UInt8 = UInt8(data[i + 3])/255 
      data[i] *= alpha 
      data[i + 1] *= alpha 
      data[i + 2] *= alpha 
     } 

     let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipLast.rawValue) 
     let ctx = CGBitmapContextCreate(data, w, h, 8, 4 * w, CGColorSpaceCreateDeviceRGB(), bitmapInfo.rawValue) 
     let imageRef = CGBitmapContextCreateImage(ctx)! 

     return imageRef 
    } 

} 

mais il ne les peintures (noir) la partie non sélectionnée (arrière-plan) de l'image.

Quelqu'un peut-il aider? Ce que je veux, c'est obtenir une image finale de la zone sélectionnée.

MISE À JOUR:

Comme @DonWoodward dit que j'ai créé cette Cattegory:

@implementation AdobeLabsUXMagicSelectionView (Foreground) 

- (UIImage *)getForeground { 
    // show the results 
    // first create a UIImage of just the foreground bits per the documentation in AdobeLabsUXMagicSelectionView.h 
    size_t w = self.image.size.width; 
    size_t h = self.image.size.height; 

    uint8_t *data = (uint8_t *)malloc(4*w*h*sizeof(uint8_t)); 
    [self readForegroundAndMatteIntoBuffer:data]; 

    // Paint the non-selected portion of the image black 
    for (int i = 0; i < 4*w*h; i += 4) { 
     float alpha = (float)data[i + 3]/255; 
     data[i ] *= alpha; 
     data[i + 1] *= alpha; 
     data[i + 2] *= alpha; 
    } 
    CGContextRef ctx = CGBitmapContextCreate(data, w, h, 8, 4*w, CGColorSpaceCreateDeviceRGB(), (CGBitmapInfo)kCGImageAlphaNoneSkipLast); 
    CGImageRef imageRef = CGBitmapContextCreateImage(ctx); 
    UIImage * foregroundBits = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 

    return foregroundBits; 
} 

@end 

Mais le résultat a beaucoup de pixel noir autour du "premier plan".

enter image description here enter image description here

Ce que je besoin? Obtenir un avant-plan "propre" (zone sélectionnée sans pixels noirs) pour mettre un UIImageView

Répondre

1

Dans ce cas, vous prémultiplication les canaux RVB par votre canal alpha, ce qui signifie que vous souhaitez utiliser kCGImageAlphaPremultipliedLast lors de la création de votre contexte , comme dans:

// Paint the non-selected portion of the image black 
for (int i = 0; i < 4*w*h; i += 4) 
{ 
    float alpha = (float)data[i + 3]/255; 
    data[i ] *= alpha; 
    data[i + 1] *= alpha; 
    data[i + 2] *= alpha; 
} 
CGContextRef ctx = CGBitmapContextCreate(data, w, h, 8, 4*w, CGColorSpaceCreateDeviceRGB(), (CGBitmapInfo)kCGImageAlphaPremultipliedLast); 
CGImageRef imageRef = CGBitmapContextCreateImage(ctx); 
UIImage * foregroundBits = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef); 

Plus d'informations peuvent être trouvées dans le CGImage docs

, vous pouvez également essayer également d'utiliser CGImageCreateWithMask avec la sortie de

- (void)getForeground:(UIImage **)foregroundImage andMatte:(UIImage **)matteImage 

comme décrit dans le Quartz 2D Programming Guide

UIImage *fg; 
UIImage *matte; 

[_magicSelectionView getForeground:&fg andMatte:&matte]; 

CGImageRef mattedForeground = CGImageCreateWithMask(fg.CGImage, matte.CGImage); 
UIImage *foregroundBits = [UIImage imageWithCGImage:mattedForeground]; 

// show the results 
_resultsView = [[UIImageView alloc] initWithFrame: CGRectMake(0, VIEW_Y_OFFSET, self.view.bounds.size.width, self.view.bounds.size.height-VIEW_Y_OFFSET)]; 
_resultsView.contentMode = UIViewContentModeScaleAspectFit; 
[_resultsView setImage: foregroundBits]; 
[self.view addSubview: _resultsView]; 

Si aucune de ces travaux, il est possible qu'il pourrait y avoir quelques problèmes avec la configuration du UIImageViews utilisé pour rendre les résultats, mais sans ce code, il est difficile dire.

+0

Cela fonctionne comme un charme :) – Klevison

3

Je pense que le problème est que votre facteur d'échelle est de type uint8_t et que vous réduisez le facteur d'échelle à zéro. Faites-en un flotteur et cela devrait fonctionner.Voici le code de MagicPuppy (dans c objectif) qu'il fait:

// show the results 
    // first create a UIImage of just the foreground bits per the documentation in AdobeLabsUXMagicSelectionView.h 
    size_t w = _magicSelectionView.image.size.width; 
    size_t h = _magicSelectionView.image.size.height; 

    uint8_t *data = (uint8_t *)malloc(4*w*h*sizeof(uint8_t)); 
    [_magicSelectionView readForegroundAndMatteIntoBuffer:data]; 

    // Paint the non-selected portion of the image black 
    for (int i = 0; i < 4*w*h; i += 4) 
    { 
     float alpha = (float)data[i + 3]/255; 
     data[i ] *= alpha; 
     data[i + 1] *= alpha; 
     data[i + 2] *= alpha; 
    } 
    CGContextRef ctx = CGBitmapContextCreate(data, w, h, 8, 4*w, CGColorSpaceCreateDeviceRGB(), (CGBitmapInfo)kCGImageAlphaNoneSkipLast); 
    CGImageRef imageRef = CGBitmapContextCreateImage(ctx); 
    UIImage * foregroundBits = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 

    // show the results 
    _resultsView = [[UIImageView alloc] initWithFrame: CGRectMake(0, VIEW_Y_OFFSET, self.view.bounds.size.width, self.view.bounds.size.height-VIEW_Y_OFFSET)]; 
    _resultsView.contentMode = UIViewContentModeScaleAspectFit; 
    [_resultsView setImage: foregroundBits]; 
    [self.view addSubview: _resultsView]; 
+0

Ma réponse a été mise à jour. – Klevison