//测地腐蚀 void Erode_Gray_g(double *src,double *ground,double *dst,int width,int height,double *se,int sewidth,int seheight,Position *center){ int SEissmooth=isSmooth(se,sewidth,seheight); double *temp=(double*)malloc(sizeof(double)*width*height); double *temp_last=(double*)malloc(sizeof(double)*width*height); Position centerde; centerde.x=sewidth/2; centerde.y=seheight/2; if(center==NULL){ center=¢erde; } matrixCopy(src,temp_last,width,height); for(int j=0;j<seheight;j++) for(int i=0;i<sewidth;i++){ matrixCopy(src,temp,width,height); double value=se[j*sewidth+i]; if(value!=0.0){ Position d; d.x=i-center->x; d.y=j-center->y; if(SEissmooth) G_Translation(temp, temp,width,height, 0.0, &d,TOFINDMIN); else G_Translation(temp, temp,width,height, -1.0*value, &d,TOFINDMIN); MinPix(temp, temp_last, temp_last,width,height); } } MaxPix(temp_last,ground,temp_last,width,height); matrixCopy(temp_last,dst,width,height); free(temp); free(temp_last); }
//细化操作 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 GeneralMatrix::initialize(rBlockSparseMatrix &M, bool sparse, bool inverse) { CheckVectorRange(M.nBlock); nBlock = M.nBlock; if (blockStruct) delete[] blockStruct; blockStruct = new int[nBlock]; CheckMemory(blockStruct); for (int l=0; l<nBlock; ++l) blockStruct[l] = abs(M.blockStruct[l]); if(sparse) { isSparse = true; smat.resize(nBlock); mat.clear(); for (int l=0; l<nBlock; ++l) { matrixCopy(smat[l], M.ele[l], inverse); } } else { isSparse = false; mat.resize(nBlock); smat.clear(); for (int l=0; l<nBlock; ++l) matrixCopy(mat[l], M.ele[l], inverse); } }
//灰度图像膨胀 void Dilate_Gray(double *src,double *dst,int width,int height,double *se,int sewidth,int seheight,Position *center){ int SEissmooth=isSmooth(se,sewidth,seheight); double *temp=(double*)(malloc(sizeof(double)*width*height)); double *temp_last=(double*)(malloc(sizeof(double)*width*height)); Position centerde; centerde.x=sewidth/2; centerde.y=seheight/2; if(center==NULL){ center=¢erde; } matrixCopy(src,temp_last,width,height); for(int j=0;j<seheight;j++) for(int i=0;i<sewidth;i++){ matrixCopy(src,temp,width,height); double value=se[j*width+i]; if(value!=0.0){ Position d; d.x=center->x-i; d.y=center->y-j; if(SEissmooth) G_Translation(temp, temp,width,height, 0.0, &d,TOFINDMAX); else G_Translation(temp, temp,width,height, value, &d,TOFINDMAX); MaxPix(temp, temp_last, temp_last,width,height); } } matrixCopy(temp_last, dst,width,height); free(temp); free(temp_last); }
//重建开操作 void Rebuild_Open(double *src,double *dst,double *ground,int width,int height,double *erodeSE,int esewidth,int eseheight,double *dilateSE,int dsewidth,int dseheight,int eroden){ double *temp=(double*)malloc(sizeof(double)*width*height); double *temp_last=(double*)malloc(sizeof(double)*width*height); matrixCopy(src,temp,width,height); for(int i=0;i<eroden;i++){ Erode_Gray(temp, temp, width,height,erodeSE,esewidth,eseheight,NULL); } while(!isEqu(temp, temp_last,width,height)){ matrixCopy(temp,temp_last,width,height); Dilate_Gray_g(temp, ground, temp,width,height, dilateSE,dsewidth,dseheight, NULL); } matrixCopy(temp_last,dst,width,height); free(temp); free(temp_last); }
//连通分量获取 void GetConCompG_Onent(double *src,double *dst,int width,int height,Position *seed){ double *temp=(double *)malloc(sizeof(double)*width*height); Zero(temp, width, height); double *lasttemp=(double*)malloc(sizeof(double)*width*height); temp[seed->y*width+seed->x]=255.0; double se[9]={255.,255.,255.,255.,255.,255.,255.,255.,255.}; while(!matrixisEqu(lasttemp, temp, width, height)){ matrixCopy(temp, lasttemp, width, height); Dilate(temp, width, height, temp, width, height, se, 3, 3, NULL); And(temp, src, temp, width, height); } matrixCopy(temp, dst, width, height); free(temp); free(lasttemp); }
// Not working :( bool MazeSequential::willSurvive(int cellnum) { Matrix tempmat; char *dest; bool ret; // Get a copy of the game matrix matrixCopy(&tempmat, m_matrix); // Add blue on that position tempmat.data[cellnum] = 1; // Allocate destination array dest = new char[tempmat.width * tempmat.height]; // Perform a GoL iteration on this temporary data updateState(&tempmat, dest, 0, (tempmat.width * tempmat.height)-1); // Check if blue cell is alive ret = (dest[cellnum] == 1); // Delete temp stuff delete [] tempmat.data; delete dest; return ret; }
//腐蚀 void Erode(double *src,int s_width,int s_height,double *dst,int d_width,int d_height,double *se,int se_width,int se_height,Position *center){ if(center==NULL){ Position temp; temp.x=se_width/2; temp.y=se_height/2; center=&temp; } MoveDirection m; double *temp=(double *)malloc(sizeof(double)*d_width*d_height); double *tempdst=(double *)malloc(sizeof(double)*d_width*d_height); double *realdst=(double *)malloc(sizeof(double)*d_width*d_height); Zero(realdst, d_width, d_height); Zoom(src,s_width,s_height,temp,d_width,d_height); for(int i=0;i<se_width;i++){ for(int j=0;j<se_height;j++){ if(se[j*se_width+i]>100.0){ m.x=center->x-i; m.y=center->y-j; Translation(temp,tempdst,d_width,d_height, &m); And(tempdst, realdst, realdst,d_width,d_height); } } } matrixCopy(realdst, dst, d_width, d_height); free(temp); free(realdst); free(tempdst); }
void matrixRotate(GLfloat M[4*4], GLfloat angle_degrees, GLfloat x, GLfloat y, GLfloat z) { const GLfloat p = 1/sqrtf(x*x + y*y + z*z); x *= p; y *= p; z *= p; const float angle = angle_degrees * (M_PI/180); const float c = cosf(angle); const float s = sinf(angle); const float c_ = 1 - c; const float zc_ = z*c_; const float yc_ = y*c_; const float xzc_ = x*zc_; const float xyc_ = x*y*c_; const float yzc_ = y*zc_; const float xs = x*s; const float ys = y*s; const float zs = z*s; GLfloat S[4*4]; matrixSet3x3(S, x*x*c_ + c, xyc_ - zs, xzc_ + ys, xyc_ + zs, y*yc_ + c, yzc_ - xs, xzc_ - ys, yzc_ + xs, z*zc_ + c); GLfloat T[4*4]; matrixMultiply(T,M,S); matrixCopy(M, T); }
//开操作 void Open_Gray(double *src,double *dst,int width,int height,double *se,int sewidth,int seheight,Position *center){ double *temp=(double*)malloc(sizeof(double)*width*height); Erode_Gray(src, temp,width,height, se,sewidth,seheight, center); Dilate_Gray(temp, temp, width,height,se, sewidth,seheight,center); matrixCopy(temp,dst,width,height); free(temp); }
void convexSpaceCreate(struct convexSpace **pSpace, struct convexFig *fig) { struct convexFigList *vertices=0; create(pSpace); convexFigGetLayer(fig, 0, convexFigMarkIdTrue, convexFigMarkIdTrue, &vertices); convexSpaceCenterPos(*pSpace, vertices); (*pSpace)->dim=0; while (vertices && ((*pSpace)->dim<dim)) { matrixCopy( convexFigListRm(&vertices)->space->pos, (*pSpace)->ortBasis+(*pSpace)->dim*dim, dim); matrixAddScaled( (*pSpace)->ortBasis+(*pSpace)->dim*dim, -1, (*pSpace)->pos, dim); matrixOrtVectorToBasis( (*pSpace)->ortBasis, (*pSpace)->ortBasis+(*pSpace)->dim*dim, (*pSpace)->dim, dim); if (figureDistCmpZeroSq(matrixVectorNormSq((*pSpace)->ortBasis+(*pSpace)->dim*dim, dim))!=0) { matrixVectorNormalize((*pSpace)->ortBasis+(*pSpace)->dim*dim, dim); (*pSpace)->dim++; } } }
//////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// //3x3的邻域内,如果中心像素大于阈值,且周围存在负像素值,则此点为0交叉点 void findCross(double *src,double *dst,int width,int height,double threshold){ double *dsttemp=(double *)malloc(sizeof(double)*width*height); Zero(dst, width, height); double c_value=0.0; int flag=1; Zero(dsttemp, width, height); for(int j=1;j<height-1;j++){ for(int i=1;i<width-1;i++){ c_value=src[j*width+i]; flag=1; for(int m=-1;m<=1&&flag;m++) for(int n=-1;n<=1;n++){ if(c_value>=threshold&&src[(j+m)*width+i+n]<-0.000001){ flag=0; dsttemp[j*width+i]=255.0; break; } } } } matrixCopy(dsttemp, dst,width, height); free(dsttemp); }
//骨架 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); }
void matrixTranslate(GLfloat M[4*4], GLfloat dx, GLfloat dy, GLfloat dz) { GLfloat S[4*4]; matrixSet3x4(S, 1, 0, 0, dx, 0, 1, 0, dy, 0, 0, 1, dz); GLfloat T[4*4]; matrixMultiply(T,M,S); matrixCopy(M, T); }
void computeXpi(GzRender* render){ GzDisplay* display = render -> display; GzCamera& cam = render -> camera; float d; //1/d d = 1/(tan((cam.FOV/2)*(PI/180))); GzMatrix Xpi = {1,0,0,0,0,1,0,0,0,0,1,0,0,0,1/d,1}; matrixCopy(Xpi, &(render->camera.Xpi),4,4); //printMatrix(Xpi); }
void matrixScale(GLfloat M[4*4], GLfloat sx, GLfloat sy, GLfloat sz) { GLfloat S[4*4]; matrixSet3x3(S, sx, 0, 0, 0, sy, 0, 0, 0, sz); GLfloat T[4*4]; matrixMultiply(T,M,S); matrixCopy(M, T); }
void computeXiw(GzRender* render){ //step 1: compute camera Z-axis GzCoord z_axis; GzDisplay* display = render -> display; GzCamera& cam = render->camera; z_axis[0] = cam.lookat[0] - cam.position[0]; z_axis[1] = cam.lookat[1] - cam.position[1]; z_axis[2] = cam.lookat[2] - cam.position[2]; //normalize z_axis /* float z_len = sqrt(z_axis[0]*z_axis[0]+z_axis[1]*z_axis[1]+z_axis[2]*z_axis[2]); z_axis[0] /= z_len; z_axis[1] /= z_len; z_axis[2] /= z_len; */ normalize(&z_axis); //step 2: construct up': up' = up - (up(dot-product)Z)Z //up(dot-product)Z float dotPro = dotProFun(cam.worldup, z_axis);// = cam.worldup[0]*z_axis[0]+cam.worldup[1]*z_axis[1]+cam.worldup[2]*z_axis[2]; //up' GzCoord up_; up_[0] = cam.worldup[0] - dotPro*z_axis[0]; up_[1] = cam.worldup[1] - dotPro*z_axis[1]; up_[2] = cam.worldup[2] - dotPro*z_axis[2]; //normalize(&up_); //step3: construct X : X = (Y*Z) = (Yy*Zz - Yz Zy)i + (Yz*Zx - Yx*Zz)j + (YxZy - Yy*Zx) GzCoord _x; _x[0] = up_[1]*z_axis[2] - up_[2]*z_axis[1]; _x[1] = up_[2]*z_axis[0] - up_[0]*z_axis[2]; _x[2] = up_[0]*z_axis[1] - up_[1]*z_axis[0]; //normalize x, y normalize(&_x); normalize(&up_); //construct Xiw float xC, yC, zC; xC = dotProFun(_x,cam.position); yC = dotProFun(up_,cam.position); zC = dotProFun(z_axis,cam.position); GzMatrix Xiw = {_x[0], _x[1], _x[2], -xC, up_[0], up_[1], up_[2], -yC, z_axis[0], z_axis[1], z_axis[2], -zC, 0, 0, 0, 1}; matrixCopy(Xiw, &(render->camera.Xiw),4,4); //printMatrix(Xiw); }
void Translation(double *src,double *dst,int width,int height,MoveDirection *direction){ double *temp=(double *)malloc(sizeof(double)*width*height); Zero(temp, width, height); for(int j=0;j<height;j++) for(int i=0;i<width;i++){ int new_x=i+direction->x; int new_y=j+direction->y; if(new_y<height &&new_x<width&&new_y>=0&&new_x>=0) temp[new_y*width+new_x]=src[j*width+i]; } matrixCopy(temp, dst, width, height); free(temp); }
void matrixOrtho(GLfloat M[4*4], GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat hither, GLfloat yon) { GLfloat S[4*4]; matrixSet3x4(S, 2/(right - left), 0, 0, -(right + left)/(right - left), 0, 2/(top - bottom), 0, -(top + bottom)/(top - bottom), 0, 0, -2/(yon-hither), -(yon + hither)/(yon - hither)); GLfloat T[4*4]; matrixMultiply(T,M,S); matrixCopy(M, T); }
//凸壳 void Convexhull(double *src,double *dst,int width,int height){ double * temp_dst=(double *)malloc(sizeof(double)*width*height); matrixCopy(src, temp_dst, width, height); double * se[4]; double * temp=(double *)malloc(sizeof(double)*width*height); double * temp_last=(double *)malloc(sizeof(double)*width*height); matrixCopy(src, temp, width,height); for(int i=0;i<4;i++){ se[i]=CreateConvexhullSE(i); while (!matrixisEqu(temp, temp_last, width, height)) { matrixCopy(temp, temp_last, width, height); Erode(temp, width,height, temp, width, height, se[i], 3, 3, NULL); Or(temp, temp_dst, temp, width, height); } matrixCopy(temp, temp_dst, width, height); free(se[i]); } matrixCopy(temp_dst, dst, width, height); free(temp); free(temp_last); }
//孔洞填充 void FillHole(double *src,double *dst,int width,int height,Position *seed){ double *temp=(double *)malloc(sizeof(double)*width*height); Zero(temp, width, height); double *lasttemp=(double *)malloc(sizeof(double)*width*height); double *nsrc=(double *)malloc(sizeof(double)*width*height); Not(src, nsrc, width, height); temp[seed->y*width+seed->x]=255.; double se[9]={255.,255.,255.,255.,255.,255.,255.,255.,255.}; while(!matrixisEqu(lasttemp, temp, width, height)){ matrixCopy(temp, lasttemp, width, height); Dilate(temp, width, height, temp, width, height, se, 3, 3, NULL); And(temp, nsrc, temp, width, height); } Or(temp, src, dst, width, height); free(temp); free(lasttemp); free(nsrc); }
void convexSpaceExpand(struct convexSpace *space, struct convexSpace *vert) { matrixCopy( vert->pos, space->ortBasis+space->dim*dim, dim); matrixAddScaled( space->ortBasis+space->dim*dim, -1, space->pos, dim); matrixOrtVectorToBasis( space->ortBasis, space->ortBasis+space->dim*dim, space->dim, dim); matrixVectorNormalize( space->ortBasis+space->dim*dim, dim); space->dim++; }
bool convexSpaceContains(struct convexSpace *space1, struct convexSpace *space2) { int i; if (space1->dim<space2->dim) return 0; if (figureDistCmpZeroSq(convexSpaceDistSq(space1, space2))!=0) return 0; for (i=0; i<space2->dim; i++) { matrixCopy( space2->ortBasis+i*dim, tempVect, dim); matrixOrtVectorToBasis( space1->ortBasis, tempVect, space1->dim, dim); if (figureDistCmpZeroSq(matrixVectorNormSq(tempVect, dim))!=0) return 0; } return 1; }
//位移,如果非平滑SE将加上sevalue,即对应的灰度值 void G_Translation(double *src,double *dst,int width,int height,double SEvalue,Position *d,int istoFindMin){ double *temp=(double*)malloc(sizeof(double)*height*width); if(istoFindMin) One(temp,width,height); else Zero(temp,width,height); for(int j=0;j<height;j++){ for(int i=0;i<width;i++){ int target_x=i+d->x; int target_y=j+d->y; if(target_x>=0&&target_y>=0&& target_x<width&&target_y<height){ double value=src[j*width+i]+SEvalue; value=(value>=255.0?255.0:value); temp[target_y*width+target_x]=value; } } } matrixCopy(temp,dst,width,height); free(&temp); }
void TrackRegulator(float *Coord_cur, float *SpeedCur, Path *cur, float *V) { float velocity[3] = {0.0, 0.0, 0.0}; float cosAlphZad = cosf(cur->alphZad), sinAlphZad = sinf(cur->alphZad); float t_alph_zad[2][2]; float buf1[3]; float velFromEt[2]; //float tracer; //float Coord_local_track[3] = {0.0, 0.0, 0.0}; matrixPlusMinus(Coord_cur,cur->coordCenter, 3, 1, -1, &buf1[0]); //(CURCOORD-CUR_CENTER) = buf1 t_alph_zad[0][0] = cosAlphZad; t_alph_zad[0][1] = sinAlphZad; t_alph_zad[1][0] = -sinAlphZad; t_alph_zad[1][1] = cosAlphZad; matrixMultiplyM2M(&t_alph_zad[0][0], 2, 2, SpeedCur, 2, 1, &(cur->Speed_local_track[0])); matrixMultiplyM2M(&t_alph_zad[0][0], 2, 2, &buf1[0], 2, 1, &(cur->Coord_local_track[0]));// cur->Coord_local_track[2] = (robotCoord[2]-cur->coordCenter[2])*cur->phiDir; if (cur->Coord_local_track[2]<0) cur->Coord_local_track[2]+=2.0*PI; //if(Coord_local_track[0]<0) // tracer=fabs(Coord_local_track[0]); //else // tracer=Coord_local_track[0]; Moving(cur->Coord_local_track[0], (cur->lengthTrace), (cur->traceVel), &velFromEt[0]); RotMoving((cur->coordCenter[2]), robotCoord[2], (cur->phiZad), (cur->omegaVel), &velFromEt[1]); //velFromEt[1]= velFromEt[1]*cur->phiDir; //LOCALCOORD(3,1)=(phiZad-CURCOORD(3,1)); //VELOCITY=regulate(LOCALCOORD,vEt); Regulate(&(cur->Coord_local_track[0]), &(cur->Speed_local_track[0]), &t_alph_zad[0][0], &velFromEt[0], &(cur->alphZad), &velocity[0]); matrixCopy(&velocity[0], 3, 1, &V[0]); }////////////////////////////////////////////////////////////////////////////////
void convexSpaceNormalCalc(struct convexSpace *space, struct convexSpace *inSpace) { int i; for (i=inSpace->dim-1; i>=0; i--) { matrixCopy( inSpace->ortBasis+i*dim, space->normal, dim); matrixOrtVectorToBasis( space->ortBasis, space->normal, space->dim, dim); if (figureDistCmpZeroSq(matrixVectorNormSq(space->normal, dim))!=0) { matrixVectorNormalize(space->normal, dim); break; } } space->normalPos=0; space->normalPos+=convexSpaceOrientedDist(space, space); if (convexSpaceOrientedDist(space, inSpace)>0) { matrixScale(space->normal, -1, dim); space->normalPos*=-1; } }
//////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// //生成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); }
/* * 四个角度对应编号 * 0 1 2 * 3 * 5 * 6 7 8 * */ void Non_MaxSuppression(double *src,double *dst,double *dirction,int width,int height){ double *temp=(double*)malloc(sizeof(double)*width*height); int dir; int y; int x; double value_c; Zero(temp, width, height); for(int j=1;j<height-1;j++) for(int i=1;i<width-1;i++){ if(dirction[j*width+i]!=-1.0){ dir=(int)dirction[j*width+i]; y=dir/3-1; x=dir%3-1; value_c=src[j*width+i]; if(value_c<=src[(j+y)*width+i+x]||value_c<src[(j-y)*width+i-x]) temp[j*width+i]=0.0; else temp[j*width+i]=value_c; } } matrixCopy(temp, dst, width, height); free(temp); }
//////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// //Kirsch算子 double Kirsch(double *src,double *dst,int width,int height){ double KirschMask[8][9]={{-3,-3,-3, -3, 0, 5, -3, 5, 5}, {-3,-3,-3, -3, 0,-3, 5, 5, 5}, {-3,-3,-3, 5, 0,-3, 5, 5,-3}, {5,-3,-3, 5, 0,-3, 5,-3,-3}, {5, 5,-3, 5, 0,-3, -3,-3,-3}, {5, 5, 5, -3, 0,-3, -3,-3,-3}, {-3, 5, 5, -3, 0, 5, -3,-3,-3}, {-3,-3, 5, -3, 0, 5, -3,-3, 5}}; double *maxtemp=(double *)malloc(sizeof(double)*width*height); double *temp=(double*)malloc(sizeof(double)*width*height); Zero(maxtemp, width, height); for(int i=0;i<8;i++){ RealRelevant(src, temp, KirschMask[i], width, height, 3, 3); MaxPix(temp, maxtemp, maxtemp, width, height); } matrixCopy(maxtemp, dst, width, height); return findMatrixMax(dst,width,height,NULL); }
// // M <-- M * A // void matrixCat(GLfloat M[4*4], const GLfloat A[4*4]) { GLfloat T[4*4]; matrixMultiply(T,M,A); matrixCopy(M, T); }