Job Recruitment Website - Job seeking and recruitment - Seek the source code of maximum likelihood method, thank you.
Seek the source code of maximum likelihood method, thank you.
/****************************************************/
/* This program achieves maximum likelihood */
/* Classification program. */
/* output: 1. Classification image, and 2. Probability file */
/* Note: For construction differences-*/
/* Covariance matrix must be a universal binary file. */
/****************************************************/
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
# include & ltmalloc.h & gt
# include & ltmath.h & gt
#define img_maximum 1024
Double det [22];
Master ()
{
FILE *fin[22],* fout[5];
int i,j,img_row,img_col,num_of_class,num _ of _ band
int count 1,count2,count3,count4,store 1;
int num_of_pixel[22],class;
Double pai=6.28, mini _ f;;
double num_band,t_2,t _ 3;
Double s[22][22][22], s 1[22][22], is[22][22], mean _ xi _1[22] [22];
Double subtraction _x[22][22], sum_of_x[22][22], mean _ xi _ a [22] [200];
double m[22][22],tx[22][22],sum_of_wi=0,temp[2][2];
Double xi[22][22], p0 = 2, p 1=0.5, P2 =1.25;
Unsigned character storage [22][ 1], read_pixel[22][img_maximum], in_file[50], out _ file [50];
Long address [22];
fpos _ t * file _ start[22];
Printf(" \ n = = = = Maximum Likelihood Classification = = = \ n ");
Printf ("Please enter a file name to record the number of training pixels of each class:");
scanf("%s ",in _ file);
If((fin[0] = fopen(in_file, "r))= = NULL)/* Number of pixels in each training group */
{
Printf ("Failed to open %s\n", in _ file);
Exit (exit _ failure);
}
Printf ("Please enter the file name of the image to be classified (interlaced band line by line):");
scanf("%s ",in _ file);
If ((fin [21] = fopen (in _ file, "Rb))= = NULL)/* Classified image: bands staggered by lines */
{
Printf ("Failed to open %s\n", in _ file);
Exit (exit _ failure);
}
Printf ("Please enter the total number of image bands (up to 20):");
Scanf("%d ",& number of bands);
for(count 1 = 1; count 1 & lt; = number of bands; count 1++)
{
file _ start[count 1]= & amp; Address [count1];
Printf ("Please enter the training file name of band %d:", count1);
scanf("%s ",in _ file);
if((fin[count 1]= fopen(in _ file," rb")) == NULL)
{
Printf ("Failed to open %s\n", in _ file);
Exit (exit _ failure);
}
}
Printf ("\ nImage input completed! \ n ");
Printf ("Output probability record file:");
scanf("%s ",out _ file);
If ((fout [1] = fopen (out _ file, "w "))= = NULL)/* Output probability file */
{
Printf ("Failed to open %s\n", out _ file);
Exit (exit _ failure);
}
Printf ("output classified image:");
scanf("%s ",out _ file);
If((fout[2] = fopen(out_file, "WB")) = = null)/* Output classification file */
{
Printf ("Failed to open %s\n", out _ file);
Exit (exit _ failure);
}
Printf ("Please enter the total number of classes:");
Scanf("%d ",& number of classes);
Printf ("Please enter the number of image lines in each band:");
scanf("%d ",& ampimg _ row);
Printf ("Please enter the number of image columns in each band:");
scanf("%d ",& ampimg _ col);
cleanmatrix(xi,22, 1);
cleanmatrix(sum_of_x,22, 1);
for(count 1 = 1; count 1 & lt; = number of bands; count 1++)
fgetpos(fin[count 1],file _ start[count 1]);
Printf ("/nCalculate the average of each class \ n");
for(count 1 = 0; count 1 & lt; Number of classes; Count 1++)/* Average value of each category */
{
Fscanf(fin[0], "%d", & number of pixels [count1]); /* Read the number of training pixels */
printf(" %d ",num _ of _ pixel[count 1]);
for(count 2 = 0; Count2 & lt band number; count2++)
mean _ Xi _ a[count 2][count 1]= 0;
for(count 2 = 0; Count2 & lt number of pixels [count1]; count2++)
{
for(count 3 = 0; Count3 & lt band number; count3++)
{
fread(store[count3], 1, 1,fin[count 3+ 1]);
store 1 = store[count 3][0];
Xi[count 3][0]=(double)store 1;
Mean _ Xi _ a[ Count3] [Count3/kloc-0] = mean _ Xi _ a[ Count3] [Count3 1]+Xi[ Count3] [0];
}
}
Printf(" \n Training average of class %d: \ n ",count1);
for(count 2 = 0; Count2 & lt band number; count2++)
{
Mean _ Xi _ a[ Count 2][ Count 1]=(double)mean _ Xi _ a[ Count 2][ Count 1]/ Number of pixels [Count1];
printf(" %f ",mean _ Xi _ a[count 2][count 1]);
}
printf(" \ n ");
}/* End of counting 1*/
for(count 1 = 1; count 1 & lt; = number of bands; count 1++)
fsetpos(fin[count 1],file _ start[count 1]);
for(count 1 = 0; count 1 & lt; Number of classes; Count 1++)/* covariance matrix and determine */
{
for(count 2 = 0; Count2 & lt band number; count2++)
Mean _ Xi _ 1[ Count2] [0] = mean _ Xi _ a[ Count2] [Count21];
cleanmatrix(s 1,22,22);
cleanmatrix(s[count 1],22,22);
for(count 2 = 0; Count2 & lt number of pixels [count1]; count2++)
{
for(count 3 = 0; Count3 & lt band number; count3++)
{
fread(store[count3], 1, 1,fin[count 3+ 1]);
store 1 = store[count 3][0];
Xi[count 3][0]=(double)store 1;
}
minusmatrix(xi,mean_xi_ 1,num_of_band, 1,MINUS _ x);
for(I = 0; I< band number; i++)
for(j = 0; J< band number; j++)
{
s[count 1][I][j]= s[count 1][I][j]+MINUS _ x[I][0]* MINUS _ x[j][0];
s 1[I][j]= s 1[I][j]+MINUS _ x[I][0]* MINUS _ x[j][0];
}
}/* End of counting 2*/
for(I = 0; I< band number; i++)
for(j = 0; J< band number; j++)
{
s[count 1][I][j]=(double)s[count 1][I][j]/num _ of _ pixel[count 1];
s 1[I][j]=(double)s 1[I][j]/num _ of _ pixel[count 1];
}
Detect (s1,band number, band number, count1);
Printf ("Determination of covariance matrix:");
printf(" %f ",det[count 1]);
}/* End of counting 1*/
for(count 1 = 0; count 1 & lt; Img _ rowcount 1++)/* classifier */
{
Printf ("Processing line %d \n", count1+1);
for(count 2 = 0; Count2 & lt band number; Count 2++)/* Read all band pixels line by line */
fread(read_pixel[count2], 1,img_col,fin[2 1]);
for(count 2 = 0; count2 & ltimg _ colcount2++)
{
for(I = 0; I< band number; i++)
Xi[i][0] = read pixel [i][ Count2];
mini _ f = 9999999999
for(count 3 = 0; Number of count3 & lt classes; count3++)
{
for(count 4 = 0; Count4 & lt band number; count4++)
Mean _ Xi _ 1[ Count4] [0] = mean _ xi _ a [Count4] [Count3];
for(I = 0; I< band number; i++)
for(j = 0; J< band number; j++)
s 1[I][j]= s[count 3][I][j];
Inverse (s 1, num_of_band, num_of_band, is);
Digital band = digital band; /* From int to double*/
minusmatrix(xi,mean_xi_ 1,num_of_band, 1,MINUS _ x);
Transpose (minus_x, num_of_band, 1, tx);
Multiplication (tx, is, 1, num_of_band, num_of_band, m);
multiply(m,minus_x, 1,num_of_band, 1,temp);
t _ 2 = 0.5 *(temp[0][0]+log(det[count 3])+num _ band * log(pai));
if(t _ 2 & lt; Mini _f)
{
mini _ f = T2;
clas = count3
}
T3 = exp(-T2);
fprintf(fout[ 1]," %lf ",-t _ 2);
End of }/* count 3 num_of_class*/
fprintf(fout[2]," %c ",class+ 1);
End of }/* count 2 img_col*/
}/* count1end of img _ row */
for(count 1 = 0; count 1 & lt; = number of bands; count 1++)
{
fclose(fin[count 1]);
}
fclose(fin[2 1]);
fclose(fout[ 1]);
fclose(fout[2]);
}
/* This function is to display the matrix */
Display matrix (z, nn, cc)
/* FILE * FILE 1; */
Double z [22] [22];
int nn,cc;
{
int i,j;
printf(" \ n ");
Printf ("The matrix is:! \ n ");
for(I = 0; I & ltnn;; i++)
for(j = 0; j & ltcc; j++)
{
printf("% 15f ",z[I][j]);
If (j == cc- 1)
{
printf(" \ n ");
}
}
}
Cleanmatrix(z, nn, cc)/* Clear the elements of matrix z */
Double z [22] [22];
int nn,cc;
{
int i,j;
for(I = 0; I & ltnn;; i++)
for(j = 0; j & ltcc; j++)
z[I][j]= 0;
}/* End of Clean Matrix */
Transpose (z, nn, cc, tz)/* Transpose Z matrix */
Double z[22][22], tz [22] [22];
int nn,cc;
{
int i,j;
for(I = 0; I & ltnn;; i++)
for(j = 0; j & ltcc; j++)
tz[j][I]= z[I][j];
}/* Transposition End */
Addmatrix(z 1, z2, row, col, item)/*add z 1 and z2 matrix */
double z 1[22][22],z2[22][22],item[22][22];
int row,col
{
int i,j;
for(I = 0; I< platoon; i++)
for(j = 0; j & ltcolj++)
item[I][j]= z 1[I][j]+z2[I][j];
}/* End of adding */
Minusmatrix(z 1, z2, row, col, item)/*minus z 1 and z2 matrix*/
double z 1[22][22],z2[22][22],item[22][22];
int row,col
{
int i,j;
for(I = 0; I< platoon; i++)
for(j = 0; j & ltcolj++)
item[I][j]= z 1[I][j]-z2[I][j];
}/* End of adding */
Multiply(z 1, z2, row, col 1, col2, item)/* Multiply the matrix z 1[row][col 1] and z2[col 1][col2]*/
double z 1[22][22],z2[22][22],item[22][22];
int row,col 1,col2
{
int i,j,k;
Double sum;
sum = 0;
cleanmatrix(item,row,col 2);
for(I = 0; I< platoon; i++)
{
for(j = 0; j & ltcol2j++)
{
for(k = 0; k & ltcol 1; k++)
{
sum = sum+z 1[I][k]* z2[k][j];
}
item[I][j]= sum;
sum = 0;
}
}
}/* Multiplication ends */
/* This function is used to calculate the inverse matrix of m[][]*/
/* Method is to divide the original matrix into upper and lower matrices */
/* Slave theorem: A = LU-& gt;; Inver(A)=inver(U)inver(L)*/
/* Therefore, according to the theorem, the matrix A must be a square. (n = c)*/
Inverse (z, nn, cc, item)
Double z[22][22], item [22] [22];
int nn,cc;
{
int i,j,k;
Double l[22][22], u [22] [22];
Double sum;
Double il[22][22], IU [22] [22]; /*il[][] and iu[][] are the inverse matrices of l[][] and u[][]*/
for(I = 0; I & ltnn;; I++)/* Set the elements of u[][] and l[][]*/
for(j = 0; j & ltcc; j++)
{
if(j & lt; I)/* Lower triangle element = 0*/
u[I][j]= 0;
other
{
u[I][j]= z[I][j];
for(k = 0; K< me; k++)
u[I][j]= u[I][j]-l[I][k]* u[k][j];
}
if(j & lt; I)
l[j][I]= 0; /* Upper triangular element = 0*/
Else if(j = = I)/* diagonal element = 1*/
l[j][I]= 1;
other
{
l[j][I]= z[j][I];
for(k = 0; K< me; k++)
l[j][I]= l[j][I]-l[j][k]* u[k][I];
l[j][I]= l[j][I]/u[I][I];
}
}
/* Next, calculate the inverse of u[][] and l[][]*/
for(I = 0; I & ltnn;; i++)
for(j = 0; j & ltcc; j++)
{
il[I][j]= 0;
iu[I][j]= 0;
}
for(I = 0; I & ltnn;; I++)/* Set diagonal elements of the inverse matrix */
{
il[I][I]= 1;
if(u[i][i]! = 0)
iu[I][I]= 1/u[I][I];
other
iu[I][I]= 0;
}
for(I = 0; I & LTCC-1; I++)/* Set the remaining elements of the L matrix */
{
for(j = I+ 1; j & ltnn; j++)
{
il[j][I]=-l[j][I];
for(k = I+ 1; k & ltj; k++)
{
il[j][I]= il[j][I]-l[j][k]* il[k][I];
}
}
}/*L matrix end setting */
for(I = 0; I & ltnn;; I++)/* Set the remaining elements of the U matrix */
{
for(j = I+ 1; j & ltcc; j++)
{
iu[I][j]= 0;
for(k = I; k & ltj; k++)
{
iu[I][j]= iu[I][j]-iu[I][k]* u[k][j];
}
if(u[j][j]! = 0)
iu[I][j]= iu[I][j]/u[j][j];
}
}/*U matrix end setting */
/* Next calculate the inverse of matrix A, where A=LU, a (-1) = u (-1) l (-1) */
sum = 0;
for(I = 0; I & ltnn;; i++)
{
for(j = 0; j & ltcc; j++)
{
for(k = 0; k & ltnn; k++)
{
sum = sum+iu[I][k]* il[k][j];
}
item[I][j]= sum;
sum = 0;
}
}
/*=======================================================================================*/
/* showmatrix(u,nn,cc);
showmatrix(iu,nn,cc);
showmatrix(l,nn,cc);
showmatrix(il,nn,cc);
showmatrix(item,nn,cc); */
/*=======================================================================================*/
The end of }/* inverse()*/
/* Determination of calculation matrix of this function */
determ(z,nn,cc,class_num)
Double z [22] [22];
int nn,cc,class _ num
{
int i,j,k;
Double l[22][22], u [22] [22];
double item = 0;
for(I = 0; I & ltnn;; I++)/* Set the elements of u[][] and l[][]*/
for(j = 0; j & ltcc; j++)
{
if(j & lt; I)/* Lower triangle element = 0*/
u[I][j]= 0;
other
{
u[I][j]= z[I][j];
for(k = 0; K< me; k++)
u[I][j]= u[I][j]-l[I][k]* u[k][j];
}
if(j & lt; I)
l[j][I]= 0; /* Upper triangular element = 0*/
Else if(j = = I)/* diagonal element = 1*/
l[j][I]= 1;
other
{
l[j][I]= z[j][I];
for(k = 0; K< me; k++)
l[j][I]= l[j][I]-l[j][k]* u[k][I];
l[j][I]= l[j][I]/u[I][I];
}
}
item = u[0][0];
for(k = 1; k & ltnn; k++)
item = item+item * u[k][k];
printf("( %lf)",item);
if(item & lt; 0.0)
item =-item;
Det[class_num] = project;
//return (item);
}
- Related articles
- Which province and city does Kunshan belong to?
- What about Dongguan Hongfeng Precision Mould Co., Ltd.?
- Which is better, Yantai Chinese and English or Yantai Tsinghua?
- When is the registration time for the 2010 Shaanxi Yan'an Accounting Qualification Examination?
- Alibaba flying pig interview difficulty
- Requirements for recruiting academic qualifications in Liaoning institutions
- Shenzhen Yongchangxin Human Resources Co., Ltd. is trustworthy.
- How to set up an mcn company
- Did you go to Weifang Bank for an interview last year? What is the written test? What is the interview process? Kneel for advice
- What about Jinan Jidong Color Art Printing Co., Ltd.?