tt.c为K均值算法,我想在C6747片中运行,可是当图片尺寸超过300*300时就会报错,说.far大小不够,.cmd文件也在附件中,请指点
C文件为:
#include <stdio.h>
#include <math.h>
#include "C6747.h"
#include <stdlib.h>
///////////////////////////////////kmeans #define ROW 150 //** -输入数据每行数据个数 #define COLUMN 150 //** – 输入数据每列数据个数 #define K_clusters 2 //** – 所分数据的簇个数,它比实际值大1,比如:分5簇,该值应该设置为6 #define num_km (ROW*COLUMN+1) //** – 行列数据总个数,它为行列数据相乘再加1,比如:num_km=ROW*COLUMN+1; ///////////////////////////////////kmeans
void kmeans(void);//Kmeans函数声明
///////////////////////////////////kmeans
short int d_km[ROW][COLUMN]={1};
//数据格式为灰度数据0-255之间的整数;如果为浮点数据,直接对浮点数据取整数才可以运算,因为灰度数据只能是整数
int ind[num_km]={0};
float old_cental[K_clusters],cental[K_clusters]; //经过kmeans算法处理之后的输出数据 int d_km_out[ROW][COLUMN]={0},serial_km[num_km];//经过kmeans算法处理之后的输出数据
#pragma DATA_SECTION(d_km,".ARRAY2");
#pragma DATA_SECTION(ind,".ARRAY2");
#pragma DATA_SECTION(serial_km,".ARRAY2"); ///////////////////////////////////kmeans
void main(void)
{ int i,j;
///////////////////wzh
/* output cluster label for each data point */ //////////////////wzh
C6747_init(); printf("DSP初始化结束\n"); d_km[0][0]=1; for (i=1;i<COLUMN;i++)
for(j=0;j<ROW;j++)
{ d_km[0][0]=0; }
for(i=0;i<1000;i++)
{
kmeans(); GPIO_OUT_DATA23=0xffff;//GPIO输出高电平
kmeans(); GPIO_OUT_DATA23=0x0000;//GPIO输出低电平
}
}
/////////////////////////////////////////////////////functioon kmeans
void kmeans(void)
{
int flag=0,temp; int i, j,k,km_temp=0; float c[K_clusters]={0},m_a,m_h; int h[257]={0};
int aa[257]={0},hc[257]={0};
float min_km=255,max_km=0,minmin=0;
for (i=0;i<COLUMN;i++)
for(j=0;j<ROW;j++)
serial_km[i*ROW+j+1]=d_km[j][i];//对数据串行化处理
for (i = 1; i < num_km; i++)
{ if (serial_km[i] < min_km)
{
min_km = serial_km[i];
}
}//得到最小值数据
minmin=min_km;//保存下最小值数据作为最后使用
for (i = 1; i < num_km; i++)
{ serial_km[i]=serial_km[i]-min_km+1;
if (serial_km[i] > max_km)
{
max_km = serial_km[i]; }
}
max_km=max_km+1;
//printf("max_km_%f \n",max_km);
//% create image histogram
km_temp=0;
for (i = 1; i < num_km; i++)
{ if(serial_km[i]>0)
{ h[serial_km[i]]=h[serial_km[i]]+1;
km_temp++;
}
}//h(ima(i))=h(ima(i))+1;
km_temp=1;
//for (i = 1; i < 256; i++)
// {printf("%d ",ind[i]);}
// printf("\n ");
for (i = 1; i < 257; i++) { if(h[i]!=0)
{ ind[km_temp++]=i;
}
}//hl=km_temp;
//hl=length(ind);
//printf("km_temp_%d \n", km_temp);
for (i = 1; i < K_clusters; i++)
{cental[i]=i*max_km/(K_clusters);
}// initiate centroids
// printf("\n ");
while(1)
{for (i = 1; i < K_clusters; i++)
{ old_cental[i]=cental[i];
};//current classification oldmu=mu;
//ind,lengthind,h1
for (i = 1; i < km_temp; i++)// for i=1:hl=km_temp
{ for(j = 1; j < K_clusters; j++)
{c[j]=fabs(cental[j]-ind[i]);
// printf("c_%d ",c[j]);
} //c=abs(ind(i)-mu);
// printf("\n");
min_km=255;//每次循环都初始化
for (j = 1; j < K_clusters; j++)
{if (c[j] < min_km)
{
min_km = c[j];
temp=j;//temp=cc(1);
}
}
// printf("min_%d ",min_km);
hc[ind[i]]=temp;//hc[ind[i]]=temp; }//end for
for(i = 1; i < K_clusters; i++)// for i=1:k { k=1; for (j = 1; j < 257; j++) //全局搜索
{ //printf("hccc_%d ",hc[j]);
if(i==hc[j])
{
aa[k]=j;
k++;
} }//a=find(hc==i); m_a=0;
m_h=0;
for (j = 1; j < k; j++)
{m_a=m_a+h[aa[j]];
}
//printf("m_a_%f ",m_a);
// printf("k_%d ",k);
for (j = 1; j < k; j++)
{aa[j]=aa[j]*h[aa[j]];
m_h=aa[j]+m_h;
}
// printf("m_h_%f\n ",m_h);
cental[i]=m_h/m_a; } // mu(i)=sum(a.*h(a))/sum(h(a));
flag=0; for(i = 1; i < K_clusters; i++)
{if (cental[i]!=old_cental[i])
{flag=flag+1;}
} if (flag==0)
{//printf("\nflag=%d\n",flag);
break;}
}//end while
//% calculate mask //printf("over");
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COLUMN; j++) { for(k = 1; k < K_clusters; k++)
{c[k]=(int)fabs(cental[k]-d_km[i][j]);
} //c=abs(copy(i,j)-
min_km=255;
for (k = 1; k < K_clusters; k++)
{if (c[k] < min_km)
{
min_km = c[k];
km_temp=k;
} }//a=find(c==min(c)); d_km_out[i][j]=km_temp;
// printf("%d ", d_km_out[i][j]);
}
// printf("\n ");
}//% calculate mask end
for (k = 1; k < K_clusters; k++)
{cental[k]=cental[k]+minmin-1;
};
//mu=mu+mi-1; % recover real range
printf("over!");
}
CMD文件:
-l rts6700.lib
-l dsp67x.lib
-stack 0x00001000 /* Stack Size */
-heap 0x00001000 /* Heap Size */
MEMORY
{
VECS: o = 0x80000000 l = 0x00000f00
ARMRAM: o = 0xFFFF0080 l = 0x00001f80
DSPL2RAM: o = 0x00800000 l = 0x00040000
SHAREDRAM: o = 0x80000f00 l = 0x0001ffff
SDRAM1: o = 0xC0000000 l = 0x00800000
SDRAM2: o = 0xC0800000 l = 0x03800000
}
SECTIONS
{ ".vectors" > VECS
.bss > DSPL2RAM
.cinit > DSPL2RAM .cio > DSPL2RAM .const > DSPL2RAM
.stack > DSPL2RAM
.sysmem > DSPL2RAM
.text > SHAREDRAM
.switch > DSPL2RAM
.far > SHAREDRAM
.ARRAY1 > SDRAM1
.ARRAY2 > SDRAM2
}
Tony Tang:
没看到出错信息。我想你的问题是在link时报.far太大放不下,SHAREDRAM是128KB, 分配到SDRAM里试试。