2011-12-02 18 views
9

Arka plan çıkarma işlemini gerçekleştiren ve ardından ön plan nesnesinin çevresinde bir sınır çizmek için findContours kullanan aşağıdaki koda sahibim.OpenCV findContours issue

// frame - Input frame from a camera. 
// output - Output frame to be displayed. 
    void process(cv:: Mat &frame, cv:: Mat &output) { 

    cv::cvtColor(frame, gray, CV_BGR2GRAY); 

    // initialize background to 1st frame 
    if (background.empty()) 
     gray.convertTo(background, CV_32F); 

    // convert background to 8U 
    background.convertTo(backImage,CV_8U); 

    // compute difference between current image and background 
    cv::absdiff(backImage,gray,foreground); 

    // apply threshold to foreground image 
    cv::threshold(foreground,output,threshold,255,cv::THRESH_BINARY_INV); 

    // accumulate background 
    cv::accumulateWeighted(gray, background, learningRate, output); 

    // Find regions of interest 
    std::vector<std::vector<cv::Point> > v; // Detected foreground points 

    cv::findContours(output,v,CV_RETR_LIST,CV_CHAIN_APPROX_NONE); 

    // Sort to find the entry with the most points at the beginning. 
      // This is done to overcome noisy input. 
    std::sort(v.begin(), v.end(), DescendingCompare); 

    cv::Mat drawing = frame; 

    std::vector<std::vector<cv::Point>> contours_poly(1); 

    // Determine an approximate polygon for v[0] which is the largest contour 
    cv::approxPolyDP(cv::Mat(v[0]), contours_poly[0], 3, false); 

    // Draw polygonal contour 
    cv::Scalar color = cv::Scalar(0,0,255); 
    cv::drawContours(drawing, contours_poly, 0, color, 2, 8, std::vector<cv::Vec4i>(), 0, cv::Point()); 

    // Show in a window 
    output = drawing; 
    v.clear(); 

} 

görüntü sadece boş beyaz arka ancak findContours() görüntünün 4 kenarları olan bir kontur dönen bir. Bu, koddaki mantığımı reddederek, bulunan en büyük kontur olmaktan çıkıyor. Bunu düzeltmek için yine de var mı? Ekran boşken boş bir vektör döndürmesini istiyorum.

Ayrıca

http://imgur.com/a/hJCQl

http://imgur.com/a/hJCQl

, bu kod verimliliğini artırmak için zaten geliştirilebilir?

+1

Yikes! Saçma hata .. Lütfen cevabınızı ayrı bir yorum olarak yazınız. – Madman

cevap

5

Arka planınız siyah (0) olmalı ve kontür oluşturmak istediğiniz herhangi bir nesne beyaz olmalıdır (veya> = 1). Tersine çevirdiniz ve bu nedenle FindContours arka planı nesneyi değil bir konturu olarak algılar.