Search code examples
c++vb.netopencvemgucvmat

Using OpenCV Mat as an Array: VB vs C++


I'm using VS2015, EmguCV 3 and VB, and am trying to translate some C++ code.

C++

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(bw, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
for (size_t i = 0; i < contours.size(); ++i)
{...}

I'm trying to use some object orientation code given in full here. Basically the code is going to tell me the angle at which an object is oriented in an image. Unfortunately it is C++ code and VB developers' brains can explode at the sight of some C++ syntax. Any help avoiding the need to clean my screen again would be welcome. The explosive material was vector<vector<point> > contours; in this particular case, and my question is about how to translate it.

I got this far:

VB

Imports Emgu.CV
Imports Emgu.CV.Structure
...
contours = New Mat
hierarchy = New Mat
CvInvoke.FindContours(m, contours, hierarchy, CvEnum.RetrType.List, CvEnum.ChainApproxMethod.ChainApproxNone)

I'm using EmguCV 3. This claims that FindContours takes image As IInputOutputArray, contours As IOutputArray, hierarchy As IOutputArray. So I figured I could provide three Mats. m is defined earlier has been successfully processed (e.g. with Threshold) so I'm happy with m. contours and hierarchy on the other hand may be problematic. When I run the code, I get an unhandled exception:

Emgu.CV.Util.CvException: OpenCV: (_contours.kind() == _InputArray::STD_VECTOR_VECTOR || _contours.kind() == _InputArray::STD_VECTOR_MAT || _contours.kind() == _InputArray::STD_VECTOR_UMAT)

This seems to suggest I've passed the wrong types to OpenCV although I would have expected Emgu to handle that. But I have no clue. Any help?


Solution

  • Based on the Documentation and under the VB section:

    "contours Type: Emgu.CV.IOutputArray -> Detected contours. Each contour is stored as a vector of points."

    Therefore, instead of sending a single MAT as your contours, you should be sending a container of vectors of points.

    See here The Equivalent of C++ Vectors for VB.Net.