像元純度指數算法C++實現




#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;
}

}





注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com