问题 用于查找轮廓的OpenCV示例代码:向量释放问题


我正在尝试在OpenCV 2.4.2中开始使用轮廓检测​​。为此,我为OpenCV设置了一个项目并复制了在中找到的整个示例代码 文件。供将来参考,以下是代码:

#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和a contours.clear() 调用,在143行,并触发向量解除分配[将在函数结束时自动发生],这似乎是问题的根源。同样的问题也出现在确切的示例代码中。)

问题似乎与解除分配有关 contours 向量。如果我试着打电话也会发生同样的情况 contours.clear()。我已经在VS调试器中检查了变量的内容,但据我所知,并没有什么不寻常之处。

我试图在不再需要时立即清除载体(之后) for 循环),但它没有帮助。我还试图将平台工具集切换到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>。所以有问题的载体是 contours不是 hierarchy

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


1224
2017-07-26 20:00


起源

你知道没有必要清除轮廓对象,对吧?它将在thresh_callback函数调用结束时进行垃圾收集。你的代码没有这条线吗? - Régis B.
@RégisB。:我相信我在我的问题中提到过,没有这条线就会发生同样的错误。我明确打电话的唯一原因 contours.clear 这是为了突出问题发生的位置。 - ShdNx
All Configuratins -> General -> Platform Toolset -> Visual Studio 2010 (v100) 解决了我的问题。 - vkalit


答案:


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

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

编辑:如果您无法重建OpenCV以使用与您的应用程序相同的CRT,您可以尝试通过修改应用程序清单告诉链接器为您的应用程序使用与OpenCV相同的CRT版本。看到 如何强制C ++编译器使用特定的CRT版本? 有关如何执行此操作的详细信息。


16
2017-07-26 20:48



我下载了OpenCV源代码,并在我的机器上构建了它。我不知道它与之相关联 - 我没有更改预设设置。我在哪里可以看到这些信息?我试图在发布模式下编译和运行我的应用程序,但问题仍然存在。 - ShdNx
依赖步行者(dependencywalker.com)可以向您显示给定的DLL / EXE它依赖的DLL。如果在依赖关系树中弹出了类似MSVC *的名称,则可以将其与应用程序使用的内容进行比较。如果没有找到这样的依赖关系,则DLL可能静态地与CRT链接。 - Michael
另外,既然你提到自己正在构建OpenCV(也使用visual studio?),你可以在构建OpenCV时进入项目属性,并确保使用与应用程序相同的CRT(代码生成 - >运行时)图书馆)。 - Michael
我检查了我的exe和OpenCV dll的依赖项,结果发现当OpenCV使用MSVCP100D.DLL时,我的exe使用了MSVCP110D.DLL。我想这来自于为VS 10构建的OpenCV(它使用CMake),而我的exe是在VS 11中构建的。我检查了代码生成 - >运行时库选项,但是没有选项可以使用早期的运行时库,我可以只是在多线程(Debug)之间做出决定? (DLL)? - ShdNx
非常感谢您的帮助。我通过使用VS 2010构建我的应用程序来修复此问题,因此它默认使用MSVCP100D.DLL。 - ShdNx


进入你的解决方案选项,那里有c-runtime(CRT)设置并检查链接(如上所述的CRT)...如果你的项目是/是(曾经)用VS10创建并且你使用的是更新的版本,只需编辑链接不是10,而是11或12 ..


0
2017-10-21 14:13





添加到系统路径(作为示例): H:\来源\ opencv_v2_4_13_2 \输出\ BIN \调试

目录: “H:\源\ opencv_v2_4_13_2 \输出” 用于Visual Studio 14(2015)opencv项目的CMake作为目标目录。

您的项目使用opencv dll文件,但找不到: opencv_core2413d.dll


0
2018-02-07 22:29