gpt4 book ai didi

c++ - 用于查找轮廓 : vector deallocation issue 的 OpenCV 示例代码

转载 作者:IT王子 更新时间:2023-10-28 23:32:32 26 4
gpt4 key购买 nike

我正在尝试开始使用 OpenCV 2.4.2 中的轮廓检测。为此,我为 OpenCV 建立了一个项目,并复制了 documentation 中的整个示例代码。 .供以后引用,这里是代码:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

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 )
{
/// 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 );

contours.clear(); // Error!!
}

它在 Visual Studio 11 RC (Windows 7 SP1) 中编译良好,但在 thresh_callback 末尾出现错误功能。这是堆栈跟踪:

msvcr110d.dll!_CrtIsValidHeapPointer(const void * pUserData) Line 2036
msvcr110d.dll!_free_dbg_nolock(void * pUserData, int nBlockUse) Line 1322
msvcr110d.dll!_free_dbg(void * pUserData, int nBlockUse) Line 1265
msvcr110d.dll!operator delete(void * pUserData) Line 54
std::allocator<cv::Point_<int> >::deallocate(cv::Point_<int> * _Ptr, unsigned int __formal) Line 586
std::_Wrap_alloc<std::allocator<cv::Point_<int> > >::deallocate(cv::Point_<int> * _Ptr, unsigned int _Count) Line 888
std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > >::_Tidy() Line 1542
std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > >::~vector<cv::Point_<int>,std::allocator<cv::Point_<int> > >() Line 901
std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > >::`scalar deleting destructor'(unsigned int)
std::allocator<std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > >::destroy<std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > >(std::<cv::Point_<int>,std::allocator<cv::Point_<int> > > * _Ptr) Line 624
std::allocator_traits<std::allocator<std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > > >::destroy<std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > >(std::allocator<std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > > & _Al, std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > * _Ptr)758
std::_Wrap_alloc<std::allocator<std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > > >::destroy<std::vector<cv::Point_<int>,std::allocator<cv::Poin> > > >(std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > * _Ptr) Line 909
std::_Destroy_range<std::_Wrap_alloc<std::allocator<std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > > > >(std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > * _First, std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > * _Last, std::_Wrap_alloc<std::allocator<std::vector<cv::Point_<int>,std::allocator<cv::<int> > > > > & _Al, std::_Nonscalar_ptr_iterator_tag __formal) Line 89
std::_Destroy_range<std::_Wrap_alloc<std::allocator<std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > > > >(std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > * _First, std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > * _Last, std::_Wrap_alloc<std::allocator<std::vector<cv::Point_<int>,std::allocator<cv::<int> > > > > & _Al) Line 80
std::vector<std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > >,std::allocator<std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> >::_Destroy(std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > * _First, std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > * _Last) Line 1480
std::vector<std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > >,std::allocator<std::vector<cv::Point_<int>,std::allocator<cv::Point_<int> > > > >::c Line 1416
thresh_callback(int __formal, void * __formal) Line 143
opencv_highgui242d.dll!icvUpdateTrackbar(CvTrackbar * trackbar, int pos) Line 1938
opencv_highgui242d.dll!HGToolbarProc(HWND__ * hwnd, unsigned int uMsg, unsigned int wParam, long lParam) Line 1982

(请注意,实际上我有一个示例代码的略微修改版本,添加了一些 printf-s 和一个 contours.clear() 调用,在第 143 行,它触发了 vector 释放 [这将在函数的结尾] 这似乎是问题的根源。完全相同的问题也出现在确切的示例代码中。)

问题似乎与 contours 的释放有关 vector 。如果我尝试调用 contours.clear(),也会发生同样的情况.我在 VS 调试器中检查了变量的内容,但据我所知并没有什么异常。

我试图在不再需要 vector 时立即清除它(在 for 循环之后),但它没有帮助。我还尝试将 Platform Toolset 切换到 Visual Studio 10,它甚至不会编译出超出我能力范围的错误消息:

error C1083: Cannot open include file: 'SDKDDKVer.h': No such file or directory (C:\<project path>\targetver.h) (Line 8, Column 1)
IntelliSense: cannot open source file "SDKDDKVer.h" (C:\<project path>\targetver.h) (Line 8, Column 1)
IntelliSense: cannot open source file "windows.h" (C:\<opencv path>\build\include\opencv2\core\operations.hpp (Line 83, Column 3)

任何帮助将不胜感激。请注意,我不是 C++ 程序员:我对 C++ 编程和原生编程的知识很少,甚至更没有经验。

编辑:原来调试器显示错误的行作为错误源。我应该在调用堆栈中看到问题出在 vector > 上。所以有问题的 vector 是contours ,而不是 hierarchy !

编辑 #2:我还尝试使用创建 vector<vector<cv::Point> > 的最小代码来重现该问题。 ,将一些项目放入其中,然后将其清除,但我无法重现该问题。

最佳答案

您正在 Debug模式下构建应用程序并链接到多线程调试 DLL CRT。你知道 OpenCV DLL 链接到哪个 CRT 吗?如果它与静态 CRT 链接,它将用从单独堆分配的数据填充 vector ,这会导致您正在使用的调试 CRT 中出现断言。

如果您在 Release模式下构建应用程序,您应该不会再看到断言,但最终可能会导致内存泄漏。最好的办法是确保您的应用程序和 OpenCV DLL 都链接到同一个多线程 DLL CRT。

编辑:如果您无法重建 OpenCV 以使用与您的应用程序相同的 CRT,您可以尝试通过修改应用程序 list 来告诉链接器为您的应用程序使用与 OpenCV 相同的 CRT 版本。见 How to Enforce C++ compiler to use specific CRT version?有关如何执行此操作的更多信息。

关于c++ - 用于查找轮廓 : vector deallocation issue 的 OpenCV 示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11677118/

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