-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
niblack_thresholding.cpp
56 lines (48 loc) · 1.57 KB
/
niblack_thresholding.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* C++ sample to demonstrate Niblack thresholding.
*/
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/ximgproc.hpp"
using namespace std;
using namespace cv;
using namespace cv::ximgproc;
Mat_<uchar> src;
int k_ = 8;
int blockSize_ = 11;
int type_ = THRESH_BINARY;
int method_ = BINARIZATION_NIBLACK;
void on_trackbar(int, void*);
int main(int argc, char** argv)
{
// read gray-scale image
if(argc != 2)
{
cout << "Usage: ./niblack_thresholding [IMAGE]\n";
return 1;
}
const char* filename = argv[1];
src = imread(filename, IMREAD_GRAYSCALE);
imshow("Source", src);
namedWindow("Niblack", WINDOW_AUTOSIZE);
createTrackbar("k", "Niblack", &k_, 20, on_trackbar);
createTrackbar("blockSize", "Niblack", &blockSize_, 30, on_trackbar);
createTrackbar("method", "Niblack", &method_, 3, on_trackbar);
createTrackbar("threshType", "Niblack", &type_, 4, on_trackbar);
on_trackbar(0, 0);
waitKey(0);
return 0;
}
void on_trackbar(int, void*)
{
double k = static_cast<double>(k_-10)/10; // [-1.0, 1.0]
int blockSize = 2*(blockSize_ >= 1 ? blockSize_ : 1) + 1; // 3,5,7,...,61
int type = type_; // THRESH_BINARY, THRESH_BINARY_INV,
// THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV
int method = method_; //BINARIZATION_NIBLACK, BINARIZATION_SAUVOLA, BINARIZATION_WOLF, BINARIZATION_NICK
Mat dst;
niBlackThreshold(src, dst, 255, type, blockSize, k, method);
imshow("Niblack", dst);
}