Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Meshgrid of Matlab to OpenCV

Hi,

translating this code from Matlab to C. Code of Matlab:

x = (-cols/2 : (cols/2 - 1))/(cols/2);
y = -(-rows/2 : (rows/2 - 1))/(rows/2);
[x,y] = meshgrid(x,y);

What I did in C/C++ is :

double x[cols],y[rows];
double X[cols][rows], Y[cols][rows];
double epsilon=0.0001;

for(int j=0;j<cols;j++){
 for(int i=-(cols/2);i<=(cols/2)-1;i++){
  x[j]= i/(cols/2);
 }
}
for(int k=0;k<rows;k++){
  for(int z=-(rows/2);z<=(rows/2)-1;z++){
        y[k]= -z/(rows/2);
  }
}

for(int i=0;i<cols;i++){
  for(int z=0;z<rows;z++){
      X[i][z]=x[i];
      Y[i][z]=y[z];
  }
}

Meshgrid of Matlab to OpenCV

Hi,

translating this code from Matlab to C. Code of Matlab:

x = (-cols/2 : (cols/2 - 1))/(cols/2);
y = -(-rows/2 : (rows/2 - 1))/(rows/2);
[x,y] = meshgrid(x,y);

What I did in C/C++ is :

double x[cols],y[rows];
double X[cols][rows], Y[cols][rows];
double epsilon=0.0001;

for(int j=0;j<cols;j++){
 for(int i=-(cols/2);i<=(cols/2)-1;i++){
  x[j]= i/(cols/2);
 }
}
for(int k=0;k<rows;k++){
  for(int z=-(rows/2);z<=(rows/2)-1;z++){
        y[k]= -z/(rows/2);
  }
}

for(int i=0;i<cols;i++){
  for(int z=0;z<rows;z++){
      X[i][z]=x[i];
      Y[i][z]=y[z];
  }
}

Is it right?