2

我有一个圆柱形物体,上面粘贴了一个圆形标签。我提取图像的轮廓。在此处输入图像描述

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

这是一个不对称的图像在此处输入图像描述

编辑我试图扩展@Haris 的解决方案是这样的在此处输入图像描述

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

4

1 回答 1

2

所以你想把你的对象转换成最小的封闭圆,

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

在此处输入图像描述

因此,请执行以下操作,

 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);`

在此处输入图像描述

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

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

在此处输入图像描述

于 2014-04-23T10:35:21.930 回答