/grabcutios

Image segmentation using GrabCut algorithm for iOS

Primary LanguageC++Apache License 2.0Apache-2.0

GrabCutIOS

Image segmentation using GrabCut algorithm in IOS

Overview

GrabCut is an image segmentation method based on graph cuts. The algorithm was designed by Carsten Rother, Vladimir Kolmogorov & Andrew Blake from Microsoft Research Cambridge, UK. in their paper, "GrabCut": interactive foreground extraction using iterated graph cuts . An algorithm was needed for foreground extraction with minimal user interaction, and the result was GrabCut.

Screenshot

screenshot.png

Requirement

  1. OpenCV Framework

Usage

  1. Import GrabCutManager
#import "GrabCutManager.h"
GrabCutManager* grabcut = [[GrabCutManager alloc] init];
  1. Set foreground boundary with a rect.
-(UIImage*) doGrabCut:(UIImage*)sourceImage foregroundBound:(CGRect) rect iterationCount:(int)iterCount;
-(void) doGrabcut{
    __weak typeof(self)weakSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
                                             (unsigned long)NULL), ^(void) {
        UIImage* resultImage= [weakSelf.grabcut doGrabCut:weakSelf.resizedImage foregroundBound:weakSelf.grabRect iterationCount:5];
        resultImage = [weakSelf masking:weakSelf.originalImage mask:[weakSelf resizeImage:resultImage size:weakSelf.originalImage.size]];        
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            [weakSelf.resultImageView setImage:resultImage];
        });
    });
}
  1. Make masking image with the adding or removing parts from result.
-(UIImage*) doGrabCutWithMask:(UIImage*)sourceImage maskImage:(UIImage*)maskImage iterationCount:(int) iterCount;
-(void) doGrabcutWithMaskImage:(UIImage*)image{
    __weak typeof(self)weakSelf = self;    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
                                             (unsigned long)NULL), ^(void) {
        UIImage* resultImage= [weakSelf.grabcut doGrabCutWithMask:weakSelf.resizedImage maskImage:[weakSelf resizeImage:image size:weakSelf.resizedImage.size] iterationCount:5];
        resultImage = [weakSelf masking:weakSelf.originalImage mask:[weakSelf resizeImage:resultImage size:weakSelf.originalImage.size]];
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            [weakSelf.resultImageView setImage:resultImage];
        });
    });
}

Limitation

This program use OpenCV library. It is not use GPU in IOS. it is obviously more slower than library that it use GPU. So I want to improve this code to use GPU like GPUImage.

This algorithm is based on color value distribution. There is a limitation when foreground and background color are similar.

References

License

GrabCutIOS is licensed under the Apache License, Version 2.0. See LICENSE for full license text.

    Copyright (c) 2015 Naver Corp.
    @Author Eunchul Jeon

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.