C OPENCV恢复边界矩形
C++ Opencv Rescale Bounding Rect
所以,我有一个问题:
im在Android中进行操作,并且由于我使用手机拍摄的图像非常高,因此我想将图像大小调整到较小的图像,获取轮廓并处理图像,然后在原始图像上创建界限。为此,我需要扩展该边界框,以使其完全适合我的原始图像。我拥有的代码在原始图像上的处理和绘制边界框非常合作,但是如何进行缩放?这是代码片段:
Mat &image = *(Mat *) matAddrRgba;
//over here I should resize image and do the processing with the resized one, and in the end, scale everything back so I can draw the bounding box to the original
Rect bounding_rect;
Mat thr(image.rows, image.cols, CV_8UC1);
cvtColor(image, thr, CV_BGR2GRAY); //Convert to gray
threshold(thr, thr, 150, 255, THRESH_BINARY + THRESH_OTSU); //Threshold the gray
vector<vector<Point> > contours; // Vector for storing contour
vector<Vec4i> hierarchy;
RotatedRect rect;
findContours(thr, contours, hierarchy, CV_RETR_CCOMP,
CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image
sort(contours.begin(), contours.end(),
compareContourAreas); //Store the index of largest contour
bounding_rect = boundingRect(contours[0]);
rect = minAreaRect(contours[0]);
// matrices we'll use
Mat rot_mat, rotated;
// get angle and size from the bounding box
float angle = rect.angle;
Size rect_size = rect.size;
if (rect.angle < -45.) {
angle += 90.0;
swap(rect_size.width, rect_size.height);
}
rot_mat = getRotationMatrix2D(rect.center, angle, 1);
warpAffine(image, rotated, rot_mat, image.size(), INTER_CUBIC);
image = rotated;
cvtColor(image, thr, CV_BGR2GRAY); //Convert to gray
threshold(thr, thr, 150, 255, THRESH_BINARY + THRESH_OTSU); //Threshold the gray
findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
sort(contours.begin(), contours.end(), compareContourAreas);
bounding_rect = boundingRect(contours[0]);
image = Mat(image, bounding_rect);
好吧,所以,我将从此开始:
Mat &img = (Mat ) matAddrRgba;
Mat image;
double thrA = 5;
resize(img, image, Size((int) (img.size().width / thrA), (int) (img.size().height / thrA)));
Rect bounding_rect;
Mat thr(image.rows, image.cols, CV_8UC1);
cvtColor(image, thr, CV_BGR2GRAY); //Convert to gray
threshold(thr, thr, 150, 255, THRESH_BINARY + THRESH_OTSU); //Threshold the gray
vector<vector<Point> > contours; // Vector for storing contour
vector<Vec4i> hierarchy;
RotatedRect rect;
findContours(thr, contours, hierarchy, CV_RETR_CCOMP,
CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image
sort(contours.begin(), contours.end(),
compareContourAreas); //Store the index of largest contour
bounding_rect = boundingRect(contours[0]);
bounding_rect.width = (int) (bounding_rect.width * thrA);
bounding_rect.height = (int) (bounding_rect.height * thrA);
bounding_rect.x = (int) (bounding_rect.x * thrA);
bounding_rect.y = (int) (bounding_rect.y * thrA);
rectangle(img, bounding_rect, Scalar(0, 172, 236, 255), 3);
如您所见,您应该有一个比例,该比例应与边界矩形的宽度,高度,x和y相乘。您可以从中找出其余的。
相关文章:
- 用C++将哈希表写入文件并从文件中恢复
- std::当在256字节边界上写入整数时,流的奇怪行为
- 使用不变量来确定二分搜索中的边界条件
- 跨 DLL 边界访问虚拟方法是否安全/可能?
- Opencv 恢复到比我设置的更高的分辨率
- 变量在使用赋值语句赋值后恢复为以前的值
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 在 64 位边界上对齐C++结构数组?
- 删除所有字符串后如何恢复 QStringList?
- std::数组边界检查如何工作?
- CGAL:如何创建填充边界曲线的曲面网格?
- 当对套接字 send() 的同步调用由于连接另一端丢失而被阻止时,如何恢复?
- 在 c++ 中是否允许创建具有运行时边界的数组?
- 是否可以跨 dll 边界返回常量引用/指向 std::vectors?
- 如何仅使用一次固定<<设置精度(2)?或者至少恢复到默认行为?
- "co_yield"是否可以在恢复协程时从调用方返回值?
- OpenSSL C API:如何在程序exec()之后恢复TLS连接?
- 通过指针恢复对数组的引用.UB与否?
- 从不同进程中的另一个线程挂起/恢复线程或进程
- C OPENCV恢复边界矩形