#include "all.h"
double FCMeans(int *first,int* result, int clusterNum, int cols, int rows, int featureNum, int m_Value)
{
int sampleNum = cols * rows;
double j_before = 0;
double j_now = 0;
int flag = 0;//隶属度是否为1的标识符。
//创建来记录隶属度值
//Mat u_account= Mat::zeros(sampleNum, clusterNum, CV_64FC1);
vector<double>u_account;
u_account.resize(sampleNum*clusterNum);
//创建聚类中心,并随机初始化
vector<double>center;
center.resize(clusterNum*featureNum);
for (int i = 0; i < clusterNum; i++)
{
for (int j = 0; j < featureNum; j++)
center[i*featureNum + j] = (255.0 / clusterNum) *(i + 1);
}
//创建距离,并初始化
vector<double>distance;
distance.resize(sampleNum*clusterNum);
for (int i = 0; i < sampleNum; i++)//遍历像素点的循环
{
for (int k = 0; k < clusterNum; k++)//聚类循环
{
double dis = 0;
for (int n = 0; n < featureNum; n++)
{
double temp = 0;
temp = center[k*featureNum + n] - first[i*featureNum + n];
dis = dis + temp *temp;
}
distance[i*clusterNum + k] = dis;
}
}
j_before = 0;
j_now = 0;
//*********************************开始聚类
for (int i = 0; i < MAX; i++)
{
j_before = j_now;//记录上一次循环损失函数的值
j_now = 0; //初始化这一次损失函数的值
//计算隶属度矩阵
for (int i = 0; i < sampleNum; i++)
{
flag = 0;
double top = 0;//每一个点的隶属度的分子
double base = 0;//每一个点的隶属度的分母;计算每个类的隶属度时分母不变
int k_save = 0;//记录距离为0的类别
for (int k = 0; k < clusterNum; k++)
{
double now_dis = 0;//记录当前当前坐标与聚类中心的距离
now_dis = distance[i*clusterNum + k];
if (now_dis != 0)
{
base = base + pow(1.0 / now_dis, 1.0 / (m_Value - 1));//算分母的值
}
else
{
flag = 1;
k_save = k;
break;
}
}
if (flag == 1)
{
for (int k = 0; k < clusterNum; k++)
{
u_account[i*clusterNum + k] = 0;
}
u_account[i*clusterNum + k_save] = 1;
}
else
{
for (int k = 0; k < clusterNum; k++)
{
top = pow(1.0 / distance[i*clusterNum + k], 1.0 / (m_Value - 1));//算分子的值
u_account[i*clusterNum + k] = top / base;
}
}
}
//修改新的聚类中心;聚类中心是featureNum个维度;clusterNum个;不同维度的分母值是一样的,分子不一样
for (int k = 0; k < clusterNum; k++)//类别遍历
{
double base = 0; //每个维度的分母是一样的,所以在这里初始化,而且下面也只计算了一遍
for (int j = 0; j < featureNum; j++)
{
double top = 0;
for (int i = 0; i < sampleNum; i++)
{
double a = 0;//分子分母的相似部分
a = pow(u_account[i*clusterNum + k], m_Value);/////
if (j == 0) //只计算一次分母值
{
base = base + a;
}
top = top + a * first[i*featureNum + j];
}
center[k*featureNum + j] = top / base;
}
}
//更新距离数组,同时计算j的值
for (int i = 0; i < sampleNum; i++)//遍历像素点的循环
{
for (int k = 0; k < clusterNum; k++)//聚类循环
{
double dis = 0;
for (int n = 0; n < featureNum; n++)
{
double temp = 0;
temp = center[k*featureNum + n] - first[i*featureNum + n];
dis = dis + temp *temp;
}
distance[i*clusterNum + k] = dis;
j_now = j_now + pow(u_account[i*clusterNum + k], m_Value)*dis;//记录当前循环损失函数的值
}
}
if (fabsl(j_now - j_before) <= e)
break;
}
///聚类完毕,修改输出的像素值
for (int i = 0; i < sampleNum; i++)
{
int k_save = 0;//记录最大隶属度的类别
double temp = 0;//比较隶属度用的变量
for (int k = 0; k < clusterNum; k++)
{
if (u_account[i*clusterNum + k] > temp)
{
temp = u_account[i*clusterNum + k];
k_save = k;
}
}
for (int j = 0; j < featureNum; j++)
{
result[i*featureNum + j] = center[k_save*featureNum + j]+0.5;
}
}
/* for (int i = 0; i < 20; i++)
{
for (int j = 0; j < clusterNum; j++)
{
cout << u_account[i*clusterNum + j] << " ";
}
cout << endl;
} //看隶属度值的函数
*/
cout << endl;
cout << clusterNum << "个聚类中心的像素值为: " << endl;
for (int i = 0; i < clusterNum; i++)
{
for (int j = 0; j < featureNum; j++)
{
cout << center[i*featureNum + j] << " ";
}
cout << endl;
}//看聚类中心的函数
return j_now;
}
void suit_FCM(int *first, int* result, int cols, int rows, int featureNum, int m_Value)
{
int num = cols*rows;
double j_now = 0;
double j_before = 0;
j_before = FCMeans(first, result, 2, cols, rows, featureNum, 2);
cout << j_before << endl;
for (int i = 3; i < pow(num,0.5); i++)//pow(num, 0.5)
{
j_now = FCMeans(first, result, i, cols, rows, featureNum, 2);
cout << j_now << endl;
if (-j_now + j_before <= j_e)
{
cout << "最终聚类的类别为: " << i << endl;
break;
}
j_before = j_now;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
模糊C均值.zip (28个子文件)
模糊C均值
模糊C均值.sln 985B
模糊C均值.v12.suo 22KB
Debug
模糊C均值.ilk 1.78MB
模糊C均值.pdb 1.85MB
模糊C均值.exe 90KB
模糊C均值
main.cpp 1KB
fcm.cpp 5KB
all.h 894B
12.jpg 30KB
模糊C均值.vcxproj.filters 1KB
Debug
fcm.obj 246KB
模糊C均值.log 3KB
vc120.idb 651KB
main.obj 202KB
模糊C均值.tlog
模糊C均值.lastbuildstate 183B
link.read.1.tlog 1KB
link.command.1.tlog 1004B
CL.write.1.tlog 2KB
cl.command.1.tlog 2KB
CL.read.1.tlog 43KB
link.write.1.tlog 490B
vc120.pdb 932KB
copy.obj 3KB
1.jpg 102KB
123.jpg 10KB
模糊C均值.vcxproj 4KB
1234.jpg 134KB
模糊C均值.sdf 14.69MB
共 28 条
- 1
资源评论
rrblog
- 粉丝: 3
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功