将程序五的轮廓检测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; }