你好,我正在做一个安卓应用程序,它使用OpenCV来检测矩形/正方形,为了检测它们,我正在使用squares.cpp的函数(修改了一点)。我找到的每个方块的点都存储在vector>方块中,然后我将它传递给函数,该函数选择最大的一个并将其存储在矢量theBiggestSq中。问题出在裁剪功能上,我将在下面粘贴代码(我会在youtube上发布显示问题的链接)。如果实际的正方形离相机足够远,它可以正常工作,但如果我在某个点上关闭它一点,它就会挂起。我将从LogCat发布问题的打印屏幕,并且有打印出来的点(从theBiggestSq矢量中提取的边界点,也许它将有助于找到解决方案)。
void cutAndSave(vector<Point> theBiggestSq, Mat image){
RotatedRect box = minAreaRect(Mat(theBiggestSq));
// Draw bounding box in the original image (debug purposes)
//cv::Point2f vertices[4];
//box.points(vertices);
//for (int i = 0; i < 4; ++i)
//{
//cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA);
//}
//cv::imshow("box", img);
//cv::imwrite("box.png", img);
// Set Region of Interest to the area defined by the box
Rect roi;
roi.x = box.center.x - (box.size.width / 2);
roi.y = box.center.y - (box.size.height / 2);
roi.width = box.size.width;
roi.height = box.size.height;
// Crop the original image to the defined ROI
//bmp=Bitmap.createBitmap(box.size.width / 2, box.size.height / 2, Bitmap.Config.ARGB_8888);
Mat crop = image(roi);
//Mat crop = image(Rect(roi.x, roi.y, roi.width, roi.height)).clone();
//Utils.matToBitmap(crop*.clone()* ,bmp);
imwrite("/sdcard/OpenCVTest/1.png", bmp);
imshow("crop", crop);
}video of my app and its problems

打印的数据线分别是: roi.x roi.y roi.width roi.height
另一个问题是,绘制的边界应该是绿色的,但正如你在视频中看到的那样,它们是扭曲的(弯曲,就像那些边界是由玻璃制成的吗?)。
谢谢你的帮助。我是openCV的新手,只做了一个月,所以请容忍。
编辑:绘图代码:
//draw//
for( size_t i = 0; i < squares.size(); i++ )
{
const Point* p = &squares[i][0];
int n = (int)squares[i].size();
polylines(mBgra, &p, &n, 1, true, Scalar(255,255,0), 5, 10);
//Rect rect = boundingRect(cv::Mat(squares[i]));
//rectangle(mBgra, rect.tl(), rect.br(), cv::Scalar(0,255,0), 2, 8, 0);
}发布于 2012-12-10 23:37:15
这个错误基本上告诉了你原因--你的ROI超出了图像尺寸。这意味着当您从RotatedRect box中提取Rect roi时,x或y都小于零,或者宽度/高度将尺寸推到图像之外。您应该使用类似下面这样的方法来检查
// Propose rectangle from data
int proposedX = box.center.x - (box.size.width / 2);
int proposedY = box.center.y - (box.size.height / 2);
int proposedW = box.size.width;
int proposedH = box.size.height;
// Ensure top-left edge is within image
roi.x = proposedX < 0 ? 0 : proposedX;
roi.y = proposedY < 0 ? 0 : proposedY;
// Ensure bottom-right edge is within image
roi.width =
(roi.x - 1 + proposedW) > image.cols ? // Will this roi exceed image?
(image.cols - 1 - roi.x) // YES: make roi go to image edge
: proposedW; // NO: continue as proposed
// Similar for height
roi.height = (roi.y - 1 + proposedH) > image.rows ? (image.rows - 1 - roi.y) : proposedH;https://stackoverflow.com/questions/13803728
复制相似问题