2016-05-26 11 views
1

私はこのpicの瞬間を見つけるためにthisチュートリアルを使用しようとしています。しかし、私はアサーションを取得し続けているラインでエラーが失敗しましたモーメントの計算中にOpenCVアサーションが失敗しました

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

私はここで間違って何をしていますか?

#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

アサーションエラーの詳細を投稿できますか? – Sunreef

+0

@Sunreef確かなこと - http://i.imgur.com/JriVMEl.jpg ミキは私のミスがどこにあるのかを指摘しました。 –

答えて

1

momentsの入力はstd::vector<cv::Point>なく、std::vector<std::vector<cv::Point>>なければなりません。

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

提案をありがとう - それは働いた! (それはあまりにも多くの等高線を見つけますが、それは一歩前です):) –

+0

おそらくjpeg圧縮のために、あなたはバイナリイメージを 'findContours'に渡すべきです。 'findContours'の前に' kontury = kontury> 100; 'を追加してください。 – Miki

関連する問題