Opencv Moments在尝试检测轮廓中心时没有名为m00的成员

时间:2017-08-02 03:56:46

标签: c++ opencv

我一直在努力制作一个使用opencv和c ++的色彩检测器,但是在编译时我遇到了大量错误,我不知道为什么它不能正常工作

我的代码:

#include "opencv2/imgproc.hpp"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

void detect(cv::Scalar LOWER_COLOR, cv::Scalar UPPER_COLOR, cv::Mat STREAM, double *P_CX, double *P_CY, std::vector < std::vector < cv::Point > > *P_CNTS, int *FAILED){
    cv::Mat ALL_COLORS_IN_RANGE;
    cv::inRange(STREAM, LOWER_COLOR, UPPER_COLOR, ALL_COLORS_IN_RANGE);
    cv::Mat BLURRED;
    cv::GaussianBlur(ALL_COLORS_IN_RANGE, BLURRED, cv::Size (121, 121), 0, 0);
    cv::Mat THRESH;
    cv::threshold(BLURRED, THRESH, 100, 255, 0);
    cv::Mat OBJECT;
    cv::Mat DIALATE_KERNEL = cv::getStructuringElement(cv::MORPH_RECT, cv::Size((121*121) + 1, (121*121)+1));
    cv::dilate(THRESH, OBJECT, DIALATE_KERNEL);
    std::vector < std::vector < cv::Point > > CNTS;
    std::vector<cv::Vec4i> HIERARCHY;
    cv::findContours (OBJECT, CNTS, HIERARCHY, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point (0, 0));
    if(CNTS.size() == 0){
        *FAILED = 1;
    }
    else{
        cv::Moments MU[CNTS.size()];
        MU = cv::moments(CNTS);
        if(MU.m00 != 0){
            *P_CNTS = CNTS;
            *P_CX = MU.m10 / MU.m00
            *P_CY = MU.m01 / MU.m00
        }
        else{
        *FAILED = 1;
        }
    }
}

我的编译器输出:

https://pastebin.com/9Y8Y4yaR

1 个答案:

答案 0 :(得分:0)

我通过找到最大的轮廓并找到轮廓的瞬间来解决我的问题。

#include "opencv2/imgproc.hpp"
#include "opencv2/core.hpp"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

void detect(cv::Scalar LOWER_COLOR, cv::Scalar UPPER_COLOR, cv::Mat STREAM, double *P_CX, double *P_CY, std::vector < std::vector < cv::Point > > *P_CNTS, int *FAILED){
    cv::Mat ALL_COLORS_IN_RANGE;
    cv::inRange(STREAM, LOWER_COLOR, UPPER_COLOR, ALL_COLORS_IN_RANGE);
    cv::Mat BLURRED;
    cv::GaussianBlur(ALL_COLORS_IN_RANGE, BLURRED, cv::Size (121, 121), 0, 0);
    cv::Mat THRESH;
    cv::threshold(BLURRED, THRESH, 100, 255, 0);
    cv::Mat OBJECT;
    cv::Mat DIALATE_KERNEL = cv::getStructuringElement(cv::MORPH_RECT, cv::Size((121*121) + 1, (121*121)+1));
    cv::dilate(THRESH, OBJECT, DIALATE_KERNEL);
    std::vector < std::vector < cv::Point > > CNTS;
    std::vector<cv::Vec4i> HIERARCHY;
    cv::findContours (OBJECT, CNTS, HIERARCHY, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point (0, 0));
    if(CNTS.size() == 0){
        *FAILED = 1;
    }
    else{
        std::vector<cv::Moments> MU(CNTS.size());
        std::vector<cv::Point2f> MC(CNTS.size());
        int largest_area=0;
        int largest_contour_index=0;
        for( int i = 0; i< CNTS.size(); i++ ){
            double a=contourArea( CNTS[i],false);
            MU[i] = cv::moments( CNTS[i], false );
            if(a>largest_area){
                largest_area=a;
                largest_contour_index=i;
                if(MU[i].m00 != 0){
                    *P_CNTS = CNTS;
                    *P_CX = MU[i].m10/MU[i].m00;
                    *P_CY = MU[i].m01/MU[i].m00;
                }
                else{
                *FAILED = 1;
                }
            }
        }
    }
}