gpt4 book ai didi

c++ - Opencv 3.0.0、C++、Visual Studio 2015 - 查找轮廓和 ConvexHull 时出错

转载 作者:太空宇宙 更新时间:2023-11-03 22:00:38 31 4
gpt4 key购买 nike

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

#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() 函数时,它总是会在 vector 类模板中触发断点(void Tidy() 中的某处)。弹出对话框并显示以下消息:

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

http://i60.tinypic.com/1zvdf1t.png

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

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();
}
}

http://i61.tinypic.com/2yx0p37.png

这是 findContours 函数或 vector 类的问题还是完全不同的问题?

最佳答案

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

如果您在调试模式下编译,链接器需要输入变量opencv_world310d.lib而不是opencv_world310.lib(注意d 点之前)。

只有在编译Release模式时才需要输入opencv_world310.lib

我遇到了同样的问题。花了我一天的时间才弄清楚。

关于c++ - Opencv 3.0.0、C++、Visual Studio 2015 - 查找轮廓和 ConvexHull 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32160401/

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