More Tools‎ > ‎

Find Contours Example Code

//

//  main.cpp

//  OpenCV Facial Detection

//

//  Created by Garrett Menghini on 2/5/12.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//

#include <iostream>

#include <opencv2/opencv.hpp>


#ifdef _CH_

#pragma package <opencv>

#endif


#define CV_NO_BACKWARD_COMPATIBILITY


#ifndef _EiC

#include <math.h>

#endif


CvSeq* contours = 0;

CvSeq* canny_contours = 0;


int main( int argc, char** argv)

{

    // load and set up the image in a window

    IplImage* img = cvLoadImage("/Users/biggerthandads/Desktop/contour_test.jpg");

    IplImage* newimg = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U1);

    IplImage* cannyimg = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U1);

    IplImage* greyimg = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U1);

    IplImage* testimg = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U1);

    

    // convert the loaded image to a canny image

    cvCvtColor(img, greyimg, CV_BGR2GRAY);

    cvCanny(greyimg, cannyimg, 501503);

    

    // necessary to convert loaded image to an image with channel depth of 1

    cvConvertImage(cannyimg, newimg);

    cvConvertImage(img, testimg);

    

    // allocate necessary memory to store the contours 

    CvMemStorage* storage = cvCreateMemStorage(0);

    CvMemStorage* canny_storage = cvCreateMemStorage(0);

    

    // find the contours in both the loaded image and the canny filtered image

    cvFindContours(testimg, storage, &contourssizeof(CvContour),

                   CV_RETR_EXTERNALCV_CHAIN_CODE);

    cvFindContours(newimg, canny_storage, &canny_contourssizeof(CvContour),

                   CV_RETR_EXTERNALCV_CHAIN_CODE);

    

    // draw the contours on both the loaded image and the canny filtered image

    cvDrawContours(testimg, contourscvScalar(255,255,255), cvScalarAll(255), 100);

    //for( ; canny_contours != 0; canny_contours = canny_contours->h_next )

//{

    //    CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );

        /* replace CV_FILLED with 1 to see the outlines */

        cvDrawContours( newimg, canny_contourscvScalar(255,255,255), cvScalarAll(255),100);

    //}

    

    // create the display windows

    cvNamedWindow("Loaded Image"CV_WINDOW_AUTOSIZE);

    cvNamedWindow("Loaded Contours"CV_WINDOW_AUTOSIZE);

    cvNamedWindow("Canny Image"CV_WINDOW_AUTOSIZE);

    cvNamedWindow("Canny Contours"CV_WINDOW_AUTOSIZE);

    

    // show the images in the display windows

    cvShowImage("Loaded Image", img);

    cvShowImage("Loaded Contours", testimg);

    cvShowImage("Canny Image", cannyimg);

    cvShowImage("Canny Contours", newimg);

    

    // save the images

    cvSaveImage("/Users/biggerthandads/Desktop/black_and_white.jpg", img);

    cvSaveImage("/Users/biggerthandads/Desktop/canny_contour.jpg", newimg);

    cvSaveImage("/Users/biggerthandads/Desktop/test_contour.jpg", testimg);

    cvSaveImage("/Users/biggerthandads/Desktop/canny_image.jpg", cannyimg);

    

    // wait until a key is pressed

    cvWaitKey(0);

    

    // destroy the windwos and release the images

    cvDestroyWindow("Loaded Image");

    cvDestroyWindow("Loaded Contours");

    cvDestroyWindow("Canny Image");

    cvDestroyWindow("Canny Contours");

    

    cvReleaseMemStorage( &storage);

    cvReleaseMemStorage( &canny_storage);

    cvReleaseImage(&img);

    cvReleaseImage(&newimg);

    cvReleaseImage(&cannyimg);

    cvReleaseImage(&greyimg);

}

Comments