【发布时间】:2018-05-04 13:02:45
【问题描述】:
您好,我正在尝试运行一个简单的 OpenCV 程序来检测图片中的轮廓。但是,findcontours 不断抛出异常。
这是我的代码:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <opencv2\opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
/** @function main */
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <string.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, const char * argv[]) {
IplImage* img = cvLoadImage("C:\\Users\\310217955\\Documents\\Visual Studio 2010\\Projects\\aviTest\\Debug\\Pictures\\Capture.jpg");
cv::Mat image = cv::cvarrToMat(img);
if (!image.data) {
std::cout << "Image file not found\n";
return 1;
}
//Prepare the image for findContours
cv::cvtColor(image, image, CV_BGR2GRAY);
cv::threshold(image, image, 128, 255, CV_THRESH_BINARY);
//Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten
//std::vector<std::vector<cv::Point> > contours;
vector<cv::Mat> contours;
cv::Mat contourOutput = image.clone();
cv::findContours( contourOutput, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE );
//Draw the contours
cv::Mat contourImage(image.size(), CV_8UC3, cv::Scalar(0,0,0));
cv::Scalar colors[3];
colors[0] = cv::Scalar(255, 0, 0);
colors[1] = cv::Scalar(0, 255, 0);
colors[2] = cv::Scalar(0, 0, 255);
for (size_t idx = 0; idx < contours.size(); idx++) {
cv::drawContours(contourImage, contours, idx, colors[idx % 3]);
}
cv::imshow("Input Image", image);
cvMoveWindow("Input Image", 0, 0);
cv::imshow("Contours", contourImage);
cvMoveWindow("Contours", 200, 0);
cv::waitKey(0);
system("PAUSE");
return 0;
}
这是异常的图像:
我怎样才能解决这个问题?是否缺少 dll 文件或需要解决某些设置?
我对代码做了以下修改
vector<Vec4i> hierarchy;
findContours( contourOutput, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
但现在我得到以下异常:
【问题讨论】:
-
请不要使用贬值的 c API!请参考c++目录中的示例代码示例并还原!
-
看不懂,我目前的opencv版本是2.4.9
-
您是如何通过“错误 C1010:在查找预编译头文件时意外结束文件。您是否忘记在源代码中添加 '#include "StdAfx.h"'?”
-
为什么要多次包含相同的标题?为什么有多个相同的
using namespace? (其实是why any?) |您的 OpenCV 副本是否使用/用于 MSVC++ 10.0(即 MSVS 2010)?您是否与为您所处的“模式”(调试/发布)构建的库链接?
标签: c++ visual-studio-2010 opencv