Visual Studio 2013 Express Visual C++( アンマネージドコード ) OpenCV 3.1 |
■263.OpenCV 2値化 | Prev Top Next | |
|
今回は2値化です。閾値によって細かい調整ができます。
#include <iostream> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc.hpp> #pragma comment( lib, "opencv_world300d.lib" ) int main(int argc, const char* argv[]) { int hr = -1; try { cv::Mat src, dst; src = cv::imread("D:/TEMP/MaverickProj/Image/OpenCV/26/Texture.png", cv::IMREAD_GRAYSCALE); cv::threshold(src, // 入力配列(シングルチャンネル,8ビット,あるいは32ビット浮動小数点型). dst, 200, // 閾値. 255, // 閾値処理の種類が THRESH_BINARY や THRESH_BINARY_INV の場合に利用される,最大値の値. cv::THRESH_BINARY // 閾値処理の種類._INVを指定すると階調反転 ); // 元画像表示用のウィンドウ作成 cv::namedWindow("Source", 1); // 元画像をウィンドウに表示 cv::imshow("Source", src); cv::namedWindow("2値化", 1); cv::imshow("2値化", dst); cv::waitKey(0); hr = 0; } catch (cv::Exception ex) { std::cout << ex.err << std::endl; } // ウィンドウの破棄 cv::destroyAllWindows(); return hr; }元画像
thresh = 200 thresholdType = THRESH_BINARY
thresh = 150 thresholdType = THRESH_BINARY
thresh = 200 thresholdType = THRESH_TRUNC
thresh = 150 thresholdType = THRESH_TRUNC
thresh = 200 thresholdType = THRESH_TOZERO
thresh = 150 thresholdType = THRESH_TOZERO