opencv 프로그램 9: 윤곽 검출 균형 화

프로그램 5 의 윤곽 을 검사 합 니 다.http://blog.csdn.net/zhangjikuan/article/details/39853879균형 화 를 이루다
코드 는 다음 과 같다.
// 15ContoursEqualize.cpp :              。
//

#include "stdafx.h"  
#include <opencv2/opencv.hpp>    
using namespace std;    
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")    
IplImage *g_pGrayImage = NULL;  
IplImage *g_pGrayEqualizeImage = NULL; 
const char *pstrWindowsBinaryTitle = "   ";   
const char *pstrWindowsBinaryEqualizeTitle = "      ";
const char *pstrWindowsOutLineTitle = "   "; 
const char *pstrWindowsOutLineEqualizeTitle = "      "; 
CvSeq *g_pcvSeq = NULL;    
    
void on_trackbar(int pos)    
{    
    //          
    IplImage *pBinaryImage = cvCreateImage(cvGetSize(g_pGrayImage), IPL_DEPTH_8U, 1);    
    cvThreshold(g_pGrayImage, pBinaryImage, pos, 255, CV_THRESH_BINARY);  
	//                 
    IplImage *pBinaryEqualizeImage = cvCreateImage(cvGetSize(g_pGrayImage), IPL_DEPTH_8U, 1);    
    cvThreshold(g_pGrayEqualizeImage, pBinaryEqualizeImage, pos, 255, CV_THRESH_BINARY);  
    //                
    cvShowImage(pstrWindowsBinaryTitle, pBinaryImage);    
    cvShowImage(pstrWindowsBinaryEqualizeTitle, pBinaryEqualizeImage);

    CvMemStorage* cvMStorage = cvCreateMemStorage();    
    //                     
    cvFindContours(pBinaryImage,cvMStorage, &g_pcvSeq);    
    
    IplImage *pOutlineImage = cvCreateImage(cvGetSize(g_pGrayImage), IPL_DEPTH_8U, 3);    
    //        
    int _levels = 5;    
    cvZero(pOutlineImage);    
    cvDrawContours(pOutlineImage, g_pcvSeq, CV_RGB(255,0,0), CV_RGB(0,255,0), _levels);    
    cvShowImage(pstrWindowsOutLineTitle, pOutlineImage);    
    
	
	CvMemStorage* cvEqualizeMStorage = cvCreateMemStorage();    
    //                     
    cvFindContours(pBinaryEqualizeImage,cvEqualizeMStorage, &g_pcvSeq);    
    
    IplImage *pOutlineEqualizeImage = cvCreateImage(cvGetSize(g_pGrayImage), IPL_DEPTH_8U, 3);    
        
    cvZero(pOutlineEqualizeImage);    
    cvDrawContours(pOutlineEqualizeImage, g_pcvSeq, CV_RGB(255,0,0), CV_RGB(0,255,0), _levels);  
    cvShowImage(pstrWindowsOutLineEqualizeTitle,  pOutlineEqualizeImage);
    
    cvReleaseMemStorage(&cvMStorage);  
	cvReleaseMemStorage(&cvEqualizeMStorage);
    cvReleaseImage(&pBinaryImage);    
    cvReleaseImage(&pOutlineImage); 
	cvReleaseImage(&pOutlineEqualizeImage);
	cvReleaseImage(&pBinaryEqualizeImage); 
	
}    
    
int main( int argc, char** argv )    
{       
    const char *pstrWindowsSrcTitle = "  ";    
    const char *pstrWindowsToolBarName = "   ";    
    
    //             
    IplImage *pSrcImage = cvLoadImage("beautiful.jpg", CV_LOAD_IMAGE_UNCHANGED);    
    //         
    cvNamedWindow(pstrWindowsSrcTitle, CV_WINDOW_AUTOSIZE);    
    cvShowImage(pstrWindowsSrcTitle, pSrcImage);    
    
    //          
    g_pGrayImage =  cvCreateImage(cvGetSize(pSrcImage), IPL_DEPTH_8U, 1);    
    cvCvtColor(pSrcImage, g_pGrayImage, CV_BGR2GRAY);    
    //        
	 g_pGrayEqualizeImage = cvCreateImage(cvGetSize(g_pGrayImage), IPL_DEPTH_8U, 1);
    cvEqualizeHist(g_pGrayImage, g_pGrayEqualizeImage);
    //          ,            
    cvNamedWindow(pstrWindowsBinaryTitle, CV_WINDOW_AUTOSIZE);    
    cvNamedWindow(pstrWindowsOutLineTitle, CV_WINDOW_AUTOSIZE); 
	cvNamedWindow(pstrWindowsBinaryEqualizeTitle, CV_WINDOW_AUTOSIZE);
    cvNamedWindow(pstrWindowsOutLineEqualizeTitle, CV_WINDOW_AUTOSIZE);
    
    //          
    int nThreshold = 0;    
    cvCreateTrackbar(pstrWindowsToolBarName, pstrWindowsBinaryTitle, &nThreshold, 254, on_trackbar);    
    
    on_trackbar(1);    
    
    cvWaitKey(0);    
    
    cvDestroyWindow(pstrWindowsSrcTitle);    
    cvDestroyWindow(pstrWindowsBinaryTitle);    
    cvDestroyWindow(pstrWindowsOutLineTitle);    
    cvReleaseImage(&pSrcImage);    
    cvReleaseImage(&g_pGrayImage);    
    return 0;    
}    

결 과 는 다음 과 같다.

좋은 웹페이지 즐겨찾기