2012-12-10 65 views
0

你好我正在做一個android應用程序,它使用OpenCV來檢測矩形/方塊,檢測它們我正在使用來自squares.cpp的函數(修改了一下)。找到的每個正方形的點都存儲在矢量>正方形中,然後將其傳遞給選擇最大的正方形並將其存儲在向量theBiggestSq中的函數。問題在於我將在下面粘貼代碼的裁剪功能(我會將鏈接發佈到顯示問題的YouTube)。如果實際的廣場距離攝像機足夠遠,它可以正常工作,但如果我將它關閉一點,它會掛起。我將從LogCat中發佈問題的打印屏幕,並打印出這些點(從BiggestSq矢量獲取的邊界點,可能有助於找到解決方案)。OpenCV作物功能致命信號11

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

enter image description here

簾線分別印刷有: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); 

    } 

回答

2

這個錯誤基本上告訴你原因 - 投資回報率超過了圖像尺寸。這意味着當您從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; 
+0

所以這應該避免這種錯誤再次發生,或者它只是爲了確認「寬度/高度推動外樹形象」的尺寸? – silvestrairwave

+0

我沒有測試過它,但它應該確保roi始終位於圖像內部,不管是哪個盒子返回,並且我相信它是圖像之外的roi導致您的錯誤。 – Chris

+0

好的,謝謝!我會立即嘗試並儘快發佈結果。最後一件事:你知道爲什麼繪製矩形/方塊的邊界是奇怪的嗎?應該是綠色的,但正如你在視頻中看到的那樣,它們是白色的玻璃狀的東西? (很難描述)。 – silvestrairwave

相關問題