2016-05-26 13 views
1

Ich habe versucht, this Tutorial zu verwenden, um Momente dieser pic zu finden. Aber ich bekomme immer Bestätigung fehlgeschlagen Fehler in ZeileOpenCV Assertion Fehler beim Berechnen von Momenten

mu[i] = moments(contours, false); 

Was mache ich hier falsch?

#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/core/core.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include <iostream> 
#include <cmath> 
#include <stdio.h> 
#include <stdlib.h> 
using namespace cv; 
using namespace std; 

int main(int, char *[]) { 
    cv::Mat roi = cv::imread("ROI.jpg"); 
    cv::Mat kontury = roi; 
    cv::cvtColor(roi, kontury, CV_RGB2GRAY); 
    vector<vector<Point> > contours; 
    vector<Vec4i> hierarchy; 
    findContours(kontury, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 

    /// Get the moments 
    vector<Moments> mu(contours.size()); 
    for (int i = 0; i < contours.size(); i++) 
    { 
     mu[i] = moments(contours, false); 
    } 

    /// Get the mass centers: 
    vector<Point2f> mc(contours.size()); 
    for (int i = 0; i < contours.size(); i++) 
    { 
     mc[i] = Point2f(mu[i].m10/mu[i].m00, mu[i].m01/mu[i].m00); 
    } 

    /// Calculate the area with the moments 00 and compare with the result of the OpenCV function 
    Mat drawing = Mat::zeros(roi.size(), CV_8UC3); 

    printf("\t Info: Area and Contour Length \n"); 
    for (int i = 0; i< contours.size(); i++) 
    { 
     printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f \n", i, mu[i].m00, contourArea(contours[i]), arcLength(contours[i], true)); 
     Scalar color = Scalar(rand() % 256, rand() % 256, rand() % 256); 
     drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point()); 
     circle(drawing, mc[i], 4, color, -1, 8, 0); 
    } 

    cv::waitKey(-1); 
} 
+0

Können Sie die Details des Assertionsfehlers posten? – Sunreef

+0

@Sunreef Sicheres Ding- http://i.imgur.com/JriVMEl.jpg Miki wies darauf hin, wo mein Fehler war. –

Antwort

1

Die Eingabe von moments sollte ein std::vector<cv::Point>, nicht std::vector<std::vector<cv::Point>> sein. Sie haben gerade vergessen, die i-te Kontur auszuwählen:

mu[i] = moments(contours[i], false); 
//      ^^^ 
+0

Danke für den Vorschlag - es hat funktioniert! (Es findet zu viele Konturen, aber es ist ein Schritt voraus) :) –

+0

Sie sollten zu 'findContours' ein binäres Bild übergeben, während Sie nicht, wahrscheinlich aufgrund von JPEG-Komprimierung ist. Fügen Sie 'kontury = kontury> 100;' vor 'findContours' hinzu – Miki

Verwandte Themen