2

I'm using the remap function to map a irregular grid( 650 xyz-coordinates ) to a regular one ( 160x160 points from -5....5 step 1/160) but I can't seem to get it working. By the way the interpolation that I use is bicubic. Can someone please tell me if it's even possible to do it like this? thanks in advance.

using namespace cv;
using namespace POINTS;

std::ofstream file;
Mat src(400,3,CV_32F);
Mat dst(160,160,CV_32F);
Mat map_x;
Mat map_y;
int ind = 0;

Mat matx( 400, 1, CV_32F, &pointsx );
Mat maty( 400, 1, CV_32F, &pointsy );
Mat matz( 400, 1, CV_32F, &pointsz );


void matrixDump( const Mat* mat );
void createMatrix( Mat* mat );

int main()
{

 hconcat( matx, maty, matx );
 hconcat( matx, matz, src );

 map_x.create( 160,160, CV_32FC1 );
 map_y.create( 160, 160, CV_32FC1 );

 createMatrix( &map_x );
 createMatrix( &map_y );
 Mat T = map_y.t();
 remap( src, dst, map_x, T, CV_INTER_CUBIC, BORDER_CONSTANT, Scalar(0, 0, 0) );

 return 0;
}


void matrixDump( const Mat* mat )
{
    file.open("interpolation.txt");

    for( int i=0; i<mat->rows ; i++ )
    {
       for( int j=0; j<mat->cols;j++)
       {
           file << mat->at<float>(i,j) << " " ;
       }
    }
    file.close();
}


void createMatrix( Mat* mat )
{ 
   for( int i=0; i<mat->rows; i++)
   {
       for( int j=0; j<mat->cols; j++ )
       {
           float value = -1. + (j*2./(mat->rows-1));
           mat->at<float>(i,j) = 5. * value;
       }
   }
}
xyfix
  • 61
  • 1
  • 7

1 Answers1

0

map_x.create( 160,160, CV_32FC1 ); is float but when you fill it you use mat.at<double>(i,j) = 5. * value; . I don't know if it solve your problem, but it should be corrected.

Andrey Smorodov
  • 10,649
  • 2
  • 35
  • 42
  • Thanks Andrey, I changed that with a few other mistakes but can you tell if I'm on the right track by using remap for my problem? – xyfix Aug 24 '13 at 17:48
  • You need map every point on source image to point on destination. As I remember remap does assignment: Idst(map_y(i,j),map_x(i,j))=Isrc(i,j); if it that you want, then you in right way. Sometimes, it is more effective to use inverse mapping, from dst to src, i.e. scan dst image point by point and evaluate coordinates in src image from which get the value to set in src image. This method allows you avoid not filled points on dst image. But remap doing interpolation, so you not need worry about it. But anyway you need fill all the elements in map_x and map_y matrices without misses. – Andrey Smorodov Aug 24 '13 at 18:37
  • Andrey, I corrected the code above and it runs now without any crashes, but unfortunately the result doesn't seem to be correct. Every row in map_x(160x160) begins from -5 to 5 ( step is 1/160 ) and every column in map_y(160x160) begins from -5 t0 5( step is 1/160 ). – xyfix Aug 24 '13 at 20:12
  • Ough, I forget that you want use irregular grid! Ok, I think the simpliest way to deal with it is to use barycentric interpolation. All you need is to triangulate your irregular points and then use barycentric interpolation. Take a look at: http://classes.soe.ucsc.edu/cmps160/Fall10/resources/barycentricInterpolation.pdf – Andrey Smorodov Aug 25 '13 at 08:00
  • Andrey, does that mean that remap can't handle an irregular grid as source and map that to a regular grid? I'm actually trying to find a equivalent for the griddata function in Matlab. On the internet I found that this remap function in OpenCV could do that with bicubic interpolation. – xyfix Aug 25 '13 at 12:26
  • Yes, remap can't do translation from irregular grid to regular. You need implement it by yourself. It can do nonlinear sofisticated mappings, but you must set every point in map_x and map_y matrices by yourself remap will not do it for you. You can fill these matrices (map_x and map_y) using some key values and using interpolation on them. Then remap will map src_img to dst_img using bicubic interpolation on images, not on mappings. – Andrey Smorodov Aug 25 '13 at 15:40
  • Were you able to figure out how to interpolate an irregular grid? – Utkarsh Sinha Jan 03 '16 at 08:21