11

OpenCV2.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ループの後)すぐにクリアしようとしましたが、役に立ちませんでした。また、PlatformToolsetをVisualStudio 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++プログラミングとネイティブプログラミング全般の経験はさらに少ないです。

編集:デバッガーがエラーの原因として間違った行を表示したことが判明しました。問題がベクトル>にあることをコールスタックで確認する必要がありました。したがって、問題のあるベクトルはcontours、ではなくhierarchyです。

編集#2vector<vector<cv::Point> > :また、を作成し、いくつかのアイテムを入れてからクリアする最小限のコードで問題を再現しようとしましたが、問題を再現できませんでした。

4

3 に答える 3

17

アプリケーションをデバッグ モードでビルドし、マルチスレッド デバッグ DLL CRT にリンクしています。OpenCV DLL がリンクされている CRT を知っていますか? 静的 CRT に対してリンクされている場合、別のヒープから割り当てられたデータでベクターがいっぱいになり、使用しているデバッグ CRT でアサーションが発生します。

アプリケーションをリリース モードでビルドすると、アサートは表示されなくなりますが、メモリ リークが発生する可能性があります。最善の方法は、アプリケーションと OpenCV DLL の両方が同じマルチスレッド DLL CRT に対してリンクされていることを確認することです。

編集: アプリケーションと同じ CRT を使用するように OpenCV を再構築できない場合は、アプリケーション マニフェストを変更して、リンカーにアプリケーションの OpenCV と同じ CRT バージョンを使用するように指示してみてください。特定の CRT バージョンを使用するように C++ コンパイラを強制する方法を参照してください。その方法の詳細については。

于 2012-07-26T20:48:50.180 に答える
0

ソリューション オプションに移動し、そこに c-runtime (CRT) 設定があり、リンク (上記の CRT) を確認します...プロジェクトが (一度) VS10 で作成されていて、新しいバージョンを使用している場合は、編集するだけですリンクは 10 ではなく、11 または 12 ..

于 2014-10-21T14:13:18.493 に答える