gpt4 book ai didi

c++ - OpenCV C++ VS2010 : FindContours throws exception

转载 作者:行者123 更新时间:2023-11-28 04:54:48 25 4
gpt4 key购买 nike

您好,我正在尝试运行一个简单的 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;
}

这是异常的图像:

Exception thrown

我该如何解决这个问题?是否缺少 dll 文件或需要解决某些设置问题?

我对代码做了如下改动

vector<Vec4i> hierarchy;
findContours( contourOutput, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);

但现在我得到以下异常:

enter image description here

最佳答案

那么您的代码工作得很好。我只在我的项目文件中注释掉了头文件 stdafx.h 和 nonfree.hpp。这是我得到的输出 enter image description here

确保您提供了正确的输入 img 路径,或者将您的输入图像粘贴到您的 Visual Studio 解决方案目录中,然后直接加载图像。

问题可能出在其中一个模块上。我建议重新安装 opencv(尝试下载最新版本)并重试。确保您正在添加 xyzd.lib 文件,例如 opencv_highgui330d.lib opencv_imgproc330d.lib 在 linker>>Input>> additional dependencies 中。版本可能因您而异。希望这对你有帮助!

关于c++ - OpenCV C++ VS2010 : FindContours throws exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47406776/

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