'OpenCV C# iterate Mat object like C++
I'm trying to follow a tutorial of OpenCV which is written in C++ but I need to write it in C#. This works quite ok so far. As I'm not a C++ developer and not. familiar with the language I came to a point now where I struggle a bit and where I'm not sure if what I did is the equivalent to the C++ version.
This is the code in C++
vector<Point> pointsOfInterest;
Mat_<uchar>::iterator itMask = mask.begin<uchar>();
Mat_<uchar>::iterator end = mask.end<uchar>();
for( ; itMask!=end; ++itMask)
if(*itMask==255)
pointsOfInterest.push_back(itMask.pos());
What I read here is a loop, that iterates over the whole Mat and checks if the value at position is 255 and if so, it adds the point to the end of the pointsOfInterest list.
So I guess this means every x,y pixel in the Mat is checked for the value. My equivalent in C# looks like this:
List<Point> pointsOfInterest = new List<Point>();
for(int x = 0;x<mask.Cols;x++)
{
for(int y=0;y<mask.Rows; y++)
{
if (mask.At<int>(x, y) == 255)
pointsOfInterest.Add(new Point(x, y));
}
}
but the result I'm getting is not as expected that's why I wonder if this part is really correct? Sometimes the mask.At throws a NullPointer as well which is strange. I'm also wondering that the C++ just iterates a one dimensional array (I think at least) meanwhile C# would need 2-Dimensions.
Can someone confirm that it is the same or does someone know what is wrong and needs to be adjusted?
Solution 1:[1]
I have also found out that changing the x and y, for the img.At(y,x) , will solve the exception and you will get the points in your list.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Learner |
