gpt4 book ai didi

c++ - 计算力矩时 OpenCV 断言失败

转载 作者:太空宇宙 更新时间:2023-11-03 22:51:07 24 4
gpt4 key购买 nike

我一直在尝试使用 this查找此时刻的教程 pic .但是我一直在行中收到断言失败错误

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);
}

最佳答案

moments的输入应该是 std::vector<cv::Point> , 不是 std::vector<std::vector<cv::Point>> .你只是忘了选择第 i 个轮廓:

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

关于c++ - 计算力矩时 OpenCV 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37463328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com