簡體   English   中英

用於查找輪廓的OpenCV示例代碼:向量釋放問題

[英]OpenCV example code for find contours: vector deallocation issue

我正在嘗試在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和一個contours.clear()調用,它位於第143行,它觸發了向量釋放[會自動發生在函數的結尾]似乎是問題的根源。同樣的問題也出現在確切的示例代碼中。)

問題似乎在於contours矢量的重新分配。 如果我嘗試調用contours.clear()也會出現同樣的情況。 我已經在VS調試器中檢查了變量的內容,但據我所知,沒有什么不尋常的。

我試圖在不再需要時(在for循環之后)清除矢量,但它沒有幫助。 我還試圖將Platform Toolset切換到Visual Studio 10,而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> >的最小代碼重現問題,將一些項放入其中,然后清除它,但我無法重現問題。

您正在以調試模式構建應用程序並鏈接到多線程調試DLL CRT。 你知道OpenCV DLL鏈接到哪個CRT嗎? 如果它與靜態CRT鏈接,它將使用從單獨的堆分配的數據填充向量,這會導致您正在使用的Debug CRT中的斷言。

如果您在發布模式下構建應用程序,則不應再看到斷言,但最終可能會泄漏內存。 最好的辦法是確保您的應用程序和OpenCV DLL都鏈接到同一個多線程DLL CRT。

編輯:如果您無法重建OpenCV以使用與您的應用程序相同的CRT,您可以嘗試通過修改應用程序清單告訴鏈接器為您的應用程序使用與OpenCV相同的CRT版本。 請參閱如何強制使用C ++編譯器以使用特定的CRT版本? 有關如何執行此操作的詳細信息。

進入你的解決方案選項,那里有c-runtime(CRT)設置並檢查鏈接(如上所述的CRT)...如果你的項目是/是(曾經)用VS10創建並且你使用的是更新的版本,只需編輯鏈接不是10,而是11或12 ..

添加到系統路徑(作為示例):H:\\ Source \\ opencv_v2_4_13_2 \\ output \\ bin \\ Debug

其中目錄:“H:\\ Source \\ opencv_v2_4_13_2 \\ output”用於CMake作為visual Studio 14(2015)opencv項目的目標目錄。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM