【问题标题】:OpenCV crop function fatal signal 11OpenCV 裁剪函数致命信号 11
【发布时间】:2012-12-10 15:17:03
【问题描述】:

您好,我正在做一个使用 OpenCV 检测矩形/正方形的 android 应用程序,为了检测它们,我正在使用 squares.cpp 中的函数(稍作修改)。找到的每个正方形的点存储在向量>正方形中,然后我将其传递给选择最大的函数并将其存储在向量 theBiggestSq 中。问题出在裁剪功能上,我将在下面粘贴代码(我也会将链接发布到 youtube 以显示问题)。如果实际的正方形距离相机足够远,它可以正常工作,但如果我在某个时候将它关闭一点,它就会挂起。我将从 LogCat 发布问题的打印屏幕,并打印出点(取自 BiggestSq 向量的边界点,也许有助于找到解决方案)。

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

    }

【问题讨论】:

    标签: android opencv crop


    【解决方案1】:

    这个错误基本上告诉你原因 - 你的投资回报率超过了图像尺寸。这意味着当您从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;
    

    【讨论】:

    • 所以这应该防止这个错误再次发生,或者只是为了确认'宽度/高度将尺寸推到图像之外'?
    • 我没有测试过,但它应该确保 roi 始终在图像内,无论返回什么框,我相信是 roi 在图像之外导致你出错。
    • 好的,谢谢!我现在就试试,很快就会公布结果。最后一件事:你知道为什么绘制找到的矩形/正方形的边界很奇怪吗?应该是绿色的,但正如你在视频中看到的那样,它们是白色玻璃状的东西? (难以描述)。
    • 不确定,我看不到您在代码中绘制矩形的位置。如果有帮助,您应该使用 cv::rectangle 函数...
    • 看起来您的解决方案现在可以工作了,因为它现在可以编写文件代码行了! :) 它仍然存在,但是根据 SD 卡上的保存显示一些问题:pastebin.com/AFqeu4uG 也许这只能保存 iplimage?不是马特?编辑帖子:添加绘图代码
    猜你喜欢
    • 2014-09-13
    • 2012-09-16
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    相关资源
    最近更新 更多