2017-10-16 115 views
0

我試圖使用OpenCV檢測以下框的尺寸。如果您覺得它有用,我也會看到深度圖片。使用opencv檢測一個框

我曾嘗試是使用Hough變換算法得到的線條interesected點......

我的問題是,是否有可能得到輪廓然後使用選擇的輪廓Hough變換?代碼如何看起來像這樣?

#include <opencv2/imgproc.hpp> 
#include <opencv2/highgui.hpp> 
#include <iostream> 

using namespace cv; 
using namespace std; 

cv::Point2f center(0, 0); 

cv::Point2f computeIntersect(cv::Vec4i a, 
    cv::Vec4i b) 
{ 
    int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3], x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3]; 
    float denom; 

    if (float d = ((float)(x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4))) 
    { 
    cv::Point2f pt; 
    pt.x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4))/d; 
    pt.y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4))/d; 
    return pt; 
    } 
    else 
    return cv::Point2f(-1, -1); 
} 

void sortCorners(std::vector<cv::Point2f>& corners, 
    cv::Point2f center) 
{ 
    std::vector<cv::Point2f> top, bot; 

    for (int i = 0; i < corners.size(); i++) 
    { 
    if (corners[i].y < center.y) 
     top.push_back(corners[i]); 
    else 
     bot.push_back(corners[i]); 
    } 
    corners.clear(); 

    if (top.size() == 2 && bot.size() == 2) { 
    cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0]; 
    cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1]; 
    cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0]; 
    cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1]; 


    corners.push_back(tl); 
    corners.push_back(tr); 
    corners.push_back(br); 
    corners.push_back(bl); 
    } 
} 

int main(int argc, char** argv) 
{ 


    Mat src, src_copy, edges, dst; 
    src = imread("freezeFrame__1508152029892.png", 0); 

    src_copy = src.clone(); 

    GaussianBlur(src, edges, Size(5, 5), 1.5, 1.5); 

    erode(edges, edges, Mat());// these lines may need to be optimized 
    dilate(edges, edges, Mat()); 
    dilate(edges, edges, Mat()); 
    erode(edges, edges, Mat()); 

    Canny(edges, dst, 50, 300, 3); // canny parameters may need to be optimized 
    imshow("canny", dst); 

    vector<Point> selected_points; 
    vector<vector<Point> > contours; 
    findContours(dst, contours, RETR_LIST, CHAIN_APPROX_SIMPLE); 


    for (size_t i = 0; i < contours.size(); i++) 
    { 
    Rect minRect = boundingRect(contours[i]); 

    if ((minRect.width > src.cols/2) | (minRect.height > src.rows/2)) // this line also need to be optimized 
    { 
     selected_points.insert(selected_points.end(), contours[i].begin(), contours[i].end()); 

     drawContours(src, contours, i, Scalar(0, 0, 255), 3); 

    } 
    } 

imshow("Selected contours", src); 

cv::waitKey(0); 

    vector<Vec4i> lines; 
    HoughLinesP(selected_points, lines, 1, CV_PI/180, 50, 50, 10); 
    for (size_t i = 0; i < lines.size(); i++) 
    { 

    cv::Vec4i v = lines[i]; 
    lines[i][0] = 0; 
    lines[i][1] = ((float)v[1] - v[3])/(v[0] - v[2]) * -v[0] + v[1]; 
    lines[i][2] = src.cols; 
    lines[i][3] = ((float)v[1] - v[3])/(v[0] - v[2]) * (src.cols - v[2]) + v[3]; 

    v = lines[i]; 
    cv::line(src, cv::Point(v[0], v[1]), cv::Point(v[2], v[3]), CV_RGB(0, 255, 0)); 

    } 



    std::vector<cv::Point2f> corners; 
    for (int i = 0; i < lines.size(); i++) 
    { 
    for (int j = i + 1; j < lines.size(); j++) 
    { 
     cv::Point2f pt = computeIntersect(lines[i], lines[j]); 
     if (pt.x >= 0 && pt.y >= 0) 
     corners.push_back(pt); 
    } 
    } 

    for (int i = 0; i < corners.size(); i++) 
    { 
    // Draw corner points 
    cv::circle(src, corners[i], 3, CV_RGB(255, 0, 0), 2); 
    } 

    imshow("line src", src); 
    imshow("line dest", edges); 
    cv::waitKey(0); 


    return 0; 
} 

enter image description here

我要計算這個盒子其實體積...

回答

0

這是一個非常寬泛的問題......我只是給你我將如何進行一些建議,而不是代碼。我想你想確定一張休閒桌子上的任何盒子的體積,而不僅僅是這個特定圖像中的盒子。

使用(假定是)密集的深度圖像,您可以重建場景的3D點雲。您可以使用pcl library以及RANSAC或其他擬合方法來適應點雲中的平面。

然後您應該瞭解哪個表面是表格,然後找到盒子表面並計算封閉體積。

另一種方式可能是使用cv :: goodFeaturesToTrack()在圖像中找到角點,再次使用深度圖像計算相應的3D點以計算檢測到的角點的3D位置,然後找到連接角點和試圖找出哪條線垂直於其他線。

+0

如何使用深度圖像計算相應的3D點? – andre

+0

找到連接角的線是N!問題我想,我會如何優化它? – andre