我正在使用OpenCV 3.2并使用Visual Studio 2017进行编译。
我正在尝试通过将摄像头输入阈值化为二进制图像来构建简单的运动跟踪代码,然后使用OpenCV的findContours
函数来检测对象。不幸的是,我遇到了一个错误,该错误表明该函数存在未处理的异常。
(error message)
我通过手动调整项目属性链接了OpenCV库,如下所示:
C:\opencv320\build\include
C:\opencv320\build\x64\vc14\lib
opencv_world320d.lib
和opencv_world320.lib
我一直在使用相同的findContours
函数测试不同的代码,但无论代码如何,它似乎都会给出相同的错误。我认为它与堆有关,以及如何分配和释放内存,但我似乎无法自己调试它。
以下是我一直在测试的示例代码:
#include "stdafx.h"
#include <opencv2\highgui.hpp>
#include <opencv2\videoio.hpp>
#include <opencv2\opencv.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
struct Component
{
cv::Rect boundingBox;
double area;
double circularity;
};
int main()
{
// Create a small image with a circle in it.
cv::Mat image(256, 256, CV_8UC3, cv::Scalar(0, 0, 0));
cv::circle(image, cv::Point(80, 110), 42, cv::Scalar(255, 127, 63), -1);
// Find canny edges.
cv::Mat cannyEdges;
cv::Canny(image, cannyEdges, 80, 60);
// Show the images.
cv::imshow("img", image);
cv::imshow("cannyEdges", cannyEdges);
// Find the contours in the canny image.
std::vector<cv::Vec4i> hierarchy;
// "Each contour is stored as a vector of points."
typedef std::vector<std::vector<cv::Point> > TContours;
TContours contours;
cv::findContours(cannyEdges, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);
// cannyEdges is destroyed after calling cv::findContours
// Print number of found contours.
std::cout << "Found " << contours.size() << " contours." << std::endl;
// Convert contours to Components.
typedef std::vector<Component> TComponents;
TComponents components;
for (TContours::const_iterator it(contours.begin()); it != contours.end(); ++it)
{
Component c;
c.area = cv::contourArea(*it);
c.boundingBox = cv::boundingRect(*it);
c.circularity = 0.0; // Insert whatever you mean by circularity;
components.push_back(c);
}
for (TComponents::const_iterator it(components.begin()); it != components.end(); ++it)
std::cout << it->area << std::endl; // and whatever you want.
// Wait for user input.
cv::waitKey();
}