我们知道,构成一幅数字图像最基本的元素是一个一个的像素点,也就是像素。理解像素间的一些基本关系是我们以后进行图形图形处理的基础和关键。如相邻像素(像素的邻域),像素的邻接性、连通性、区域和边界这些内容。下面我想写一下像素间各种距离的定义以及用C++编写的程序来计算像素间的距离。
对于像素p(x , y),q(s , t),z(v , w),用D(p , q)来表示像素p , q间的距离,有:
一 像素间距离的定义(D(x , y)应满足的条件)
1 D(p , q) ≥ 0.(当且仅当p = q);
2 D(p , q) = D(q , p);
3 D(p , q) + D(q , z) ≥ D(p , z);
二 像素距离的分类及计算方法
1 欧式距离(Euclidean Distance)
(1)相信大家对这个距离公式是非常熟悉的,初中时就学了,也称它为两点间的距离。p和q之间的欧式距离定义如下:
De(p , q) =
对于像素p(x , y),q(s , t),z(v , w),用D(p , q)来表示像素p , q间的距离,有:
一 像素间距离的定义(D(x , y)应满足的条件)
1 D(p , q) ≥ 0.(当且仅当p = q);
2 D(p , q) = D(q , p);
3 D(p , q) + D(q , z) ≥ D(p , z);
二 像素距离的分类及计算方法
1 欧式距离(Euclidean Distance)
(1)相信大家对这个距离公式是非常熟悉的,初中时就学了,也称它为两点间的距离。p和q之间的欧式距离定义如下:
De(p , q) =

(2)距离直观描述:距点(x , y)小于或等于某一值r的欧式距离是中心在(x , y)半径为r的圆平面。
2 城区距离(City-Block Distance)
(1)p和q之间的城区距离定义如下:
D4(p , q) = |x - s| + |y - t|
(2)距离直观描述:距点(x , y)小于或等于某一值r的城区距离是中心在(x , y)对角线为2r的菱形。
3 棋盘距离(Chess Board Distance)
(1)p和q之间的棋盘距离定义如下:
D8(p , q) = max(|x - s| , |y - t|)
(2)距离直观描述:距点(x , y)小于或等于某一值r的棋盘距离是中心在(x , y)对角线为2r的正方形。
三 用C++实现的计算像素间距离的源程序
2 城区距离(City-Block Distance)
(1)p和q之间的城区距离定义如下:
D4(p , q) = |x - s| + |y - t|
(2)距离直观描述:距点(x , y)小于或等于某一值r的城区距离是中心在(x , y)对角线为2r的菱形。
3 棋盘距离(Chess Board Distance)
(1)p和q之间的棋盘距离定义如下:
D8(p , q) = max(|x - s| , |y - t|)
(2)距离直观描述:距点(x , y)小于或等于某一值r的棋盘距离是中心在(x , y)对角线为2r的正方形。
三 用C++实现的计算像素间距离的源程序
#include<math.h>
#include <iostream.h>
class Point
{
public:
Point(int xValue=0,int yValue=0)
{
x = xValue;
y = yValue;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
private:
int x,y;
};
class pixelDistance
{
public:
pixelDistance(Point pointValueA,Point pointValueB,int distanceTypeValue)
{
pointA = pointValueA;
pointB = pointValueB;
distanceType = distanceTypeValue;
}
pixelDistance(){};
double getPixelDistance();
private:
Point pointA;
Point pointB;
int distanceType;
};
double pixelDistance::getPixelDistance()
{
switch(distanceType) {
//欧式距离
case 0:
return sqrt((pointA.getX() - pointB.getX()) * (pointA.getX() - pointB.getX()) + (pointA.getY() - pointB.getY()) * (pointA.getY() - pointB.getY()));
break;
//城区距离
case 1:
return abs(pointA.getX() - pointB.getX()) + abs(pointA.getY() - pointB.getY());
break;
//棋盘距离
case 2:
return abs(pointA.getX() - pointB.getX()) > abs(pointA.getY() - pointB.getY()) ? abs(pointA.getX() - pointB.getX()) : abs(pointA.getY() - pointB.getY());
break;
default:
return 0;
break;
}
}
void main()
{
pixelDistance pd;
Point p1,p2;
int p1x,p1y,p2x,p2y;
double dValue;
int dType;
char * dTypeStr;
cout << "Please choice the type of distanse and two points' value. 0--Euclidean Distance; 1--City Block Distance; 2--Chess Board Distance." << endl;
cin >> dType >> p1x >> p1y >> p2x >> p2y;
while ((dType>3) || (dType <0)) {
cout << "Sorry! You choice wrongly. Please choice again."<< endl;
cin >> dType;
}
switch(dType) {
case 0:
dTypeStr = "Euclidean Distance";
break;
case 1:
dTypeStr = "City Block Distance";
break;
case 2:
dTypeStr = "Chess Board Distance";
break;
}
p1 = Point(p1x,p1y);
p2 = Point(p2x,p2y);
pd = pixelDistance(p1,p2,dType);
dValue = pd.getPixelDistance();
cout << "The Type Of Distance is " ;
cout << dTypeStr;
cout << ",The Value Of Distance is ";
cout << dValue <<endl;
}