//细化操作 void Thinning(double *src,double *dst,int width,int height){ double *temp=(double *)malloc(sizeof(double)*width*height); double *temp_last=(double *)malloc(sizeof(double)*width*height); double *temp_com=(double *)malloc(sizeof(double)*width*height); matrixCopy(src, temp, width, height); while(!matrixisEqu(temp, temp_com,width,height)){ matrixCopy(temp, temp_com, width, height); for(int i=0;i<8;i++){ matrixCopy(temp, temp_last, width, height); double *se1=CreateThinningSE(i); double *se2=CreateThinningUSE(i); HitorMiss(temp, width, height, se1, 3, 3, se2, 3, 3, temp, NULL, NULL); matrixSub(temp_last, temp, temp, width, height); free(se1); free(se2); } } matrixCopy(temp, dst, width, height); free(temp); free(temp_com); free(temp_last); }
//二值图像,边缘检测 void BinaryEdge(double *src,int width,int height,double* dst){ double *temp=(double *)malloc(sizeof(double)*width*height); double se[9]={255.,255.,255.,255.,255.,255.,255.,255.,255.}; Erode(src, width, height, temp, width, height,se ,3, 3, NULL); matrixSub(src, temp, dst, width, height); free(temp); }
//灰度梯度形态学提取 void Gard_Gray(double *src,double *dst,int width,int height,double *se,int sewidth,int seheight,Position *center){ double *temp_dilate=(double *)malloc(sizeof(double)*width*height); double *temp_erode=(double *)malloc(sizeof(double)*width*height); Dilate_Gray(src, temp_dilate,width,height,se,sewidth,seheight, center); Erode_Gray(src, temp_erode,width,height,se,sewidth,seheight, center); matrixSub(temp_dilate,temp_erode,dst,width,height); free(temp_dilate); free(temp_erode); }
//骨架 void FrameWork(double *src,double *dst,int width,int height,double *se,int se_width,int se_height){ double *temp_dst=(double*)malloc(sizeof(double)*width*height); Zero(temp_dst, width, height); double *temp=(double *)malloc(sizeof(double)*width*height); double *temp_open=(double *)malloc(sizeof(double)*width*height); matrixCopy(src, temp, width, height); while(!matrixisEmpty(temp,width,height)){ Erode(temp, width, height, temp, width, height, se, se_width, se_height, NULL); matrixCopy(temp, temp_open, width, height); Open(temp_open, width, height, temp_open, se, se_width, se_height, NULL); matrixSub(temp, temp_open,temp_open, width, height); Or(temp_open, temp_dst, temp_dst, width, height); } matrixCopy(temp_dst, dst, width, height); free(temp); free(temp_open); free(temp_dst); }
//////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// //生成DoG模板 //使用两个高斯模板做差 void getDoGMask(double *mask,int width,int height,double delta){ double *mask1=(double *)malloc(sizeof(double)*width*height); double *mask2=(double *)malloc(sizeof(double)*width*height); GaussianMask(mask1, width, height, delta); GaussianMask(mask2, width, height, delta*1.6); matrixMultreal(mask1, mask1, delta*2*M_PI, width, height); matrixMultreal(mask2, mask2, delta*3.2*M_PI, width, height); matrixSub( mask2,mask1, mask1, width, height); double sum=0.0; for(int i=0;i<width*height;i++){ sum+=mask1[i]; } double di=sum/(double)(width*height); for(int i=0;i<width*height;i++){ mask1[i]-=di; } matrixCopy(mask1, mask, width, height); //matrixMultreal(mask, mask, 10, width, height); free(mask1); free(mask2); }
void main(void) { char menuChoice; header(); do { matrixMaker(1); matrixMaker(2); menuChoice = menu(); do { switch (menuChoice) { case 'A': //addition matrixAdder(); break; case 'S'://subtraction matrixSub(); break; case 'M'://matrix multiplication matrixMultiply(); break; case 'E'://Element by element multiplication matrixElemMultiply(); break; case 'N': // move to next case or exit(?) //what do we put here? Instructions unclear. break; } display(menuChoice); fileWriter(menuChoice); // option loop operation code printf("\n\nWould you like to run a different operation with the same sets of data? (Y/N) "); scanf(" %c", &loopOperations); loopOperations = toupper(loopOperations); while(loopOperations != 'Y' && loopOperations != 'N') { printf("Input must be either Y or N!\n"); printf("\n\nWould you like to run a different operation with the same sets of data? (Y/N) "); scanf(" %c", &loopOperations); loopOperations = toupper(loopOperations); } } while(loopOperations == 'Y'); // option loop code with new data printf("\n\nWould you like to use new files for a different data set? (Y/N) "); scanf(" %c", &loopProgram); loopProgram = toupper(loopProgram); while(loopProgram != 'Y' && loopProgram != 'N') { printf("Input must be either Y or N!\n"); printf("\n\nWould you like to use new files for a different data set? (Y/N) "); scanf(" %c", &loopProgram); loopProgram = toupper(loopProgram); } } while(loopProgram == 'Y'); }
void strassen(Matrices *m){ int seuil = 0; int *a11,*a12,*a21,*a22; int *b11,*b12,*b21,*b22; a11 = malloc((m->size*m->size*sizeof(int))/4); a12 = malloc((m->size*m->size*sizeof(int))/4); a21 = malloc((m->size*m->size*sizeof(int))/4); a22 = malloc((m->size*m->size*sizeof(int))/4); b11 = malloc((m->size*m->size*sizeof(int))/4); b12 = malloc((m->size*m->size*sizeof(int))/4); b21 = malloc((m->size*m->size*sizeof(int))/4); b22 = malloc((m->size*m->size*sizeof(int))/4); int index1=0; int index2=0; int index3=0; int index4=0; int i; for(i = 0 ; i < m->size*m->size ; i++){ if(i%m->size < m->size/2 ){ if((i/m->size) < m->size/2){ a11[index1]=m->a[i]; b11[index1++]=m->b[i]; }else{ a21[index2]=m->a[i]; b21[index2++]=m->b[i]; } }else{ if((i/m->size) < m->size/2){ a12[index3]=m->a[i]; b12[index3++]=m->b[i]; }else{ a22[index4]=m->a[i]; b22[index4++]=m->b[i]; } } } Matrices temp; temp.size = m->size/2; int* p1; p1 = malloc(m->size*sizeof(int)); temp.result = p1; matrixSum(a11,a22,temp.size,temp.a); matrixSum(b11,b22,temp.size,temp.b); strassen(&temp); int* p2; p2 = malloc(m->size*sizeof(int)); temp.result = p2; matrixSum(a21,a22,temp.size,temp.a); temp.b = b11; strassen(&temp); int* p3; p3 = malloc(m->size*sizeof(int)); temp.result = p3; matrixSub(b12,b22,temp.size,temp.b); temp.a = a11; strassen(&temp); }
//重建顶帽操作 void Rebuild_Tophat(double *src,double *dst,double *ground,int width,int height,double *dilateSE,int dsewidth,int dseheight,double *erodeSE,int esewidth,int eseheight,int eroden){ Rebuild_Open(src,dst,ground,width,height,erodeSE,esewidth,eseheight,dilateSE,dsewidth,dseheight,eroden); matrixSub(src,dst,dst,width,height); }
//底帽操作 void BottomHat(double *src,double *dst,int width,int height,double *se,int sewidth,int seheight,Position *center){ double *temp=(double *)malloc(sizeof(double)*width*height); Close_Gray(src, temp,width,height,se,sewidth,seheight,center); matrixSub(temp,src,dst,width,height); free(temp); }
// QR Factorization void QR(double* A, int m, int n, double* Q, double* R) { // Not a very efficient approach, but simple and effective // Written by Nico van Duijn, 9/13/2015 // Source of algorithm (adjusted by me): // http://www.keithlantz.net/2012/05/qr-decomposition-using-householder-transformations/ // A is the (m*n) matrix to be factorized A=Q*R double mag, alpha; double u[m], v[m], vtrans[m]; double P[m * m], I[m * m], vvtrans[m * m], Rbackup[m * m], Qbackup[m * m]; matrixCopy(A, m, m, R); // Initialize R to A // Initialize Q, P, I to Identity for (int i = 0; i < m * m; i++)Q[i] = 0; for (int i = 0; i < m; i++)Q[i * n + i] = 1; for (int i = 0; i < m * m; i++)P[i] = 0; for (int i = 0; i < m; i++)P[i * n + i] = 1; for (int i = 0; i < m * m; i++)I[i] = 0; for (int i = 0; i < m; i++)I[i * n + i] = 1; for (int i = 0; i < n; i++) //loop through all columns { for (int q = 0; q < m; q++)u[q] = 0; // set u and v to zero for (int q = 0; q < m; q++)v[q] = 0; mag = 0.0; //set mag to zero for (int j = i; j < m; j++) { u[j] = R[j * n + i]; mag += u[j] * u[j]; } mag = sqrt(mag); alpha = u[i] < 0 ? mag : -mag; mag = 0.0; for (int j = i; j < m; j++) { v[j] = j == i ? u[j] + alpha : u[j]; mag += v[j] * v[j]; } mag = sqrt(mag); if (mag < 0.0000000001) continue; for (int j = i; j < m; j++) v[j] /= mag; // P = I - (v * v.transpose()) * 2.0; matrixTranspose(v, m, 1, vtrans); matrixMult(v, vtrans, m, 1, m, vvtrans); matrixScale(vvtrans, m, m, 2.0); matrixSub(I, vvtrans, m, m, P); // R = P * R; matrixMult(P, R, m, m, m, Rbackup); matrixCopy(Rbackup, m, m, R); //Q = Q * P; matrixMult(Q, P, m, m, m, Qbackup); matrixCopy(Qbackup, m, m, Q); } }