如何将图像转换为圆柱形?

时间:2016-04-01 09:32:10

标签: c++ opencv objective-c++ opencv-stitching cylindrical

我使用客观的c语言。

我想将图片转换为圆柱形。这里我使用下面的cpp文件代码来转换图像。

    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;
     }

使用此代码我得到了圆柱形状,但我的图像被缩小了。没有获得完整的圆柱投影图像,我的图像如下所示,

Image link

我想以圆柱形状显示我的完整图像。 如果提供了一些来源或帮助,大大赞赏。

提前致谢

0 个答案:

没有答案