close
Distance Transform [2]經常應用在二值影像,其運算結果則為一灰階影像,
其強度並非表示亮度值,而是表示物件內部每一點與物件邊緣的距離。
如果以 1 表示物件像素,0 是背景像素,則 distance transform 定義為
對於每一個物件區域的像素,計算其與最近的背景像素的距離,並以此距離值取代原像素值。
debug環境設定檔案
http://www.mediafire.com/download/l2kf42zva55u23q/VS2010OpenCV249X86Debug.props
release環境設定檔案
http://www.mediafire.com/download/3y9uer5ni6e0a0t/VS2010OpenCV249X86Release.props
參數
1. 來源影像
2. 輸出影像
3. 距離種類
以下為常見的距離量測[2]
對應上述距離種類的參數設定
/* Distance types for Distance Transform and M-estimators */
enum
{
CV_DIST_USER =-1, /* User defined distance */
CV_DIST_L1 =1, /* distance = |x1-x2| + |y1-y2| */
CV_DIST_L2 =2, /* the simple euclidean distance */
CV_DIST_C =3, /* distance = max(|x1-x2|,|y1-y2|) */
CV_DIST_L12 =4, /* L1-L2 metric: distance = 2(sqrt(1+x*x/2) - 1)) */
CV_DIST_FAIR =5, /* distance = c^2(|x|/c-log(1+|x|/c)), c = 1.3998 */
CV_DIST_WELSCH =6, /* distance = c^2/2(1-exp(-(x/c)^2)), c = 2.9846 */
CV_DIST_HUBER =7 /* distance = |x|<c ? x^2/2 : c(|x|-c/2), c=1.345 */
};
4. 遮罩大小
//! computes the distance transform map
CV_EXPORTS_W void distanceTransform( InputArray src, OutputArray dst,
int distanceType, int maskSize );
標頭檔
// DistanceTransformSample.cpp : 定義主控台應用程式的進入點。
//
#include "stdafx.h"
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
主程式如下
// Load the image
//char* file = "C:\\OpenCV\\images\\art5.png";
//char* file = "C:\\OpenCV\\images\\art6.png";
//char* file = "C:\\OpenCV\\images\\art7.png";
//char* file = "C:\\OpenCV\\images\\art5cha2.png";
//char* file = "C:\\OpenCV\\images\\art5noi1.png";
char* file = "C:\\OpenCV\\images\\phn1.png";
Mat src = imread(file);
if(src.empty())
return -1;
int wid = src.size().width;
// Show source image
imshow("Source Image", src); moveWindow("Source Image", 0, 0);
//-------------------------------------------
// Create binary image from source image
Mat bw;
cvtColor(src, bw, CV_BGR2GRAY);
threshold(bw, bw, 100, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
//threshold(bw, bw, 100, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);
imshow("Binary Image", bw); moveWindow("Binary Image", wid, 0);
//-------------------------------------------
// Perform the distance transform algorithm
Mat dist;
distanceTransform(bw, dist, CV_DIST_L2, 3);
// Normalize the distance image for range = {0.0, 1.0}
// so we can visualize and threshold it
normalize(dist, dist, 0, 1., NORM_MINMAX);
imshow("Distance Transform Image", dist); moveWindow("Distance Transform Image", 2*wid, 0);
waitKey(0);
return 0;
以下影像[1], 左手邊Source Image為輸入影像, 中間Binary Image為二值化結果, 最右邊為Distance Transform結果影像
Threshold = 200
Distance Transform
下載範例程式
下載測試影像
參考資料
全站熱搜
留言列表