【问题标题】:OpenCV C++ VS2010: FindContours throws exceptionOpenCV C++ VS2010:FindContours 抛出异常
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

  • 我之前使用的是opencv 2.4.9,现在我下载并安装了opencv 2.4.10。但 findcontours 仍然存在问题。我使用的是旧版本,因为我的开发工具是 Visual Studio 2010
  • 我认为opencv的版本和visual studio的版本没有关系。如果确实如此,您可能需要考虑将您的 Visual Studio 与 opencv 一起升级。保持升级将始终对您未来的项目有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
  • 2020-02-21
  • 2013-03-15
  • 1970-01-01
  • 1970-01-01
  • 2017-01-12
相关资源
最近更新 更多