利用OpenCV提取目标轮廓

一个简单的提取飞机目标图像(二值图)轮廓的程序

    #include
    #include
    #include
    #include
    IplImage* g_image = NULL;
    IplImage* g_gray =NULL;
    int g_thresh = 100;
    CvMemStorage* g_storage = NULL;
    char name[1][80];
    int Presskey;
    int main( int argc, char** argv )
    {
    if( argc==2 && ( g_image = cvLoadImage( argv[1], 1 ) ) != 0 )
    {
    cvNamedWindow( "Contours", 1 );
    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),
    1);
    }
    cvShowImage( "Contours", g_gray );
    Presskey=cvWaitKey();
    printf("%c is pressed\n",Presskey);
    switch( Presskey )
    {
    case 's':
    cout<<"The image is going to be saved, please input the name:"<     cin>>name[0];
    cvSaveImage( name[0], g_gray );
    break;
    default:
    break;
    }
    cvDestroyWindow( "Contours" );
    cvReleaseImage( &g_image );
    cvReleaseImage( &g_gray );
    }
    else
    return -1;
    return 0;
    }


使用F15战机的二值图像进行了实验,下面是原图和结果图:

=====================================



你可能感兴趣的:(Opencv)