资源简介
FCM_S聚类图像分割程序,经过测试能取得比FCM更好的分割效果
代码片段和文件信息
// FCM_S.cpp: 主项目文件。
//iamzxf@126.com
//QQ 250129131
#include “stdafx.h“
#include “cv.h“
#include “highgui.h“
#include “cxcore.h“
#include “time.h“
#include “WTypes.h“
using namespace System;
#define height 184
#define width 180
#define cluster_n 2
#define LEVEL 256
#define iters 100
#define mm 1.75
#define thres 0.00001
#define alpha 2
#define p 0
#define q 1
double membership[height][width][cluster_n];
double center[cluster_n];
double distance[iters];
double temp_sumw;
int main(array ^args)
{
IplImage *pImg*newImg;
pImg=cvLoadImage(“tu2.jpg“0);
if(!pImg){
printf(“cannot load the file.\n“);
return -1;
}
uchar *data*newdata;
int channels stepdepth;
depth=pImg->depth;
step=pImg->widthStep;
channels=pImg->nChannels;
data=(uchar *) pImg->imageData;
int ijkmnkk;
int member_sum;
double temp_sum;
int it;
SYSTEMTIME sys;
GetLocalTime( &sys);
printf(“\nthe time is %d%d.%d\n“sys.wMinutesys.wSecondsys.wMilliseconds);
//initialize membership matrix
srand((int)time(0));
FILE *pF*fF;
pF=fopen((char *)“data1.txt““w“);
//fF=fopen((char *)“data2.txt““w“);
if (NULL==pF)
{
printf(“open write file error!!“);
return 1;
}
for(i=0;i {
for(j=0;j {
member_sum=0;
for(k=0;k {
membership[i][j][k]=rand()+1;
member_sum+=membership[i][j][k];
}
for(k=0;k {
membership[i][j][k]/=member_sum;
}
}
}
/* for(i=178;i<=180;i++)
{
for(j=140;j<=142;j++)
{
fprintf(fF “ %03u “data[i*step+j] );
}
fprintf(fF “\n“);
}
fclose(fF);
*/
for(i=178;i<=180;i++)
{
for(j=140;j<=142;j++)
{
fprintf(pF “%lf\n“ membership[i][j][0]);
}
}
fprintf(pF “\n“);
//compute the initialized cluster centers
double sum_downsum_upsum_gray;
int num_neigh;
int updownleftright;
for(k=0;k {
sum_down=0;
sum_up=0;
for(i=0;i {
for(j=0;j {
sum_down+=(1+alpha)*pow(membership[i][j][k]mm);
num_neigh=0;
sum_gray=0;
up=i-1;
down=i+1;
left=j-1;
right=j+1;
if(up<0) up=0;
if(down>height) down=height;
if(left<0) left=0;
if(right>width) right=width;
for(m=up;m<=down;m++)
{
for(n=left;n<=right;n++)
{
sum_gray+=data[m*step+n];
num_neigh++;
}
}
sum_up+=pow(membership[i][j][k]mm)*(data[i*step+j]+(sum_gray-data[i*step+j])*alpha/(num_neigh-1));
}
}
center[k]=sum_up/sum_down;
}
for(i=0;i {
printf(“center[%d]=%f \n“i+1center[i]);
}
//compute the distance[0]
for(it=0;it {
distance[it]=0;
}
for(i=0;i {
for(j=0;j {
for(k=0;k {
distance[0]+=pow(membership[
- 上一篇:信息熵的C语言实现
- 下一篇:图像处理 梯度锐化 C/C++语言实现
评论
共有 条评论