threshold関数でさまざまなしきい値処理
#geshi(c++){{
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
const char filename[] = "path/to/opencv/samples/data/pic6.png"; const char windowName[] = "Threshold"; const double kThreshold = 100; const double kHigh = 255;
int main(int argc, char**argv) {
using namespace cv;
Mat input = imread(filename, IMREAD_GRAYSCALE); namedWindow(windowName); imshow(windowName, input); waitKey(0);
Mat result;
// try various thresholding type threshold(input, result, kThreshold, kHigh, THRESH_BINARY); imshow(windowName,result); waitKey(0); threshold(input, result, kThreshold, kHigh, THRESH_BINARY_INV); imshow(windowName,result); waitKey(0); threshold(input, result, kThreshold, kHigh, THRESH_TRUNC); imshow(windowName,result); waitKey(0); threshold(input, result, kThreshold, kHigh, THRESH_TOZERO); imshow(windowName,result); waitKey(0); threshold(input, result, kThreshold, kHigh, THRESH_TOZERO_INV); imshow(windowName,result); waitKey(0);
double estimatedTh = 0.f; // try automatic threshold // try OTSU method estimatedTh = threshold(input, result, 0, kHigh, THRESH_BINARY+THRESH_OTSU); std::cout << "OTSU threshold :" << estimatedTh << std::endl; imshow(windowName,result); waitKey(0); // try OTSU method estimatedTh = threshold(input, result, 0, kHigh, THRESH_BINARY+THRESH_TRIANGLE); std::cout << "TRIANGLE threshold:" << estimatedTh << std::endl; imshow(windowName,result); waitKey(0);
return 0;
} }}