Visual Studio 2013 Express Visual C++( アンマネージドコード ) OpenCV 3.1 |
■265.OpenCV 距離画像 | Prev Top Next | |
|
入力画像中の非0ピクセルから,最も近い0ピクセルまでの距離を,全ての非0ピクセルに対して計算して出力します。
#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, binary, dst; src = cv::imread("D:/TEMP/MaverickProj/Image/OpenCV/27/Texture.png", cv::IMREAD_GRAYSCALE); cv::threshold(src, // 入力配列(シングルチャンネル,8ビット,あるいは32ビット浮動小数点型). binary, 200, // 閾値. 255, // 閾値処理の種類が THRESH_BINARY や THRESH_BINARY_INV の場合に利用される,最大値の値. cv::THRESH_BINARY // 閾値処理の種類. ); cv::distanceTransform(binary, // 8ビット,シングルチャンネル(2値)の入力画像. dst, // 求められた距離を値とする出力画像 CV_DIST_L2, // 距離の種類 5 // 距離変換マスクのサイズ.3,5 あるいは CV_DIST_MASK_PRECISE ); dst = dst / 50.0; // 元画像表示用のウィンドウ作成 cv::namedWindow("Source", 1); // 元画像をウィンドウに表示 cv::imshow("Source", src); cv::namedWindow("2値化", 1); cv::imshow("2値化", binary); cv::namedWindow("距離画像", 1); cv::imshow("距離画像", dst); cv::waitKey(0); hr = 0; } catch (cv::Exception ex) { std::cout << ex.err << std::endl; } // ウィンドウの破棄 cv::destroyAllWindows(); return hr; }元画像
2値化画像
distanceType = CV_DIST_L2
distanceType = CV_DIST_L1
distanceType = CV_DIST_C