【练习5.12】阈值化cvThreshold、自适应阈值cvAdaptiveThreshold、各参数效果对比

 

提纲
题目要求
程序代码
结果图片
要言妙道

 

 

 

  

 

题目要求:

 载入一副场景丰富的图像,使用cvThreshold()函数对其进行操作,设置阈值为128,。

a、依次用表5-5中的设置类型并显示结果

b、用函数cvAdaptiveThreshold()设param1=5

c、先设param1=0,重复b,再设param1=-5,重复b 

 

程序代码:

 

  1 // OpenCVExerciseTesting.cpp : 定义控制台应用程序的入口点。
  2 //
  3 //D:\\Work\\Work_Programming\\Source\\Image\\lena.jpg
  4 
  5 
  6 #include "stdafx.h"
  7 #include <cv.h>
  8 #include <highgui.h>
  9 #include <iostream>
 10 
 11 #include <opencv2/legacy/legacy.hpp>
 12 //#pragma comment(lib, "opencv_legacy2411.lib")
 13 
 14 using namespace cv;
 15 using namespace std;
 16 
 17 //函数声明-->--->-->--->-->--->-->--->//
 18 
 19 
 20 //<--<--<--<--<--<--<--<--<--函数声明//
 21 
 22 int _tmain(int argc, _TCHAR* argv[])
 23 {
 24     const char * soutceFile = "D:\\Work\\Work_Programming\\Source\\Image\\OpenCVExerciseImage\\第5章\\山水风景.jpg";
 25     IplImage * image_Resource = cvLoadImage(soutceFile, CV_LOAD_IMAGE_UNCHANGED);
 26     assert(image_Resource);
 27       
 28     cvNamedWindow("原始图像", 0);    
 29     cvNamedWindow("题目_a_1", 0);
 30     cvNamedWindow("题目_a_2", 0);
 31     cvNamedWindow("题目_a_3", 0);
 32     cvNamedWindow("题目_a_4", 0);
 33     cvNamedWindow("题目_a_5", 0);
 34     cvNamedWindow("题目_b_1", 0);
 35     cvNamedWindow("题目_b_2", 0);
 36     cvNamedWindow("题目_c_1", 0);
 37     cvNamedWindow("题目_c_2", 0);
 38     cvNamedWindow("题目_c_3", 0);
 39     cvNamedWindow("题目_c_4", 0);
 40 
 41     IplImage * image_Gray = cvCreateImage(cvSize(image_Resource->width, image_Resource->height), IPL_DEPTH_8U, 1);
 42     
 43     //使用cvCvtColor和cvCopy这些函数前,都应该对参数进行验证再使用
 44     if (image_Resource->nChannels != 3)
 45     {
 46         cout << "加载的图像必须为彩色图片" << endl;
 47         return 0;
 48     }
 49     
 50     cvCvtColor(image_Resource, image_Gray, CV_RGB2GRAY);
 51 
 52     cvShowImage("原始图像", image_Gray);
 53      
 54     //---------------------------a:开始--------------------------------//
 55 
 56     double max_value = 255;
 57     double threshold = 128;
 58 
 59     IplImage * image_Result_a_1 = cvCloneImage(image_Gray);
 60     cvZero(image_Result_a_1);
 61     cvThreshold(image_Gray, image_Result_a_1, threshold, max_value, CV_THRESH_BINARY);
 62     cvShowImage("题目_a_1", image_Result_a_1);
 63 
 64 
 65     IplImage * image_Result_a_2 = cvCloneImage(image_Gray);
 66     cvZero(image_Result_a_2);
 67     cvThreshold(image_Gray, image_Result_a_2, threshold, max_value, CV_THRESH_BINARY_INV);
 68     cvShowImage("题目_a_2", image_Result_a_2);
 69 
 70 
 71     IplImage * image_Result_a_3 = cvCloneImage(image_Gray);
 72     cvZero(image_Result_a_3);
 73     cvThreshold(image_Gray, image_Result_a_3, threshold, max_value, CV_THRESH_TRUNC);
 74     cvShowImage("题目_a_3", image_Result_a_3);
 75 
 76     IplImage * image_Result_a_4 = cvCloneImage(image_Gray);
 77     cvZero(image_Result_a_4);
 78     cvThreshold(image_Gray, image_Result_a_4, threshold, max_value, CV_THRESH_TOZERO_INV);
 79     cvShowImage("题目_a_4", image_Result_a_4);
 80 
 81     IplImage * image_Result_a_5 = cvCloneImage(image_Gray);
 82     cvZero(image_Result_a_5);
 83     cvThreshold(image_Gray, image_Result_a_5, threshold, max_value, CV_THRESH_TOZERO);
 84     cvShowImage("题目_a_5", image_Result_a_5);
 85     //---------------------------a:结束--------------------------------//    
 86 
 87     //---------------------------b:开始--------------------------------//
 88 
 89     IplImage * image_Result_b_1 = cvCloneImage(image_Gray);
 90     cvZero(image_Result_b_1);
 91     cvAdaptiveThreshold(image_Gray, image_Result_b_1, max_value, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 3, 5);
 92 
 93     cvShowImage("题目_b_1", image_Result_b_1);
 94 
 95 
 96     IplImage * image_Result_b_2 = cvCloneImage(image_Gray);
 97     cvZero(image_Result_b_2);
 98     cvAdaptiveThreshold(image_Gray, image_Result_b_2, max_value, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY_INV,3,5);
 99 
100     cvShowImage("题目_b_2", image_Result_b_2);
101 
102     //---------------------------b:结束--------------------------------//    
103 
104     //---------------------------c:开始--------------------------------//
105  
106     IplImage * image_Result_c_1 = cvCloneImage(image_Gray);
107     cvZero(image_Result_c_1);
108     cvAdaptiveThreshold(image_Gray, image_Result_c_1, max_value, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 3, 0);
109 
110     cvShowImage("题目_c_1", image_Result_c_1);
111 
112 
113     IplImage * image_Result_c_2 = cvCloneImage(image_Gray);
114     cvZero(image_Result_c_2);
115     cvAdaptiveThreshold(image_Gray, image_Result_c_2, max_value, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY_INV, 3, 0);
116 
117     cvShowImage("题目_c_2", image_Result_c_2);
118 
119     IplImage * image_Result_c_3 = cvCloneImage(image_Gray);
120     cvZero(image_Result_c_3);
121     cvAdaptiveThreshold(image_Gray, image_Result_c_3, max_value, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 3, -5);
122 
123     cvShowImage("题目_c_3", image_Result_c_3);
124 
125 
126     IplImage * image_Result_c_4 = cvCloneImage(image_Gray);
127     cvZero(image_Result_c_4);
128     cvAdaptiveThreshold(image_Gray, image_Result_c_4, max_value, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY_INV, 3, -5);
129 
130     cvShowImage("题目_c_4", image_Result_c_4);
131 
132     //---------------------------c:结束--------------------------------//    
133 
134     cvWaitKey(0);
135 
136     cvReleaseImage(&image_Resource);
137     cvReleaseImage(&image_Result_a_1);
138     cvReleaseImage(&image_Result_a_2);
139     cvReleaseImage(&image_Result_a_3);
140     cvReleaseImage(&image_Result_a_4);
141     cvReleaseImage(&image_Result_a_5);
142     cvReleaseImage(&image_Result_b_1);
143     cvReleaseImage(&image_Result_b_2);
144     cvReleaseImage(&image_Result_c_1);
145     cvReleaseImage(&image_Result_c_2);
146     cvReleaseImage(&image_Result_c_3);
147     cvReleaseImage(&image_Result_c_4);
148 
149     cvDestroyWindow("原始图像");     
150     cvDestroyWindow("题目_a_1");
151     cvDestroyWindow("题目_a_2");
152     cvDestroyWindow("题目_a_3");
153     cvDestroyWindow("题目_a_4");
154     cvDestroyWindow("题目_a_5");
155     cvDestroyWindow("题目_b_1");
156     cvDestroyWindow("题目_b_2");
157     cvDestroyWindow("题目_c_1");
158     cvDestroyWindow("题目_c_2");
159     cvDestroyWindow("题目_c_3");
160     cvDestroyWindow("题目_c_4");
161 
162     return 0;
163 }
164    

 

结果图片:

 

要言妙道:

 ①针对有很强照明或反射梯度的图像,需要根据梯度进行阈值化,自适应阈值技术非常有用,最有对比性的是《学习OpenCV》第161页使用cvThreshold和cvAdaptiveThreshold对图片处理的例子,当棋盘标定板有很强的照明梯度的时候,使用cvThreshold得不到完整的棋盘,但cvAdaptiveThreshold可以。

②cvAdaptiveThreshold的参数thresholdType ,官方文档的说明是 Thresholding type that must be either THRESH_BINARY or THRESH_BINARY_INV .,并不是cvThreshold的5种类型都能用

③CV_THRESH_TRUNC,对应的操作时dsti=(srci>T)?T:srci,其中T代表阈值,这点《学习OpenCV》中笔误

 

posted on 2015-04-26 12:58  毋忆典藏  阅读(966)  评论(0编辑  收藏  举报