象素间各种距离的定义及计算

本文介绍了数字图像中像素间的基本关系,包括相邻像素、邻接性、连通性等概念,并详细解析了三种像素距离——欧式距离、城区距离和棋盘距离的定义与计算方法。此外,还提供了一段C++代码示例,用于演示如何计算像素间的不同距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们知道,构成一幅数字图像最基本的元素是一个一个的像素点,也就是像素。理解像素间的一些基本关系是我们以后进行图形图形处理的基础和关键。如相邻像素(像素的邻域),像素的邻接性、连通性、区域和边界这些内容。下面我想写一下像素间各种距离的定义以及用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) =       
 (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++实现的计算像素间距离的源程序
#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;
}
from http://blog.sina.com.cn/s/blog_5061d31a01008vgp.html

转载于:https://www.cnblogs.com/clarkchen/archive/2011/07/17/2108748.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值