Sunday, March 6, 2011

opcv contour





#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <math.h>
using namespace cv;

IplImage* g_image = NULL;
IplImage* g_gray = NULL;
int g_thresh = 100;
CvMemStorage* g_storage = NULL;
void on_trackbar(int) {
if( g_storage==NULL ) {
g_gray = cvCreateImage( cvGetSize(g_image), 8, 1 );
g_storage = cvCreateMemStorage(0);
} else {
cvClearMemStorage( g_storage );
}
CvSeq* contours = 0;
cvCvtColor( g_image, g_gray, CV_BGR2GRAY );
cvThreshold( g_gray, g_gray, g_thresh, 255, CV_THRESH_BINARY );
cvFindContours( g_gray, g_storage, &contours );
cvZero( g_gray );
if( contours )
cvDrawContours(
g_gray,
contours,
cvScalarAll(255),
cvScalarAll(255),
100
);
cvShowImage( "Contours", g_gray );
}
int main( int argc, char** argv )
{
 g_image = cvLoadImage("C:\\Users\\abc\\Pictures\\voc_space_sun_1_graph_solar_system.jpg");
cvNamedWindow( "Contours", 1 );
cvCreateTrackbar(
"Threshold",
"Contours",
&g_thresh,
255,
on_trackbar
);
on_trackbar(0);
cvWaitKey();
int p[3]; p[0] = CV_IMWRITE_JPEG_QUALITY; p[1] = 100; p[2] = 0; cvSaveImage("out2.jpg", g_gray, p);
return 0;
}
--------------------------------------------------------------------------------------------

No comments:

Post a Comment