Search code examples
c++opencvcomputer-visiongpuvectorization

Speeding up opencv image mapping


I have a rectangular input image of size 256x507, which I wish to map to a 1920x1080 image but in form of a sector.

  • y of input is proportional to distance in output image
  • x of input is proportional to angle from centre line in output image

I already have a written a code which does what I want:

cv::Mat sector_image=cv::Mat::zeros(1080, 1920, CV_8UC1);
int cx=888;
int cy=1080-949;
int rx=1687;
int ry=1080-575;
int lx=89;
int ly=1080-575;
float r= distance(cx, cy, lx,ly);
 float pi = 2 * asin(1.0);
float src_angl=130*pi/180;
//std::cout<<"angle(lx,ly,cx,cy,rx,ry);"<<angle(lx,ly,cx,cy,rx,ry)<<std::endl;
//std::cout<<"src_angl"<<src_angl<<std::endl;
for (int y = 0; y < sector_image.rows; y += 1)
{
    for (int x = 0; x < sector_image.cols; x += 1)
    {
         float dist=  distance(cx, cy, x,y);
         int px=0;int py=0;
         px=r*(x-cx)/dist + cx;
         py=r*(y-cy)/dist + cy;
         if ((px>=lx)&&(px<=rx)&&(dist<=r)&&(y>=cy)){
            float angl= angle(lx,ly,cx,cy,x,y);
            angl=angl-65*pi/180;
            //std::cout<<"angl"<<angl<<std::endl;
            int srcy = (int)((dist/r)*my_mat.rows);
            int srcx = (int)(sin(angl)/sin(65*pi/180)*128+127.5);
            if(srcx==-1){
                srcx=0;
            }
            else if(srcx==256){
                srcx=255;
            }
            if((srcx>=0)&&(srcy>=0)&&(srcx<=my_mat.cols)&&(srcy<=my_mat.rows))
           {
                sector_image.at<uchar>(y,x) = my_mat.at<uchar>(srcy,srcx);
                //std::cout<<x<<" "<<y<<" "<<angl<<std::endl;

            }


        }
    }
}

But this code is slow. Is there any way to use vectorized numpy operations to speed it up? Or using GPU would be even better.


Solution

  • I Generated map once at the start, then used cv::remap. Thanks to all the comments