2

我是 OpenCV 的新手,所以请宽容。我正在做一个 Android 应用程序来识别正方形/矩形并裁剪它们。查找正方形/矩形的函数将找到的对象放入矢量>正方形。我只是想知道如何根据存储在vector> squares中的点中的数据裁剪图片,以及如何计算图片应该旋转的角度。感谢您的任何帮助

4

3 回答 3

2

这篇文章引用了OpenCV QA:Extract a RotatedRect area

Felix Abecassis有一篇很棒的文章,关于旋转和去偏斜图像。这也向您展示了如何提取 RotatedRect 中的数据:

您基本上只需要cv::getRotationMatrix2D来获取仿射变换的旋转矩阵,使用cv::warpAffinecv::getRectSubPix来裁剪旋转的图像。我的应用程序中的相关行是:

// This is the RotatedRect, I got it from a contour for example...
RotatedRect rect = ...;
// matrices we'll use
Mat M, rotated, cropped;
// get angle and size from the bounding box
float angle = rect.angle;
Size rect_size = rect.size;
// thanks to http://felix.abecassis.me/2011/10/opencv-rotation-deskewing/
if (rect.angle < -45.) {
    angle += 90.0;
    swap(rect_size.width, rect_size.height);
}
// get the rotation matrix
M = getRotationMatrix2D(rect.center, angle, 1.0);
// perform the affine transformation on your image in src,
// the result is the rotated image in rotated. I am doing
// cubic interpolation here
warpAffine(src, rotated, M, src.size(), INTER_CUBIC);
// crop the resulting image, which is then given in cropped
getRectSubPix(rotated, rect_size, rect.center, cropped);
于 2012-12-01T17:46:40.463 回答
1

周围有很多有用的帖子,我相信您可以进行更好的搜索。

庄稼:

旋转:

计算角度:

于 2012-12-01T11:12:22.813 回答
0

虽然这个问题已经很老了,但我认为需要一个不像旋转整个图像那样昂贵的答案(参见@bytefish 的答案)。您将需要一个边界矩形,由于某种原因rotatedRect.boundingRect()对我不起作用,所以我不得不使用Imgproc.boundingRect(contour). 这是适用于 Android 的 OpenCV,其他环境的操作几乎相同:

Rect roi = Imgproc.boundingRect(contour);
// we only work with a submat, not the whole image:
Mat mat = image.submat(roi); 
RotatedRect rotatedRect = Imgproc.minAreaRect(new MatOfPoint2f(contour.toArray()));
Mat rot = Imgproc.getRotationMatrix2D(rotatedRect.center, rotatedRect.angle, 1.0);
// rotate using the center of the roi
double[] rot_0_2 = rot.get(0, 2);
for (int i = 0; i < rot_0_2.length; i++) {
    rot_0_2[i] += rotatedRect.size.width / 2 - rotatedRect.center.x;
}
rot.put(0, 2, rot_0_2);
double[] rot_1_2 = rot.get(1, 2);
for (int i = 0; i < rot_1_2.length; i++) {
    rot_1_2[i] += rotatedRect.size.height / 2 - rotatedRect.center.y;
}
rot.put(1, 2, rot_1_2);
// final rotated and cropped image:
Mat rotated = new Mat();
Imgproc.warpAffine(mat, rotated, rot, rotatedRect.size);
于 2016-07-26T00:55:55.920 回答