| 1 | #pragma once
|
|---|
| 2 |
|
|---|
| 3 | #include <map>
|
|---|
| 4 | #include "CvBlob_.h"
|
|---|
| 5 | #include "RenderBlobsMode.h"
|
|---|
| 6 |
|
|---|
| 7 | namespace KwsmLab{
|
|---|
| 8 | namespace OpenCvSharp{
|
|---|
| 9 | namespace Blob
|
|---|
| 10 | {
|
|---|
| 11 | using namespace System::Collections::Generic;
|
|---|
| 12 |
|
|---|
| 13 | /// <summary>
|
|---|
| 14 | /// members of CvBlobs::Enumerator
|
|---|
| 15 | /// </summary>
|
|---|
| 16 | private struct CvBlobsEnumeratorVariables
|
|---|
| 17 | {
|
|---|
| 18 | __CvBlobs* map;
|
|---|
| 19 | __CvBlobs::iterator it;
|
|---|
| 20 | };
|
|---|
| 21 |
|
|---|
| 22 | /// <summary>
|
|---|
| 23 | /// List of blobs.
|
|---|
| 24 | /// A map is used to access each blob from its label number.
|
|---|
| 25 | /// </summary>
|
|---|
| 26 | public ref class CvBlobs : public DisposableObject, public IDictionary<CvLabel, CvBlob^>
|
|---|
| 27 | {
|
|---|
| 28 | private:
|
|---|
| 29 | /// <summary>
|
|---|
| 30 | /// CvBlobs pointer (std::map<CvLabel,CvBlob *>*)
|
|---|
| 31 | /// </summary>
|
|---|
| 32 | __CvBlobs* ptr;
|
|---|
| 33 |
|
|---|
| 34 | internal:
|
|---|
| 35 | /// <summary>
|
|---|
| 36 | /// CvBlobs pointer (std::map<CvLabel,CvBlob *>*)
|
|---|
| 37 | /// </summary>
|
|---|
| 38 | property __CvBlobs* Ptr
|
|---|
| 39 | {
|
|---|
| 40 | __CvBlobs* get(){ return ptr; }
|
|---|
| 41 | void set(__CvBlobs* value){ ptr = value; }
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | public:
|
|---|
| 45 | CvBlobs();
|
|---|
| 46 | !CvBlobs();
|
|---|
| 47 | ~CvBlobs();
|
|---|
| 48 |
|
|---|
| 49 | UInt32 Label(IplImage^ img, IplImage^ imgOut);
|
|---|
| 50 | void FilterLabels(IplImage^ imgIn, IplImage^ imgOut);
|
|---|
| 51 | CvLabel GreaterBlob();
|
|---|
| 52 | void Release();
|
|---|
| 53 | void RenderBlobs(IplImage^ imgLabel, IplImage^ imgSource, IplImage^ imgDest);
|
|---|
| 54 | void RenderBlobs(IplImage^ imgLabel, IplImage^ imgSource, IplImage^ imgDest, RenderBlobsMode mode);
|
|---|
| 55 | void RenderBlobs(IplImage^ imgLabel, IplImage^ imgSource, IplImage^ imgDest, RenderBlobsMode mode, Double alpha);
|
|---|
| 56 | void FilterByArea(UInt32 minArea, UInt32 maxArea);
|
|---|
| 57 |
|
|---|
| 58 | #pragma region Implementation of IDictionary
|
|---|
| 59 | #pragma region IDictionary<CvLabel,CvBlob^> Members
|
|---|
| 60 | /// <summary>
|
|---|
| 61 | /// 指定したキーおよび値を持つ要素を System.Collections.Generic.IDictionary<TKey,TValue> に追加します。
|
|---|
| 62 | /// </summary>
|
|---|
| 63 | /// <param name="key">追加する要素のキーとして使用するオブジェクト。</param>
|
|---|
| 64 | /// <param name="value">追加する要素の値として使用するオブジェクト。</param>
|
|---|
| 65 | virtual void Add(CvLabel key, CvBlob^ value)
|
|---|
| 66 | {
|
|---|
| 67 | if(value == nullptr)
|
|---|
| 68 | {
|
|---|
| 69 | throw gcnew ArgumentNullException("value");
|
|---|
| 70 | }
|
|---|
| 71 | ptr->insert( __CvBlobs::value_type(key, value->Ptr) );
|
|---|
| 72 | }
|
|---|
| 73 | /// <summary>
|
|---|
| 74 | /// 指定したキーの要素が System.Collections.Generic.IDictionary<TKey,TValue> に格納されているかどうかを確認します。
|
|---|
| 75 | /// </summary>
|
|---|
| 76 | /// <param name="key">System.Collections.Generic.IDictionary<TKey,TValue> 内で検索されるキー。</param>
|
|---|
| 77 | /// <returns>指定したキーを持つ要素を System.Collections.Generic.IDictionary<TKey,TValue> が保持している場合は true。それ以外の場合は false。</returns>
|
|---|
| 78 | virtual Boolean ContainsKey(CvLabel key)
|
|---|
| 79 | {
|
|---|
| 80 | return ptr->count(key) > 0;
|
|---|
| 81 | }
|
|---|
| 82 | /// <summary>
|
|---|
| 83 | /// System.Collections.Generic.IDictionary<TKey,TValue> のキーを保持している System.Collections.Generic.ICollection<T>を取得します。
|
|---|
| 84 | /// </summary>
|
|---|
| 85 | /// <returns>System.Collections.Generic.IDictionary<TKey,TValue> を実装するオブジェクトのキーを保持している System.Collections.Generic.ICollection<T>。</returns>
|
|---|
| 86 | virtual property ICollection<CvLabel>^ Keys
|
|---|
| 87 | {
|
|---|
| 88 | ICollection<CvLabel>^ get()
|
|---|
| 89 | {
|
|---|
| 90 | int size = ptr->size();
|
|---|
| 91 | array<CvLabel>^ keys = gcnew array<CvLabel>(size);
|
|---|
| 92 | int i = 0;
|
|---|
| 93 | for (__CvBlobs::iterator it = ptr->begin(); it != ptr->end(); it++, i++)
|
|---|
| 94 | {
|
|---|
| 95 | keys[i] = it->first;
|
|---|
| 96 | }
|
|---|
| 97 | return safe_cast<ICollection<CvLabel>^>(keys);
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 | /// <summary>
|
|---|
| 101 | /// 指定したキーを持つ要素を System.Collections.Generic.IDictionary<TKey,TValue> から削除します。
|
|---|
| 102 | /// </summary>
|
|---|
| 103 | /// <param name="key">削除する要素のキー。</param>
|
|---|
| 104 | /// <returns>要素が正常に削除された場合は true。それ以外の場合は false。このメソッドは、key が元の System.Collections.Generic.IDictionary<TKey,TValue> に見つからなかった場合にも false を返します。</returns>
|
|---|
| 105 | virtual Boolean Remove(CvLabel key)
|
|---|
| 106 | {
|
|---|
| 107 | if(ptr->count(key) > 0)
|
|---|
| 108 | {
|
|---|
| 109 | ptr->erase(key);
|
|---|
| 110 | return true;
|
|---|
| 111 | }
|
|---|
| 112 | else
|
|---|
| 113 | {
|
|---|
| 114 | return false;
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 | /// <summary>
|
|---|
| 118 | /// 指定したキーに関連付けられている値を取得します。
|
|---|
| 119 | /// </summary>
|
|---|
| 120 | /// <param name="key">値を取得する対象のキー。</param>
|
|---|
| 121 | /// <param name="value">このメソッドが返されるときに、キーが見つかった場合は、指定したキーに関連付けられている値。それ以外の場合は value パラメータの型に対する既定の値。このパラメータは初期化せずに渡されます。</param>
|
|---|
| 122 | /// <returns>指定したキーを持つ要素が System.Collections.Generic.IDictionary<TKey,TValue> を実装するオブジェクトに格納されている場合は true。それ以外の場合は false。</returns>
|
|---|
| 123 | virtual Boolean TryGetValue(CvLabel key, [Out] CvBlob^% value)
|
|---|
| 124 | {
|
|---|
| 125 | __CvBlobs::iterator it = ptr->find(key);
|
|---|
| 126 | if(it != ptr->end())
|
|---|
| 127 | {
|
|---|
| 128 | value = gcnew CvBlob(it->second);
|
|---|
| 129 | return true;
|
|---|
| 130 | }
|
|---|
| 131 | else
|
|---|
| 132 | {
|
|---|
| 133 | value = nullptr;
|
|---|
| 134 | return false;
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 | /// <summary>
|
|---|
| 138 | /// System.Collections.Generic.IDictionary<TKey,TValue> 内の値を格納している System.Collections.Generic.ICollection<T> を取得します。
|
|---|
| 139 | /// </summary>
|
|---|
| 140 | /// <returns>System.Collections.Generic.IDictionary<TKey,TValue> を実装するオブジェクトの値を保持している System.Collections.Generic.ICollection<T>。</returns>
|
|---|
| 141 | virtual property ICollection<CvBlob^>^ Values
|
|---|
| 142 | {
|
|---|
| 143 | ICollection<CvBlob^>^ get()
|
|---|
| 144 | {
|
|---|
| 145 | int size = ptr->size();
|
|---|
| 146 | array<CvBlob^>^ values = gcnew array<CvBlob^>(size);
|
|---|
| 147 | int i = 0;
|
|---|
| 148 | for (__CvBlobs::iterator it = ptr->begin(); it != ptr->end(); it++, i++)
|
|---|
| 149 | {
|
|---|
| 150 | values[i] = gcnew CvBlob(it->second);
|
|---|
| 151 | }
|
|---|
| 152 | return safe_cast<ICollection<CvBlob^>^>(values);
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 | /// <summary>
|
|---|
| 156 | /// 指定したキーを持つ要素を取得または設定します。
|
|---|
| 157 | /// </summary>
|
|---|
| 158 | /// <param name="key">取得または設定する要素のキー。</param>
|
|---|
| 159 | /// <returns>指定したキーを持つ要素。</returns>
|
|---|
| 160 | virtual property CvBlob^ default[CvLabel]
|
|---|
| 161 | {
|
|---|
| 162 | CvBlob^ get(CvLabel key)
|
|---|
| 163 | {
|
|---|
| 164 | return gcnew CvBlob((*ptr)[key]);
|
|---|
| 165 | }
|
|---|
| 166 | void set(CvLabel key, CvBlob^ value)
|
|---|
| 167 | {
|
|---|
| 168 | (*ptr)[key] = value->Ptr;
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| 171 | #pragma endregion
|
|---|
| 172 | #pragma region ICollection<KeyValuePair<CvLabel,CvBlob^>> Members
|
|---|
| 173 | /// <summary>
|
|---|
| 174 | /// System.Collections.Generic.ICollection<T> に項目を追加します。
|
|---|
| 175 | /// </summary>
|
|---|
| 176 | /// <param name="item">System.Collections.Generic.ICollection<T> に追加するオブジェクト。</param>
|
|---|
| 177 | virtual void Add(KeyValuePair<CvLabel, CvBlob^> item)
|
|---|
| 178 | {
|
|---|
| 179 | ptr->insert( __CvBlobs::value_type(item.Key, item.Value->Ptr) );
|
|---|
| 180 | }
|
|---|
| 181 | /// <summary>
|
|---|
| 182 | /// System.Collections.Generic.ICollection<T> からすべての項目を削除します。
|
|---|
| 183 | /// </summary>
|
|---|
| 184 | virtual void Clear()
|
|---|
| 185 | {
|
|---|
| 186 | ptr->clear();
|
|---|
| 187 | }
|
|---|
| 188 | /// <summary>
|
|---|
| 189 | /// System.Collections.Generic.ICollection<T> に特定の値が格納されているかどうかを判断します。
|
|---|
| 190 | /// </summary>
|
|---|
| 191 | /// <param name="item">System.Collections.Generic.ICollection<T> 内で検索するオブジェクト。</param>
|
|---|
| 192 | /// <returns>item が System.Collections.Generic.ICollection<T> に存在する場合は true。それ以外の場合は false。</returns>
|
|---|
| 193 | virtual bool Contains(KeyValuePair<CvLabel, CvBlob^> item)
|
|---|
| 194 | {
|
|---|
| 195 | __CvBlobs::iterator it = ptr->find(item.Key);
|
|---|
| 196 | if(it != ptr->end())
|
|---|
| 197 | {
|
|---|
| 198 | if(it->second == item.Value->Ptr)
|
|---|
| 199 | {
|
|---|
| 200 | return true;
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|
| 203 | return false;
|
|---|
| 204 | }
|
|---|
| 205 | /// <summary>
|
|---|
| 206 | /// System.Collections.Generic.ICollection<T> の要素を System.Array にコピーします。System.Array の特定のインデックスからコピーが開始されます。
|
|---|
| 207 | /// </summary>
|
|---|
| 208 | /// <param name="ary">System.Collections.Generic.ICollection<T> から要素がコピーされる 1 次元の System.Array。System.Array には、0 から始まるインデックス番号が必要です。</param>
|
|---|
| 209 | /// <param name="aryIndex">コピーの開始位置となる、array の 0 から始まるインデックス番号。</param>
|
|---|
| 210 | virtual void CopyTo(array<KeyValuePair<CvLabel, CvBlob^>>^ ary, int aryIndex)
|
|---|
| 211 | {
|
|---|
| 212 | if(ary == nullptr)
|
|---|
| 213 | throw gcnew ArgumentNullException("ary");
|
|---|
| 214 | if(ary->Length < this->Count + aryIndex)
|
|---|
| 215 | throw gcnew ArgumentException();
|
|---|
| 216 |
|
|---|
| 217 | int i = 0;
|
|---|
| 218 | for each(KeyValuePair<CvLabel, CvBlob^> item in this)
|
|---|
| 219 | {
|
|---|
| 220 | ary[i + aryIndex] = item;
|
|---|
| 221 | i++;
|
|---|
| 222 | }
|
|---|
| 223 | }
|
|---|
| 224 | /// <summary>
|
|---|
| 225 | /// System.Collections.Generic.ICollection<T> に格納されている要素の数を取得します。
|
|---|
| 226 | /// </summary>
|
|---|
| 227 | /// <returns>System.Collections.Generic.ICollection<T> に格納されている要素の数。</returns>
|
|---|
| 228 | virtual property Int32 Count
|
|---|
| 229 | {
|
|---|
| 230 | Int32 get()
|
|---|
| 231 | {
|
|---|
| 232 | return ptr->size();
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 | /// <summary>
|
|---|
| 236 | /// System.Collections.Generic.ICollection<T> が読み取り専用かどうかを示す値を取得します。
|
|---|
| 237 | /// </summary>
|
|---|
| 238 | /// <returns>System.Collections.Generic.ICollection<T> が読み取り専用の場合は true。それ以外の場合は false。</returns>
|
|---|
| 239 | virtual property Boolean IsReadOnly
|
|---|
| 240 | {
|
|---|
| 241 | Boolean get()
|
|---|
| 242 | {
|
|---|
| 243 | return false;
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| 246 | /// <summary>
|
|---|
| 247 | /// System.Collections.Generic.ICollection<T> 内で最初に見つかった特定のオブジェクトを削除します。
|
|---|
| 248 | /// </summary>
|
|---|
| 249 | /// <param name="item">System.Collections.Generic.ICollection<T> から削除するオブジェクト。</param>
|
|---|
| 250 | /// <returns>item が System.Collections.Generic.ICollection<T> から正常に削除された場合は true。それ以外の場合は
|
|---|
| 251 | /// false。このメソッドは、item が元の System.Collections.Generic.ICollection<T> に見つからない場合にも
|
|---|
| 252 | /// false を返します。</returns>
|
|---|
| 253 | virtual Boolean Remove(KeyValuePair<CvLabel, CvBlob^> item)
|
|---|
| 254 | {
|
|---|
| 255 | CvLabel key = item.Key;
|
|---|
| 256 | __CvBlob* value = (item.Value)->Ptr;
|
|---|
| 257 | for (__CvBlobs::iterator it = ptr->begin(); it != ptr->end(); it++)
|
|---|
| 258 | {
|
|---|
| 259 | if(it->first == key && it->second == value)
|
|---|
| 260 | {
|
|---|
| 261 | return ptr->erase(key) > 0;
|
|---|
| 262 | }
|
|---|
| 263 | }
|
|---|
| 264 | return false;
|
|---|
| 265 | }
|
|---|
| 266 | #pragma endregion
|
|---|
| 267 | #pragma region IEnumerator<KeyValuePair<CvLabel,CvBlob^>> Members
|
|---|
| 268 | #ifdef LANG_JP
|
|---|
| 269 | /// <summary>
|
|---|
| 270 | /// コレクションを反復処理する列挙子を返します。
|
|---|
| 271 | /// </summary>
|
|---|
| 272 | #else
|
|---|
| 273 | /// <summary>
|
|---|
| 274 | /// Returns an enumerator that iterates through a collection.
|
|---|
| 275 | /// </summary>
|
|---|
| 276 | #endif
|
|---|
| 277 | virtual IEnumerator<KeyValuePair<CvLabel, CvBlob^>>^ GetEnumerator() = IEnumerable<KeyValuePair<CvLabel, CvBlob^>>::GetEnumerator
|
|---|
| 278 | {
|
|---|
| 279 | return gcnew Enumerator(ptr);
|
|---|
| 280 | }
|
|---|
| 281 | protected:
|
|---|
| 282 | #ifdef LANG_JP
|
|---|
| 283 | /// <summary>
|
|---|
| 284 | /// コレクションを反復処理する列挙子を返します。
|
|---|
| 285 | /// </summary>
|
|---|
| 286 | #else
|
|---|
| 287 | /// <summary>
|
|---|
| 288 | /// Returns an enumerator that iterates through a collection.
|
|---|
| 289 | /// </summary>
|
|---|
| 290 | #endif
|
|---|
| 291 | virtual System::Collections::IEnumerator^ GetEnumeratorNonGeneric() = System::Collections::IEnumerable::GetEnumerator
|
|---|
| 292 | {
|
|---|
| 293 | return GetEnumerator();
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | /// <summary>
|
|---|
| 297 | /// implementation of IEnumerator
|
|---|
| 298 | /// </summary>
|
|---|
| 299 | ref class Enumerator : IEnumerator<KeyValuePair<CvLabel, CvBlob^>>
|
|---|
| 300 | {
|
|---|
| 301 | private:
|
|---|
| 302 | CvBlobsEnumeratorVariables* data;
|
|---|
| 303 |
|
|---|
| 304 | internal:
|
|---|
| 305 | Enumerator(__CvBlobs* map)
|
|---|
| 306 | {
|
|---|
| 307 | data = new CvBlobsEnumeratorVariables();
|
|---|
| 308 | data->map = map;
|
|---|
| 309 | data->it = map->begin();
|
|---|
| 310 | }
|
|---|
| 311 | !Enumerator()
|
|---|
| 312 | {
|
|---|
| 313 | if(data != NULL)
|
|---|
| 314 | {
|
|---|
| 315 | delete data;
|
|---|
| 316 | data = NULL;
|
|---|
| 317 | }
|
|---|
| 318 | }
|
|---|
| 319 | ~Enumerator()
|
|---|
| 320 | {
|
|---|
| 321 | if(data != NULL)
|
|---|
| 322 | {
|
|---|
| 323 | delete data;
|
|---|
| 324 | data = NULL;
|
|---|
| 325 | }
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | public:
|
|---|
| 329 | #ifdef LANG_JP
|
|---|
| 330 | /// <summary>
|
|---|
| 331 | /// コレクション内の現在の要素を取得します。
|
|---|
| 332 | /// </summary>
|
|---|
| 333 | #else
|
|---|
| 334 | /// <summary>
|
|---|
| 335 | /// Gets the element in the collection at the current position of the enumerator.
|
|---|
| 336 | /// </summary>
|
|---|
| 337 | #endif
|
|---|
| 338 | virtual property KeyValuePair<CvLabel, CvBlob^> Current
|
|---|
| 339 | {
|
|---|
| 340 | KeyValuePair<CvLabel, CvBlob^> get() = IEnumerator<KeyValuePair<CvLabel, CvBlob^>>::Current::get
|
|---|
| 341 | {
|
|---|
| 342 | CvLabel key = data->it->first;
|
|---|
| 343 | __CvBlob* value = data->it->second;
|
|---|
| 344 | return KeyValuePair<CvLabel, CvBlob^>(key, gcnew CvBlob(value));
|
|---|
| 345 | }
|
|---|
| 346 | }
|
|---|
| 347 | #ifdef LANG_JP
|
|---|
| 348 | /// <summary>
|
|---|
| 349 | /// コレクション内の現在の要素を取得します。
|
|---|
| 350 | /// </summary>
|
|---|
| 351 | #else
|
|---|
| 352 | /// <summary>
|
|---|
| 353 | /// Gets the current element in the collection.
|
|---|
| 354 | /// </summary>
|
|---|
| 355 | #endif
|
|---|
| 356 | virtual property Object^ CurrentObject
|
|---|
| 357 | {
|
|---|
| 358 | Object^ get() = System::Collections::IEnumerator::Current::get
|
|---|
| 359 | {
|
|---|
| 360 | return this->Current;
|
|---|
| 361 | }
|
|---|
| 362 | }
|
|---|
| 363 | #ifdef LANG_JP
|
|---|
| 364 | /// <summary>
|
|---|
| 365 | /// 列挙子をコレクションの次の要素に進めます。
|
|---|
| 366 | /// </summary>
|
|---|
| 367 | #else
|
|---|
| 368 | /// <summary>
|
|---|
| 369 | /// Advances the enumerator to the next element of the collection.
|
|---|
| 370 | /// </summary>
|
|---|
| 371 | #endif
|
|---|
| 372 | virtual Boolean MoveNext() = System::Collections::IEnumerator::MoveNext
|
|---|
| 373 | {
|
|---|
| 374 | (data->it)++;
|
|---|
| 375 | return data->it != data->map->end();
|
|---|
| 376 | }
|
|---|
| 377 | #ifdef LANG_JP
|
|---|
| 378 | /// <summary>
|
|---|
| 379 | /// 列挙子を初期位置、つまりコレクションの最初の要素の前に設定します。
|
|---|
| 380 | /// </summary>
|
|---|
| 381 | #else
|
|---|
| 382 | /// <summary>
|
|---|
| 383 | /// Sets the enumerator to its initial position, which is before the first element in the collection.
|
|---|
| 384 | /// </summary>
|
|---|
| 385 | #endif
|
|---|
| 386 | virtual void Reset() = System::Collections::IEnumerator::Reset
|
|---|
| 387 | {
|
|---|
| 388 | data->it = data->map->begin();
|
|---|
| 389 | }
|
|---|
| 390 | };
|
|---|
| 391 | #pragma endregion
|
|---|
| 392 | #pragma endregion
|
|---|
| 393 | };
|
|---|
| 394 | }
|
|---|
| 395 | }
|
|---|
| 396 | } |
|---|