关闭

像元纯度指数算法C++实现

标签: 像元纯度指数算法PPI算法C++聚类
252人阅读 评论(0) 收藏 举报
分类:



#include "stdafx.h"
# include <iostream>
#include <math.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	//行数
	const int n=10000; 
	//散点坐标
	double x[n];
	double y[n];
	double xSum=0;
	double ySum=0;

	//随机生成坐标点
	for (int i=0;i<n;i++)
	{
		x[i]=rand()%n;
		y[i]=rand()%n;
	} 

	//投影点的平均值
	double xAve;
	double yAve;
	//像元纯度指数
	double PPI[n];

	for (int i=0;i<n;i++)
	{
		PPI[i]=0;
	}
	//基向量的不同角度
	int angle[6]={90,30,60,0,120,150};
	//基向量斜率
	double slope;
	
	for (int k=0;k<6;k++)
	{
		xSum=0;
		ySum=0;
		cout<<angle[k]<<" ";
		//垂直向量
		if (angle[k]==90)
		{
			xAve=0;
			//求Y的平均值
 			double yTempSum=0;
 			for (int i=0;i<n;i++)
 			{
 				yTempSum+=y[i];
 			} 
 			yAve=yTempSum/n;
		}
		//水平向量
		else if (angle[k]==0)
		{
			yAve=0;
			//求x平均值
			double xTempSum=0;
			for (int i=0;i<n;i++)
			{
				xTempSum+=x[i];
			} 
			xAve=xTempSum/n;
		}
		else
		{
			double xTempSum=0;
			double yTempSum=0;
			//求投影坐标平均值
			slope=tan((double)angle[k]*3.1415926/180);
			//计算每个点投影坐标,并加和
			for (int i=0;i<n;i++)
			{
				xTempSum+=(x[i]+slope*y[i])/(slope*slope+1);
				yTempSum+=slope*(x[i]+slope*y[i])/(slope*slope+1);				
			} 
			xAve=xTempSum/n;
			yAve=yTempSum/n;
		}
		cout<<xAve<<" "<<yAve<<endl;

		//计算PPI总和
		for (int i=0;i<n;i++)
		{
			if (angle[k]==90)
			{
				PPI[i]+=abs(y[i]-yAve);
			}
			else if (angle[k]==0)
			{
				PPI[i]+=abs(x[i]-xAve);
			}
			else
			{
				slope=tan((double)angle[k]*3.1415926/180);
				//点在基向量上的投影坐标
				double xProj=(x[i]+slope*y[i])/(slope*slope+1);
				double yProj=slope*(x[i]+slope*y[i])/(slope*slope+1);
				PPI[i]+=sqrt((xProj-xAve)*(xProj-xAve)+((yProj-yAve))*(yProj-yAve));
			}
		}		
	}
	//对PPI进行冒泡排序:递增
	for (int i=0;i<n;i++)
	{		
		for (int j=0;j<n-i-1;j++)
		{
			double currLenj=PPI[j];
			double currlenj1=PPI[j+1];

			if (currLenj>currlenj1)
			{
				//交换顺序x
				int tempX=x[j];
				x[j]=x[j+1];
				x[j+1]=tempX;	

				//交换顺序y
				int tempY=y[j];
				y[j]=y[j+1];
				y[j+1]=tempY;	

				//交换每个PPI顺序
				double tempLen=PPI[j];
				PPI[j]=PPI[j+1];
				PPI[j+1]=tempLen;

			}
		}
	}

	int m=n*0.85;

	for (int j=0;j<m;j++)
	{
		double length=PPI[j]/100000;
		cout<<length<<" "<<x[j]<<" "<<y[j]<<endl;
	}
}


#include "stdafx.h"
# include <iostream>
using namespace std;
#include <fstream>
 


int _tmain(int argc, _TCHAR* argv[])
{
	//行数
	const int n=10000; 

	double a[n][2];
	double xSum=0;
	double ySum=0;

	for (int i=0;i<n;i++)
	{
		a[i][0]=rand()%n;
		a[i][1]=rand()%n;
		cout<<a[i][0]<<" "<<a[i][1]<<endl;
		xSum+=a[i][0];
		ySum+=a[i][1];
	} 

	cout<<xSum<<" "<<ySum<<endl;
	double xAve=xSum/n;
	double yAve=ySum/n;
	cout<<xAve<<" "<<yAve<<endl;
	//计算距离
	double maxLen=0;
	int maxIndex=0;
	//排序后位置
	int location[n];
	
	//冒泡排序
	for (int i=0;i<n;i++)
	{		
		for (int j=0;j<n-i-1;j++)
		{
			double currLenj=sqrt((a[j][0]-xAve)*(a[j][0]-xAve)+(a[j][1]-yAve)*(a[j][1]-yAve));
			double currlenj1=sqrt((a[j+1][0]-xAve)*(a[j+1][0]-xAve)+(a[j+1][1]-yAve)*(a[j+1][1]-yAve));

			if (currLenj>currlenj1)
			{
				//交换顺序x
				int tempX=a[j][0];
				a[j][0]=a[j+1][0];
				a[j+1][0]=tempX;	

				//交换顺序y
				int tempY=a[j][1];
				a[j][1]=a[j+1][1];
				a[j+1][1]=tempY;				
			}
		}
		location[i]=maxIndex;
	}
	int m=n*0.85;
	for (int j=0;j<m;j++)
	{
		double length=(a[j][0]-xAve)*(a[j][0]-xAve)+(a[j][1]-yAve)*(a[j][1]-yAve);
		cout<<length<<" "<<a[j][0]<<" "<<a[j][1]<<endl;
	}
	
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5749次
    • 积分:78
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:6篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论