赞
踩
#include <opencv2\opencv.hpp> #include <iostream> using namespace std; //定义灰度图像变量 IplImage *g_GrayImage = NULL; //定义二值化图片变量 IplImage *g_BinaryImage = NULL; //定义二值化窗口标题 const char *WindowBinaryTitle = "二值化图片"; //定义滑块响应函数 void on_trackbar(int n1){ //根据传递进来的参数,进行图像二值化操作,参数1-输入图像(必须为单通道灰度图),参数2-输出边缘图像,单通道黑白图,参数3-阀值,参数4-最大值,参数5-运算方法 /* 参数5 Threshold types enum { CV_THRESH_BINARY = 0, // value = value > threshold ? max_value : 0 CV_THRESH_BINARY_INV = 1, // value = value > threshold ? 0 : max_value CV_THRESH_TRUNC = 2, // value = value > threshold ? threshold : value CV_THRESH_TOZERO = 3, // value = value > threshold ? value : 0 CV_THRESH_TOZERO_INV = 4, // value = value > threshold ? 0 : value CV_THRESH_MASK = 7, CV_THRESH_OTSU = 8 // use Otsu algorithm to choose the optimal threshold value; combine the flag with one of the above CV_THRESH_* values }; */ cvThreshold(g_GrayImage, g_BinaryImage, n1, 255, CV_THRESH_BINARY); //显示二值化后的图片 cvShowImage(WindowBinaryTitle, g_BinaryImage); } int main(){ //创建源图像窗口标题变量 const char *WindowSrcTitle = "灰度图像"; //创建滑块标题变量 const char *TheSliderTitle = "二值化阀值"; //原图位置 const char *SrcPath = "C:\\Users\\Administrator\\Documents\\visual studio 2013\\Projects\\cv\\Debug\\a.jpg"; //载入原图 IplImage *SrcImage = cvLoadImage(SrcPath,CV_LOAD_IMAGE_UNCHANGED); //单通道灰度化处理 g_GrayImage = cvCreateImage(cvSize(SrcImage->width,SrcImage->height), IPL_DEPTH_8U, 1); cvCvtColor(SrcImage, g_GrayImage, CV_BGR2GRAY); //创建二值图 g_BinaryImage = cvCreateImage(cvGetSize(g_GrayImage), IPL_DEPTH_8U, 1); //创建原图窗口 cvNamedWindow(WindowSrcTitle, CV_WINDOW_AUTOSIZE); //显示原图到原图窗口 cvShowImage(WindowSrcTitle, SrcImage); //创建二值窗口 cvNamedWindow(WindowBinaryTitle, CV_WINDOW_AUTOSIZE); //创建滑块 int n = 0; cvCreateTrackbar(TheSliderTitle, WindowBinaryTitle, &n, 254, on_trackbar); //先执行一次 on_trackbar(1); cvWaitKey(0); //销毁窗口,释放图片 cvDestroyWindow(WindowBinaryTitle); cvDestroyWindow(WindowSrcTitle); cvReleaseImage(&g_BinaryImage); cvReleaseImage(&g_GrayImage); cvReleaseImage(&SrcImage); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。