📄 clustering.cpp
字号:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
#include<math.h>
#include<process.h>
#include<string.h>
#include<stdlib.h>
void readFile(char* fileName, char *ext, unsigned char ***image, int rows, int cols, int bands);
//void saveFile(char *fileName, char *ext, unsigned char image[b][r][c], int rows, int cols, int bands);
void saveFile(char *fileName, char *ext, unsigned char ***image, int rows, int cols);
char* strCat(char* srcStrs[], int n);
double pytho(int x1,int y1,int z1,int x2,int y2,int z2);
void merge(float center[4][256], int i, int j, unsigned char pix[128][128], int &noc,int r,int c);
void main()
{
clrscr();
int R=15;
int C=30;
int N=2000;
int noc = 0;
int Cmax=20;
int b, r, c;
b=3;
char dirname[20];
unsigned char ***image;
float center[4][256];
center[0][0]=0.0;
center[1][0]=0.0;
center[2][0]=0.0;
cout<<"enter the file name";
cin>>dirname;
cout<<"enter the size";
cin>>r>>c;
image=(unsigned char***)new unsigned char**[b];
for(int i=0;i<b;i++)
{
image[i]=(unsigned char**)new unsigned char*[r];
for(int j=0;j<r;j++)
{
image[i][j]=new unsigned char[c];
}
}
readFile(dirname, "raw", image, r, c, b);
unsigned char pix[128][128];
int nop=0;
for(int a=0;a<r;a++) {
printf("\b\b\b\b%3d%%", a);
for(int i=0;i<c;i++) {
nop++;
for(int j=0;j<noc;j++) {
float re=pytho(center[0][j], center[1][j], center[2][j], image[0][a][i], image[1][a][i], image[2][a][i]);
if(re<R) {
break;
}
}
if(j<noc) {
center[0][j]=(center[0][j]*center[3][j]+image[0][a][i])/(center[3][j]+1);
center[1][j]=(center[1][j]*center[3][j]+image[1][a][i])/(center[3][j]+1);
center[2][j]=(center[2][j]*center[3][j]+image[2][a][i])/(center[3][j]+1);
center[3][j]++;
pix[a][i] = j;
} else if(noc<Cmax) {
center[0][noc]=image[0][a][i];
center[1][noc]=image[1][a][i];
center[2][noc]=image[2][a][i];
center[3][noc]=1;
pix[a][i] = noc;
noc++;
}
else
{
int small=1000;
int t=0;
for(int m=0;m<Cmax;m++)
{
int min=pytho( center[0][m], center[1][m], center[2][m], image[0][a][i], image[1][a][i], image[2][a][i]);
if(min<small)
{
small=min;
t=m;
}
}
center[0][t]=(center[0][t]*center[3][t]+image[0][a][i])/(center[3][t]+1);
center[1][t]=(center[1][t]*center[3][t]+image[1][a][i])/(center[3][t]+1);
center[2][t]=(center[2][t]*center[3][t]+image[2][a][i])/(center[3][t]+1);
center[3][t]++;
pix[a][i] = t;
}
if(nop>N) {
for(int ii=0;ii<noc;ii++) {
for(int j=ii+1;j<noc;j++) {
float me=pytho(center[0][ii], center[1][ii], center[2][ii], center[0][j], center[1][j], center[2][j]);
if(me<C) {
merge(center, ii, j, pix, noc,r,c);
}
}
}
}
}
}
FILE*fp;
//fp=new FILE;
fp=fopen("test5.raw","wb");
printf("\n%d\n", noc);
for(i=0;i<r;i++) {
for(int j=0;j<c;j++) {
//printf("%d ", pix[i][j]);
pix[i][j]=(256/noc)*pix[i][j];
}
//printf("\n");
}
for(i=0;i<r;i++) {
for(int j=0;j<c;j++){
fprintf(fp,"%c", pix[i][j]);
}
}
getch();
}
double pytho(int x1,int y1,int z1,int x2,int y2,int z2)
{
double res,num;
num=(pow((x2-x1),2)+pow((y2-y1),2)+pow((z2-z1),2));
res= sqrt(num);
return res;
}
void readFile(char* fileName, char *ext, unsigned char ***image, int rows, int cols, int bands){
FILE *file;
{
char* fNs[] = {fileName, ".", ext};
char* fName = strCat(fNs, 3);
if( (file = fopen(fName, "rb")) == NULL ){
printf("File(%s) does not exists.", fName);
getch();
exit(0);
}
}
unsigned char *buff = new unsigned char[bands*cols];
for(int i=0;i<rows;i++){
int readReturn = (int) fread(buff, sizeof(unsigned char), bands*cols, file);//FileObjectIn.Read(buff, bands*cols);
if( readReturn != bands*cols){
printf("Error while reading file at row(%d). Only %d data was read.", i, readReturn);
getch();
exit(0);
}
for(int j=0;j<cols;j++){
for(int k=0;k<3;k++)
{
image[k][i][j] = buff[j*bands+k];
}
}
}
fclose(file);
delete []buff;
}
void saveFile(char *fileName, char *ext, unsigned char ***imageArray, int rows, int cols, int bands){
FILE *file;
{
char* fNs[] = {fileName, ".", ext};
char* fName = strCat(fNs, 3);
if( (file = fopen(fName, "wb")) == NULL ){
printf("File(%s) does not exists.", fName);
getch();
exit(0);
}
}
unsigned char *buf = new unsigned char[bands*cols];
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
for(int k=0;k<bands;k++){
buf[j*bands+k] = (unsigned char) imageArray[k][i][j];
}
}
fwrite(buf, sizeof(unsigned char), bands*cols, file);
}
fclose(file);
delete []buf;
}
char* strCat(char* srcStrs[], int n){
int i, length = 0;
for(i=0; i<n; i++){
length += strlen(srcStrs[i]);
}
char *destStr = new char[length];
strcpy(destStr, srcStrs[0]);
for(i=1; i<n; i++){
destStr = strcat(destStr, srcStrs[i]);
}
return destStr;
}
void merge(float center[4][256] ,int i,int j,unsigned char pix[128][128], int &noc,int r,int c)
{
int ii, jj;
center[0][i]=(center[0][i]*center[3][i]+center[0][j]*center[3][j])/(center[3][i] + center[3][j]);
center[1][i]=(center[1][i]*center[3][i]+center[1][j]*center[3][j])/(center[3][i] + center[3][j]);
center[2][i]=(center[2][i]*center[3][i]+center[2][j]*center[3][j])/(center[3][i] + center[3][j]);
center[3][i] = center[3][i] + center[3][j];
for(int k=j;k<noc-1;k++)
{
center[0][k]= center[0][k+1];
center[1][k]=center[1][k+1];
center[2][k]= center[2][k+1];
center[3][k]=center[3][k+1] ;
}
--noc;
for(ii=0;ii<r;ii++)
{
for(jj=0;jj<c;jj++)
{
if(pix[ii][jj]==j)
{
pix[ii][jj]=i;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -