ShaperCV プログラミングでのトラブル
OpenCV を C# から利用したら,楽々 GUI アプリを作れるかもしらん - BiBoLoGで構築した環境の元,OpenCV を C# から利用してこんな GUI アプリを作りたい - BiBoLoGで触れたように avi2still というプロジェクトを組んでいた.うまく実行できないというトラブルが発生したから,簡単なサンプルを動かしておこうと思った.
- 新規プロジェクトを Windows フォームアプリケーションで作る.名前は ShaperCVTest とした
- VC++ とは勝手が違うなあと思いつつ,名前を付けて保存する
- プロジェクトメニューから参照の追加を選ぶ
- openCVWrapper.dll を追加する
- プロジェクトメニューから既存項目の追加を選ぶ
- cv.dll と highguisharper.dll を追加する
- cv.dll と highguisharper.dll のプロパティで,「出力ディレクトリにコピー」を「新しい場合はコピーする」とする
- ShaperCV を利用したい .cs ファイル中で using ShaperCV; する
よし,Form1.cs のデザイナを出し,でっかい buttun を作り,ダブルクリックする.button1_Click() を書く.まず Form1.cs に using ShaperCV; を追加する.そしてみんな大好き千原研からソースコードをいただく.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using SharperCV; namespace ShaperCVTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //SharperCVによるカメラキャプチャ CvWindow windowCapture; //ウインドウを取り扱うためのクラス CvCapture capture; //カメラキャプチャを扱うためのクラス CvImage imgCapture; //画像処理全般を扱うためのクラス CvSize sizeCapture; //OpenCVにおいて,サイズを扱うクラス(構造体のように扱っている) int key = 0; //SharperCVによるカメラキャプチャの準備 //キャプチャ画像表示ウインドウを定義 windowCapture = new CvWindow("Captured Image by Cam", true); //キャプチャ用クラスを定義 capture = new CvCapture(-1); //ウインドウのサイズを変数に入れておく sizeCapture.width = 320; sizeCapture.height = 240; //キャプチャした画像を保持するクラスを定義 imgCapture = new CvImage(sizeCapture, BitDepths.IPL_DEPTH_8U, 3); do { //カメラに画像を問い合わせます if ((imgCapture = capture.QueryFrame()) == null) break; //'c'を押したらtest.jpgを保存します if (key == 'c') imgCapture.SaveImage(".\\test.jpg"); //CvImageクラスの画像をウインドウに渡します(描画します) //ShowImageに相当 windowCapture.Image = imgCapture; //cvWaitKeyと同義です key = CvWindow.WaitKey(1); } while (key != 'q'); //'q'が押されるまで続く //ウインドウをクローズ windowCapture.Close(); } } }
これでビルドして,実行してみる.
よしよし,トラブル状況の再現ができたぞ.32行目 CvWindow() でこけた.
DllNotFoundExeption はハンドルされませんでした。
DLL 'highGUIsharper.dll' を読み込めません: メモリ ロケーションへのアクセスが無効です。 (HRESULT からの例外: 0x800703E6)
これをどう解決するか../bin/Debug/ にはキチンと3つの DLL がコピーされている.NotFound はねーだろひどいよ.
http://msdn.microsoft.com/ja-jp/library/ms241899.aspx例外のトラブルシューティング : System.DllNotFoundException
この例外は、DLL のインポートで指定された DLL が見つからない場合にスローされます。
さて,先人の記録を探しに行こう.
http://icools-csharpblog.blogspot.com/2007/07/sharpercv.html
excite-webtl.jpと対比して見てみる.
- highGUIsharper.dll について NotFound
- CvImage() だけならなんともないらしい
- Haar サンプルを動かしてみたけど highGUIsharper.dll でこける.だからこれを使わないで動かす方法をとった
SharperCV dll problems
DLL の Copy if newer で解決してる.
Andreas JohanssonMVP, Moderator - Posted on 2006年5月26日 7:36:32
One solution will be to have all SharperCV dll files in the same directory as your executable. That is the Debug directory if you are debugging.
I downloaded SharperCV and did the following.
Loaded up the SampleFaceFinder project
Changed the reference to openCVWrapper to be to the openCVWrapper.dll in C:\SharperCV\bin\
Added all dll files from C:\SharperCV\bin to the project, as files.
In the solution explorer, for every DLL, I changed the "Copy to output directory" property to be "Copy if newer"Marco Rucci - Posted on 2006年5月26日 15:53:46
Ok, now i can run all that i need, and now i'm ready to new problems.
Thanks a lot for your interest
俺のは解決してねーんだけど.おっと,別ドメインに同内容がある.こちらは Google で翻訳できた.
dfltweb1.onamae.com – このドメインはお名前.comで取得されています。
Comment from: Craig [Visitor]
I tried running within vista and with the visual studio debugger I am getting errors saying highguiSharper.dll cannot be loaded.
03/06/08 @ 19:05
このコメントに引っかかった.解決法は示されていない.
GitHub - shimat/opencvsharp: .NET Framework wrapper for OpenCV
というのを見つけた.開発は実行中.切り替えるか?
最後だけど,俺がこけた行を示す.
capture = new CvCapture(avifile);