Thursday, December 25, 2014

Canny Edge Detection

Modify the earlier ViewController.m file from this project:

//
//  ViewController.m
//  InstantOpenCv
//
//  Created by Paul Chin on 12/25/14.
//  Copyright (c) 2014 Paul Chin. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //self.imageView.image=[UIImage imageNamed:@"StBernard.jpg"];
    
    UIImage *image = [UIImage imageNamed:@"StBernard.jpg"];
    UIImageToMat(image, cvImage);
    if(!cvImage.empty()){
        using namespace cv;
        Mat gray;
        cvtColor(cvImage, gray, CV_RGB2GRAY);//convert to single channel
        GaussianBlur(gray, gray, cvSize(5, 5),1.2,1.2);//remove small details
        
        Mat edges;
        Canny(gray, edges, 0, 50);  //detect edges
        cvImage.setTo(Scalar::all(255)); //fill image to white
        cvImage.setTo(Scalar(0,128,255,255),edges);//add edges
        
        self.imageView.image=MatToUIImage(cvImage);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }


@end



Run the app on iOS simulator:


2 comments:

  1. It's an incomplete code snippet, please provide full code.

    ReplyDelete
    Replies
    1. The whole project link is there! just hover over 'project' and click for the whole project

      Modify the earlier ViewController.m file from this project:

      Delete