象素间各种距离的定义及计算
对于像素p(x
一
二
De(p
D4(p
D8(p
三
#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;
}
posted on 2011-07-17 16:19 MorningChen 阅读(12052) 评论(0) 收藏 举报
浙公网安备 33010602011771号