2016-10-14 1 views
0

L'histoire est que l'utilisateur sélectionne une partie d'une image en dessinant des lignes ou des courbes. Basé sur la sélection, un masque sera créé en utilisant un détecteur de bord.Création d'un masque avec détection de bord en sélectionnant une partie d'une image

Comment puis-je y parvenir?
Est CoreImage et GPUImage pourraient aider à rendre cela possible?
La détection de contours est-elle suffisante pour créer un masque?

Je n'ai aucune connaissance avancée sur le traitement et la manipulation d'image. Je suis juste une starter essayant de comprendre et d'apprendre. En passant, cela devrait être fait dans iOS.

Tout comme this un (la partie de masquage).

Répondre

0

J'ai écrit un échantillon pour vous montrer comment utiliser un masque pour sélectionner une partie de l'image, si vous voulez implémenter un effet plus complexe, vous avez juste besoin de changer le chemin, vous pouvez obtenir n'importe quelle forme.

@interface MainViewController() 

@property UIImageView* imageV; 
@property UIView* selectView; 
@property UIView *blockView; 
@property CGPoint startPoint; 
@property CGPoint endPoint; 

@end 

@implementation MainViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    UIBarButtonItem *recoverItem = [[UIBarButtonItem alloc] initWithTitle:@"Recover" style:UIBarButtonItemStyleDone target:self action:@selector(recover)]; 
    [self.navigationItem setLeftBarButtonItem:recoverItem]; 

    _imageV = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    _imageV.image = [UIImage imageNamed:@"test"]; 
    [self.view addSubview:_imageV]; 

    _blockView = [[UIView alloc] init]; 
    _blockView.frame = _imageV.bounds; 
    _blockView.backgroundColor = [UIColor whiteColor]; 
    [_imageV addSubview:_blockView]; 
    _blockView.hidden = true; 

    _selectView = [[UIView alloc] init]; 
    _selectView.layer.borderColor = [UIColor greenColor].CGColor; 
    _selectView.layer.borderWidth = 2; 
    [_imageV addSubview:_selectView]; 
    _selectView.hidden = true; 

    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; 
    [self.view addGestureRecognizer:panGR]; 
} 

- (void) handlePan: (UIPanGestureRecognizer *)pan{ 
    if (UIGestureRecognizerStateBegan == pan.state) { 
     _startPoint = [pan locationInView:_imageV]; 
    } 
    else if (UIGestureRecognizerStateChanged == pan.state) { 
     _selectView.hidden = false; 
     CGPoint currentP = [pan locationInView:_imageV]; 
     float rectWidth = currentP.x - _startPoint.x; 
     float rectHeight = currentP.y - _startPoint.y; 
     _selectView.frame = CGRectMake(_startPoint.x, _startPoint.y, rectWidth,rectHeight); 
    } 
    else if (UIGestureRecognizerStateEnded == pan.state) { 
     _endPoint = [pan locationInView:_imageV]; 
     float rectWidth = _endPoint.x - _startPoint.x; 
     float rectHeight = _endPoint.y - _startPoint.y; 
     //create path 
     UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:[UIScreen mainScreen].bounds]; 
     UIBezierPath *otherPath = [[UIBezierPath bezierPathWithRect:CGRectMake(_startPoint.x, _startPoint.y, rectWidth,rectHeight)] bezierPathByReversingPath]; 
     [maskPath appendPath:otherPath]; 
     //set mask 
     CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
     maskLayer.path = maskPath.CGPath; 
     [_blockView.layer setMask:maskLayer]; 
     _blockView.hidden = false; 

     _selectView.hidden = true; 
    } 
} 

-(void) recover{ 
    _blockView.hidden = true; 
} 

@end 

Espérons que cela puisse vous aider.

+0

Très apprécié. Cela aide à définir la région d'intérêt. Je suppose que mon problème réside derrière le retour sur investissement et la création d'un filtre personnalisé. – mownier