2012-07-26 11 views
11

OpenCV 2.4.2で輪郭検出を開始しようとしています。この目的のために、OpenCVのプロジェクトを立ち上げ、documentationにあるサンプルコード全体をコピーしました。今後の参考のために、ここでのコードは次のとおりです。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を加え、そしてライン143であるcontours.clear()コール、およびベクターをトリガすることに注意してください割り当て解除[関数の終わりに自動的に発生する]は問題の原因と思われます。正確なサンプルコードでも同様の問題が発生します)。

この問題は、 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> >を作成し、最小限のコードで問題を再現してみました、それをクリアして、その中にいくつかのアイテムを置きますが、私は問題を再現することができませんでした。

+0

は、あなたは正しい、オブジェクトの輪郭をクリアする必要がないことを知っていますか? thresh_callback関数呼び出しの最後にガベージコレクションされます。あなたのコードはこの行なしで動作しますか? –

+1

@RégisB.:私は私の質問で、ラインなしで同じエラーが発生したと言いました。私が明示的に 'contours.clear'と呼ぶ唯一の理由は、どこに問題が発生しているのかを強調することです。 – ShdNx

+0

'すべての設定 - >一般 - >プラットフォームツールセット - > Visual Studio 2010(v100)'が私にとって問題を解決しました。 – vkalit

答えて

16

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

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

EDIT:アプリケーションと同じCRTを使用するようにOpenCVを再構築できない場合は、アプリケーションマニフェストを変更してアプリケーションのOpenCVと同じCRTバージョンを使用するようにリンカに指示してください。これを行う方法の詳細については、How to Enforce C++ compiler to use specific CRT version?を参照してください。

+0

私はOpenCVのソースコードをダウンロードし、それを私のマシン上に構築しました。私はそれがリンクされているか分からない - 私はプリセットの設定を変更していない。この情報はどこで見ることができますか? リリースモードでアプリケーションをコンパイルして実行しようとしましたが、問題は解決しません。 – ShdNx

+0

依存関係ウォーカー(http://www.dependencywalker.com/)は、依存関係を持つDLLを特定のDLL/EXEに対して表示することができます。 MSVC *のような名前のものが依存関係ツリーにポップアップすると、それをあなたのアプリケーションが使っているものと比較することができます。そのような依存関係が見つからない場合、DLLはCRTに対して静的にリンクされている可能性があります。 – Michael

+0

また、OpenCVをビルドする際には、OpenCVをビルドするときにプロジェクトプロパティに入り、アプリケーションと同じCRTを使用していることを確認することもできます(コード生成 - >ランタイムライブラリ)。 – Michael

0

あなたのプロジェクトがVS10で作成されていて、より新しいものを使用している場合は、ソリューションオプション、cランタイム(CRT)の設定、リンク(CRT)バージョンではなく、10、11、12のリンクを編集するだけです。

0

(例として)システムパスに追加します。 H: "H:\ソース\ opencv_v2_4_13_2 \出力" CMakeのから使用されているデバッグ

ディレクトリが

\ \ソース\ opencv_v2_4_13_2 \出力\ビンをVisual Studio 14(2015)opencvプロジェクトの保存先ディレクトリとして指定します。

あなたのプロジェクトは、OpenCVのDLLファイルを使用していますが、見つかりません。 opencv_core2413d.dll

関連する問題