简体   繁体   English

FindContour OpenCV C ++

[英]FindContour OpenCV C++

I have trouble when using findContour() function from opencv. 我在opencv中使用findContour()函数时遇到了麻烦。 It crash and output the following error: 它崩溃并输出以下错误:

在此输入图像描述

Here is my code: 这是我的代码:

using namespace cv;
using namespace std;

Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);

/// Function header
void thresh_callback(int, void*);

/** @function main */
int main(int argc, char** argv)
{
    src = imread("test.png");
    /// Load source image and convert it to gray
    //src = imread(argv[1], 1);

    /// Convert image to gray and blur it
    cvtColor(src, src_gray, CV_BGR2GRAY);
    blur(src_gray, src_gray, Size(3, 3));

    /// Create Window
    char* source_window = "Source";
    namedWindow(source_window, CV_WINDOW_AUTOSIZE);
    imshow(source_window, src);

    createTrackbar(" Canny thresh:", "Source", &thresh, max_thresh, thresh_callback);
    thresh_callback(0, 0);

    waitKey(0);
    return(0);
}

/** @function thresh_callback */
void thresh_callback(int, void*)
{
    Mat canny_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    /// Detect edges using canny
    Canny(src_gray, canny_output, thresh, thresh * 2, 3);
    /// Find contours
    findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    /// Draw contours
    Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);
    for (int i = 0; i< contours.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
    }

    /// Show in a window
    namedWindow("Contours", CV_WINDOW_AUTOSIZE);
    imshow("Contours", drawing);
}

All the setting is correct such as property sheet as the program can works but it crash once it done the thresh_callback functions. 所有设置都是正确的,例如属性表,因为程序可以工作但是一旦完成thresh_callback函数就会崩溃。 I use visual studio 2015 with opencv 3.0. 我使用visual studio 2015和opencv 3.0。 I had tried on visual studio 2012 or try other version of opencv like 2.4.9. 我曾尝试过visual studio 2012或尝试其他版本的opencv,如2.4.9。 Unfortunately, it still not works. 不幸的是,它仍然不起作用。 Hope you all can help me 希望你们都能帮助我

Here showed my property sheet settings: 1. Debug x64 property sheet 这里显示了我的属性表设置:1。调试x64属性表 调试x64属性表

  1. Release x64 property sheet 发布x64属性表 发布x64属性表

I am able to run others image processing function such as cv::imread . 我能够运行其他图像处理功能,如cv::imread Only the findContour() having errors. 只有findContour()有错误。

Update 更新

library path: 库路径: 在此输入图像描述

I have tested your code, I can compile it successfully against my input image (binary image of hand) and it does provide the contour of the hand. 我已经测试了你的代码,我可以根据我的输入图像(手的二进制图像)成功编译它,它确实提供了手的轮廓。 As mentioned by @Miki , you must check your linked libraries. 正如@Miki所提到的,您必须检查链接的库。 On Eclipse IDE we have to go to: Project -> Properties -> C/C++ Build -> Settings. 在Eclipse IDE上,我们必须转到:Project - > Properties - > C / C ++ Build - > Settings。 On the right side, make sure you are in the 'Tool Settings' tab and then head to MinGW C++ Linker -> Libraries. 在右侧,确保您位于“工具设置”选项卡中,然后前往MinGW C ++链接器 - >库。 You must have something similar on Visual Studio, either way you still have to check the libraries you have included in your project in the project settings. 您必须在Visual Studio上有类似的东西,无论哪种方式,您仍然需要在项目设置中检查项目中包含的库。 You must have the following linked libraries apart from the other mandatory libraries: 除了其他必需库之外,您必须具有以下链接库:

  1. libopencv_imgproc300 libopencv_imgproc300
  2. libopencv_highgui300 libopencv_highgui300

Please note that the number 300 suggests that the OpenCV version is 3.0 , I have OpenCV 3.1 thus my libraries contain the number 310 at the end eg - libopencv_imgproc310. 请注意,数字300表示OpenCV版本是3.0,我有OpenCV 3.1,因此我的库最后包含数字310,例如 - libopencv_imgproc310。 Let me know if this helps. 如果这有帮助,请告诉我。

You're linking the wrong libraries. 你正在链接错误的库。


You are linking in: 您正在链接:

C:\\opencv\\build\\x64\\ vc12 \\lib C:\\ opencv \\ build \\ x64 \\ vc12 \\ lib

That means you're using OpenCV compiled with vc12 compiler (Visual Studio 2013). 这意味着您正在使用使用vc12编译器(Visual Studio 2013)编译的OpenCV。 But you're using Visual Studio 2015, so you need to link to OpenCV compiled with vc14 . 但是您使用的是Visual Studio 2015,因此您需要链接到使用vc14编译的OpenCV。

So, look if you have the folder: 所以,看看你是否有这个文件夹:

C:\\opencv\\build\\x64\\ vc14 \\lib C:\\ opencv \\ build \\ x64 \\ vc14 \\ lib

Probably not, because OpenCV 3.0 has not the prebuild for vc14. 可能不是,因为OpenCV 3.0没有vc14的预建。 In this case you can either: 在这种情况下,您可以:

  1. Recompile OpenCV 3.0 with vc14 用vc14重新编译OpenCV 3.0
  2. Download OpenCV 3.2 which has prebuilt binaries for x64,vc14. 下载OpenCV 3.2 ,其中包含x64,vc14的预构建二进制文件。 This is the recommended approach, since OpenCV 3.2 added some nice functions, and several bug fixes. 这是推荐的方法,因为OpenCV 3.2添加了一些不错的功能,以及一些错误修复。
  3. Use Visual Studio 2013 with your current libraries 将Visual Studio 2013与当前库一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM