Opencv 3.0.0,C ++,Visual Studio 2015-查找轮廓和ConvexHull时出错 [英] Opencv 3.0.0 , C++, Visual Studio 2015 - error in finding contours and ConvexHull

查看:93
本文介绍了Opencv 3.0.0,C ++,Visual Studio 2015-查找轮廓和ConvexHull时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,该程序可以在某个对象周围创建一个多边形(任意数量的边),并找到该多边形的质心.为此,我选择使用凸面壳和findContours函数.在下面的程序中,您将获得两个窗口.一个窗口包含用于更改HSV和过滤器的形态值的跟踪栏,另一个窗口包含已过滤的图像.过滤图像并获得二进制图像后,您必须单击"c"以找到轮廓,然后单击"h"以找到凸包.过滤图像并执行形态学操作不是问题.主要问题是找到轮廓.

I am trying to write a program that can create a polygon(of any number of sides) around a certain object and find the polygon's centroid . For this purpose, I opted to use the convexhull and findContours functions. In the following program, you get two windows. One window contains the trackbars for changing the HSV and morphogical values of the filter and the other contains the filtered image. After filtering the image and obtaining a binary image, you have to click 'c' to find the contours and then 'h' to find the convexhull. Filtering the image and performing morphological operations isn't a problem. The main problem is finding contours.

#include<iostream>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\core\core.hpp>
#include<opencv2\video\background_segm.hpp>
#include<Windows.h>
using namespace cv;
using namespace std;

//functions prototypes
void on_trackbar(int, void*);
void createTrackbars();
void showimgcontours(Mat &threshedimg, Mat &original);
void toggle(int key);
void morphit(Mat &img);
void blurthresh(Mat &img);

//function prototypes ends here

//boolean toggles

bool domorph = false;
bool doblurthresh = false;
bool showchangedframe = false;
bool showcontours = false;
bool showhull = false;

//boolean toggles end


int H_MIN = 0;
int H_MAX = 255;
int S_MIN = 0;
int S_MAX = 255;
int V_MIN = 0;
int V_MAX = 255;

int kerode = 1;
int kdilate = 1;
int kblur = 1;
int threshval = 0;


int main(void)
{
    createTrackbars();
    on_trackbar(0, 0);

    Mat frame, hsvframe, rangeframe;
    int key;
    VideoCapture cap(0);
    while ((key = waitKey(30)) != 27)
    {
        toggle(key);
        cap >> frame;
        flip(frame, frame, 180);
        cvtColor(frame, hsvframe, COLOR_BGR2HSV);

        inRange(hsvframe, Scalar(H_MIN, S_MIN, V_MIN), Scalar(H_MAX, S_MAX,    V_MAX), rangeframe);

        if (domorph)
            morphit(rangeframe);

        if (doblurthresh)
            blurthresh(rangeframe);

        if (showcontours)
            showimgcontours(rangeframe, frame);

        if (showchangedframe)
            imshow("Camera", frame);
        else
            imshow("Camera", rangeframe);

    }

}


void on_trackbar(int, void*)
{//This function gets called whenever a
 // trackbar position is changed
    if (kerode == 0)
        kerode = 1;
    if (kdilate == 0)
        kdilate = 1;
    if (kblur == 0)
        kblur = 1;
}
void createTrackbars()
{
    String trackbarWindowName = "TrackBars";
    namedWindow(trackbarWindowName, WINDOW_NORMAL);
    createTrackbar("H_MIN", trackbarWindowName, &H_MIN, H_MAX, on_trackbar);
    createTrackbar("H_MAX", trackbarWindowName, &H_MAX, H_MAX, on_trackbar);
    createTrackbar("S_MIN", trackbarWindowName, &S_MIN, S_MAX, on_trackbar);
    createTrackbar("S_MAX", trackbarWindowName, &S_MAX, S_MAX, on_trackbar);
    createTrackbar("V_MIN", trackbarWindowName, &V_MIN, V_MAX, on_trackbar);
    createTrackbar("V_MAX", trackbarWindowName, &V_MAX, V_MAX, on_trackbar);
    createTrackbar("Erode", trackbarWindowName, &kerode, 31, on_trackbar);
    createTrackbar("Dilate", trackbarWindowName, &kdilate, 31, on_trackbar);
    createTrackbar("Blur", trackbarWindowName, &kblur, 255, on_trackbar);
    createTrackbar("Thresh", trackbarWindowName, &threshval, 255,    on_trackbar);

}

