C++ 如何将图像转换为圆柱形?
我使用的是objective c语言 我想把我的图像转换成圆柱形。这里我使用下面的cpp文件代码来转换图像C++ 如何将图像转换为圆柱形?,c++,opencv,objective-c++,opencv-stitching,cylindrical,C++,Opencv,Objective C++,Opencv Stitching,Cylindrical,我使用的是objective 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::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;
}
cv::垫圆柱形抛光砖2(垫img)
{
cv::Mat destImgMat(img.size(),cv_8U);
对于(int y=0;yimg.cols-2 | |左上角.y<0 |
左上角y>img.rows-2)
{
继续;
}
//双线性插值
浮动dx=当前位置x-顶部位置x;
浮动dy=当前位置y-顶部左y;
浮子重量_tl=(1.0-dx)*(1.0-dy);
浮子重量_tr=(dx)*(1.0-dy);
浮子重量_bl=(1.0-dx)*(dy);
浮子重量br=(dx)*(dy);
uchar值=重量×左侧顶部的img+
重量(左上角y,左上角x+1)+
重量(左上角y+1,左上角x)+
重量(左上角y+1,左上角x+1);
(y,x)处的目标材料=值;
}
}
返回到mgmat;
}
cv::Point2f转换_pt1dd(cv::Point2f点,整数w,整数h)
{
cv::点2F pc(点x-w/2,点y-h/2);
浮点数f=w;
浮点数r=w;
浮动ω=w/2;
浮点数z0=f-平方米(r*r-omega*omega);
浮子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::点2F最终点(pc.x*zc/f,pc.y*zc/f);
最终点x+=w/2;
终点y+=h/2;
返回终点;
}
通过这个代码,我得到了圆柱形,但我的图像被截短了。没有得到完整的圆柱投影图像,我的图像如下所示
我想以圆柱形显示我的完整图像。
如果提供了一些资源或帮助,非常感谢
提前感谢您的输入图像是否为3通道/RGB?因为您的目标映像只有一个通道(您将其创建为CV_8U,它只有一个通道),所以可能会重复