未弯曲的曲面

时间:2014-04-23 09:26:05

标签: opencv distortion unwarp

我有一个圆柱形物体,上面贴有圆形标签。我提取图像的轮廓。enter image description here

我知道圆柱形物体的半径以及标签。但并非每次获得的椭圆都是完全对称的。如何将椭圆展开成圆形?

这是一张不对称enter image description here

的图像

EDITED 我试图扩展@ Haris的解决方案,如enter image description here

而不仅仅是4分,我想使用一系列点来获得更准确的圆。但getPerspectiveTransform不允许我超过4分。有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

因此,您希望将对象转换为最小的封闭圆,

如下图所示,将芦苇矩形变换为绿色圆圈。那就是将边界矩形变换为边界圆。

enter image description here

请执行以下操作,

  

 Mat src=imread("src.png");
 Mat thr;
 cvtColor(src,thr,CV_BGR2GRAY);
 threshold( thr, thr, 20, 255,CV_THRESH_BINARY_INV );
 bitwise_not(thr,thr);
 vector< vector <Point> > contours; // Vector for storing contour
 vector< Vec4i > hierarchy;

 Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create destination image
 findContours( thr.clone(), contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
 drawContours( dst,contours, 0, Scalar(255,255,255),CV_FILLED, 8, hierarchy );
 Rect R=boundingRect(contours[0]);
 Point2f center;
 float radius;
 minEnclosingCircle( (Mat)contours[0], center, radius);`

enter image description here

在这里,您需要从边界框和圆形计算变换矩阵,如

std::vector<Point2f> src_pts;
std::vector<Point2f> dst_pts;

src_pts.push_back(Point(R.x,R.y));
src_pts.push_back(Point(R.x+R.width,R.y));
src_pts.push_back(Point(R.x,R.y+R.height));
src_pts.push_back(Point(R.x+R.width,R.y+R.height));

dst_pts.push_back(Point2f(center.x-radius,center.y-radius));
dst_pts.push_back(Point2f(center.x+radius,center.y-radius));
dst_pts.push_back(Point2f(center.x-radius,center.y+radius));
dst_pts.push_back(Point2f(center.x+radius,center.y+radius));

Mat transmtx = getPerspectiveTransform(src_pts,dst_pts);

然后应用透视变换,

Mat transformed = Mat::zeros(src.rows, src.cols, CV_8UC3);
warpPerspective(src, transformed, transmtx, src.size());
imshow("transformed", transformed);

enter image description here