#include<opencv2\opencv.hpp>
#include<highgui.h>
#include<math.h>
#include<iostream>
using namespace std;
using namespace cv;
Mat src, dst,gray_src;
int threshold_value = 127;
int threshold_max = 255;
int type_value = 2;
int Type_max = 4;
const char* output_title = "binary image";
void Threshold_Demo(int, void*);
int main(int argc, char** argv)
{
// read image
src = imread("C:/Users/qxq/Pictures/image/fox.jpg");
if (src.empty())
{
printf("Could not load image...");
return -1;
}
char INPUT_WIN[] = "input image";
namedWindow(INPUT_WIN, CV_WINDOW_AUTOSIZE);
namedWindow(output_title, CV_WINDOW_AUTOSIZE);
imshow(INPUT_WIN, src);
//創建一個滾動條
createTrackbar("T_val", output_title, &threshold_value, threshold_max, Threshold_Demo);
createTrackbar("Type val", output_title, &threshold_value, Type_max, Threshold_Demo);
Threshold_Demo(0,0);
waitKey(0);
return 0;
}
void Threshold_Demo(int, void*)
{
cvtColor(src, gray_src, CV_BGR2GRAY);//只能用8位灰度圖像
//二值化
//threshold(gray_src, dst, threshold_value, threshold_max, THRESH_BINARY);
//用滾動條調整,THRESH_BINARY對應的值爲0,一共有5種(0~4).
//threshold(gray_src, dst, threshold_value, threshold_max, type_value);
//使用OSTU自動尋找合適的閾值,有2種方法(THRESH_OTSU和THRESH_TRIANGLE)
threshold(gray_src, dst, 0, 255, THRESH_OTSU | type_value);
imshow(output_title, dst);
}