void morphit(Mat &img)
{
    erode(img, img, getStructuringElement(MORPH_RECT, Size(kerode, kerode)));
    dilate(img, img, getStructuringElement(MORPH_RECT, Size(kdilate, kdilate)));
}
void blurthresh(Mat &img)
{
    //medianBlur(img,img,kblur%2+3+kblur);
    blur(img, img, Size(kblur, kblur), Point(-1, -1), BORDER_DEFAULT);
    threshold(img, img, threshval, 255, THRESH_BINARY_INV);
}
void toggle(int key)
{

    //toggle line start
    if (key == 'm')
        domorph = !domorph;
    if (key == 'b')
        doblurthresh = !doblurthresh;
    if (key == 'r')
        showchangedframe = !showchangedframe;
    if (key == 'c')
        showcontours = !showcontours;
    if (key == 'h')
        showhull = !showhull;
    //toggle line end
}

void showimgcontours(Mat &threshedimg, Mat &original)
{
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    int largest_area = 0;
    int largest_contour_index = 0;

    findContours(threshedimg, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

    vector<vector<Point> >hull(contours.size());

    //find a hull for each contour
    for (int i = 0; i < contours.size(); i++)
    {
        convexHull(Mat(contours[i]), hull[i], false);
    }

    //this will find the largest contour
    for (int i = 0; i< contours.size(); i++) // iterate through each contour. 
    {
        double a = contourArea(contours[i], false);  //  Find the area of each contour
        if (a>largest_area)
        {
            largest_area = a;
            largest_contour_index = i;                //Store the index of   the largest contour
        }

    }
    //search for the largest contour has end

    if (contours.size() > 0)
    {
        drawContours(original, contours, largest_contour_index, CV_RGB(0, 255, 0), 2, 8, hierarchy);
        //if you want to show every contour, use the following
        //drawContours(original,-1, CV_RGB(0, 255, 0), 2, 8, hierarchy);
        if (showhull)
            drawContours(original, hull, largest_contour_index, CV_RGB(0, 0, 255), 2, 8, hierarchy);
        //if you want to show every hull(s), use the following
        //drawContours(original,-1, CV_RGB(0, 255, 0), 2, 8, hierarchy);
    }
}

问题在于,每当我尝试运行findcontours()函数时,它总是会在矢量类模板(在void Tidy()中的某个位置)上触发一个断点.弹出对话框,并显示以下消息:

The problem is that it always triggers a breakpoint in the vector class templates(somewhere in void Tidy()) whenever I try to run the findcontours() function. A dialog box pops up and displays the following message:

"ConsoleApplication2.exe中0x00007FF9374CD328(ucrtbase.dll)的未处理异常:将无效参数传递给认为无效参数致命的函数."

"Unhandled exception at 0x00007FF9374CD328 (ucrtbase.dll) in ConsoleApplication2.exe: An invalid parameter was passed to a function that considers invalid parameters fatal."

然后,对话框将我重定向到向量类模板,并在下面的第7行中显示一个断点.

Then the dialog box redirects me to the vector class templates and displays a breakpoint in the 7th line of the below segment.

void _Tidy()
    {   // free all storage
    if (this->_Myfirst() != pointer())
        {   // something to free, destroy and deallocate it
        this->_Orphan_all();
        _Destroy(this->_Myfirst(), this->_Mylast());
        this->_Getal().deallocate(this->_Myfirst(),
            this->_Myend() - this->_Myfirst());
        this->_Myfirst() = pointer();
        this->_Mylast() = pointer();
        this->_Myend() = pointer();
        }
    }

findContours函数或向量类或完全不同的东西有问题吗?

Is this a problem with the findContours function or the vector class or something completely different?

推荐答案

这可能是一个简单的链接问题.

It is probably a simple linking problem.

如果在 Debug 模式下进行编译,则链接程序需要输入变量opencv_world310d.lib而不是opencv_world310.lib(请注意点前的d).

If you are compiling in Debug mode, the linker needs the Input Variables opencv_world310d.lib not the opencv_world310.lib (notice the d before the dot).

仅当编译发布模式时,才需要输入opencv_world310.lib.

Only when you compile the Release mode you have to type in the opencv_world310.lib.

我有同样的问题.花了我一天时间才能弄清楚.

I had the same problem. Cost me a day to figure out.

这篇关于Opencv 3.0.0,C ++,Visual Studio 2015-查找轮廓和ConvexHull时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