一直认为imshow中有namedWindow,在imshow前再imshow是种累赘,所以一直把例程中的namedWindow注释掉,今天在阈值化中发现滚动条的控件出不来,几经检查,最后发现加上namedWindow才得以正常。
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
using namespace cv;
int threshold_value = 0;
int threshold_type = 3;;
int const max_value = 255;
int const max_type = 4;
int const max_BINARY_value = 255;
char* window_name = "Threshold Demo";
char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
char* trackbar_value = "Value";
Mat src, src_gray;
void Threshold_Demo( int, void* );
int main( )
{
//char* window_name = "Sobel Demo - Simple Edge Detector";
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
src = imread( "D:\\short.jpg" ,1);
GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
/// 转换为灰度图
cvtColor( src, src_gray, CV_RGB2GRAY );
namedWindow( window_name, CV_WINDOW_AUTOSIZE );//居然这句不能删,不然控件出不来
Ptr<CLAHE> clahe = createCLAHE();//CLAHE
clahe->setClipLimit(10);
clahe->apply(src_gray,src_gray);
//imshow("lena_CLAHE",src_gray);
//waitKey();
createTrackbar( trackbar_type,
window_name, &threshold_type,
max_type, Threshold_Demo );
createTrackbar( trackbar_value,
window_name, &threshold_value,
max_value, Threshold_Demo );
/// 初始化自定义的阈值函数
Threshold_Demo( 0, 0 );
/// 等待用户按键。如果是ESC健则退出等待过程。
while(true)
{
int c;
c = waitKey( 20 );
if( (char)c == 27 )
{ break; }
}
return 0;
}
void Threshold_Demo( int, void* )
{
/* 0: 二进制阈值
1: 反二进制阈值
2: 截断阈值
3: 0阈值
4: 反0阈值
*/
threshold( src_gray, src_gray, threshold_value, max_BINARY_value,threshold_type );
imshow( window_name, src_gray );
}