How to convert image to cylindrical shape?

asked 2016-04-01 04:30:04 -0600

Ankit C gravatar image

Hello,

I am using objective c language.

I want to convert my image to cylindrical shape. Here I am using below cpp file code to convert image.

    cv::Mat CylindricalWarper2 (Mat img)
    {
        cv::Mat destImgMat(img.size(), CV_8U);

        for(int y = 0; y < img.rows; y++)
        {
            for(int x = 0; x < img.cols; x++)
            {
                cv::Point2f current_pos(x,y);

                current_pos = convert_pt1dd(current_pos, img.cols, img.rows);

                cv::Point2i top_left((int)current_pos.x,(int)current_pos.y);            

                if(top_left.x < 0 || top_left.x > img.cols-2 || top_left.y < 0 ||   
                top_left.y > img.rows-2)
                {
                   continue;
                }
                //bilinear interpolation
                float dx = current_pos.x-top_left.x;
                float dy = current_pos.y-top_left.y;

                float weight_tl = (1.0 - dx) * (1.0 - dy);
                float weight_tr = (dx)       * (1.0 - dy);
                float weight_bl = (1.0 - dx) * (dy);
                float weight_br = (dx)       * (dy);

                uchar value = weight_tl * img.at<uchar>(top_left) +
                weight_tr * img.at<uchar>(top_left.y,top_left.x+1) +
                weight_bl * img.at<uchar>(top_left.y+1,top_left.x) +
                weight_br * img.at<uchar>(top_left.y+1,top_left.x+1);

                destImgMat.at<uchar>(y,x) = value;
            }
        }
        return destImgMat;
     }


    cv::Point2f convert_pt1dd(cv::Point2f point,int w,int h)
    {
          cv::Point2f pc(point.x-w/2,point.y-h/2);

         float f = w;
         float r = w;

         float omega = w/2;
         float z0 = f - sqrt(r*r-omega*omega);

         float zc = (2*z0+sqrt(4*z0*z0-4*(pc.x*pc.x/(f*f)+1)*(z0*z0-r*r)))/(2*      
         (pc.x*pc.x/(f*f)+1));

         cv::Point2f final_point(pc.x*zc/f,pc.y*zc/f);
         final_point.x += w/2;
         final_point.y += h/2;

         return final_point;
     }

With this code I got the cylindrical shape but my image cut down. Not get the full cylindrical projection image, My image look like below,

image description

I want to display my full image in cylindrical shape. If some source or help provided, greatly Appreciated.

Thanks in advance

edit retag flag offensive close merge delete

Comments

Take a look here as it seems his code is working properly.

StevenPuttemans gravatar imageStevenPuttemans ( 2016-04-01 07:01:44 -0600 )edit

Oh and btw, there is actually a cylindrical warping class, so it might do exactly what you are doing manually and it might be more optimized!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-04-01 07:03:02 -0600 )edit