c++ - C++ 中的 4 点 OpenCV 裁剪透视

标签 c++ opencv

我正在寻找一种将图像透视裁剪 4 点的方法。 我找到了这个来源,但是当我使用它时,结果是错误的。

原始来源: This is original source

有什么问题?

这是我的来源:

int main()

{

cv::Mat src = cv::imread("E:\\aaaaaa.jpg");


vector<Point> not_a_rect_shape;
not_a_rect_shape.push_back(Point(2224, 257));
not_a_rect_shape.push_back(Point(372, 393));
not_a_rect_shape.push_back(Point(338, 1498));
not_a_rect_shape.push_back(Point(2397, 1414));

// For debugging purposes, draw green lines connecting those points 
// and save it on disk
const Point* point = &not_a_rect_shape[0];
int n = (int)not_a_rect_shape.size();
Mat draw = src.clone();
polylines(draw, &point, &n, 1, true, Scalar(0, 255, 0), 3, LINE_AA);
imwrite("E:\\draw.jpg", draw);

// Assemble a rotated rectangle out of that info
RotatedRect box = minAreaRect(cv::Mat(not_a_rect_shape));
std::cout << "Rotated box set to (" << box.boundingRect().x << "," << box.boundingRect().y << ") " << box.size.width << "x" << box.size.height << std::endl;

Point2f pts[4];

box.points(pts);

// Does the order of the points matter? I assume they do NOT.
// But if it does, is there an easy way to identify and order 
// them as topLeft, topRight, bottomRight, bottomLeft?

cv::Point2f src_vertices[3];
src_vertices[0] = pts[0];
src_vertices[1] = pts[1];
src_vertices[2] = pts[3];
//src_vertices[3] = not_a_rect_shape[3];

Point2f dst_vertices[3];
dst_vertices[0] = Point(0, 0);
dst_vertices[1] = Point(box.boundingRect().width - 1, 0);
dst_vertices[2] = Point(0, box.boundingRect().height - 1);

/* Mat warpMatrix = getPerspectiveTransform(src_vertices, dst_vertices);

cv::Mat rotated;
cv::Size size(box.boundingRect().width, box.boundingRect().height);
warpPerspective(src, rotated, warpMatrix, size, INTER_LINEAR, BORDER_CONSTANT);*/
Mat warpAffineMatrix = getAffineTransform(src_vertices, dst_vertices);

cv::Mat rotated;
cv::Size size(box.boundingRect().width, box.boundingRect().height);
warpAffine(src, rotated, warpAffineMatrix, size, INTER_LINEAR, BORDER_CONSTANT);

imwrite("E:\\rotated.jpg", rotated);


return 0;

这是结果: enter image description here

enter image description here

enter image description here

那么我该如何解决呢?

谢谢。

最佳答案

您将 pts 错误地匹配到边界框的点。

什么是reference说说 RotatedRect::points 方法 ?:

void cv::RotatedRect::points ( Point2f pts[] ) const returns 4 vertices of the rectangle

Parameters pts The points array for storing rectangle vertices. The order is bottomLeft, topLeft, topRight, bottomRight.

在 XY 坐标系中它看起来像:

   (0,0)--------------------------------> x
   |
   |              topLeft ----- topRight
   |             /                        \
   |            /                          \
   |    bottomLeft --------------------- bottomRight
  \ /
   y

这个

src_vertices[0] = pts[0]; // bottomLeft
src_vertices[1] = pts[1]; // topLeft
src_vertices[2] = pts[3]; // bottomRight

应该对应于那个(很容易翻译成边界框的坐标):

Point2f dst_vertices[3];
dst_vertices[0] = Point(0, box.boundingRect().height);
dst_vertices[1] = Point(0, 0);
dst_vertices[2] = Point(box.boundingRect().width, box.boundingRect().height);

关于c++ - C++ 中的 4 点 OpenCV 裁剪透视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58864829/

相关文章:

c++ - 您将如何对内存分配器进行单元测试?

c++ - QProcess:启动子进程而不等待完成但收到完成信号

c++ - 函数参数 - 指针或对指针的引用?

c++ - 对拍摄照片并将其保存到文件的 opencv c++ 程序进行单元测试

matlab - 使用 A0 和 A4 相机校准板有什么区别?有没有 ?如果是,是什么以及为什么?

c++ - 获取重载静态函数的地址

C++ 表达式 : vector subscript out of range error Line:1733

c++ - 视频加载时间很长。打开cv

python - 从字节串到 Python 中的 OpenCV 的 IplImage?

objective-c - 将图像显示到 iPhone 模拟器屏幕