0

I know SO is not rent-a-coder, but I have a really simple python example that I need help translating to C++

grey_image_as_array = numpy.asarray( cv.GetMat( grey_image ) )

non_black_coords_array = numpy.where( grey_image_as_array > 3 )

# Convert from numpy.where()'s two separate lists to one list of (x, y) tuples:
non_black_coords_array = zip( non_black_coords_array[1], non_black_coords_array[0] )

First one is rather simple I guess - a linear indexable array is created with what bytes are retruned from cv.GetMat, right?

What would be an equivalent of pyton's where and especially this zip functions?

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
Maxim V. Pavlov
  • 10,303
  • 17
  • 74
  • 174
  • For zip, have a look at this question (you should be able to use boost::zip_iterator): http://stackoverflow.com/questions/8511035/sequence-zip-function-for-c11 – ChristopheD Feb 28 '12 at 23:32
  • 2
    You don't need `zip()` in a C++ equivalent, since you'd write the code in a way that `non_black_coords_array` has the desired structure right away. It is needed in Python because you cannot control the output format of `numpy.where()`. – Sven Marnach Feb 28 '12 at 23:35

1 Answers1

0

I don't know about OpenCV, so I can't tell you what cv.GetMat() does. Apparently, it returns something that can be used as or converted to a two-dimensional array. The C or C++ interface to OpenCV that you are using will probably have a similarly names function.

The following lines create an array of index pairs of the entries in grey_image_as_array that are bigger than 3. Each entry in non_black_coords_array are zero based x-y-coordinates into grey_image_as_array. Given such a coordinates pair x, y, you can access the corresponsing entry in the two-dimensional C++ array grey_image_as_array with grey_image_as_array[y][x].

The Python code has to avoid explicit loops over the image to achieve good performance, so it needs to make to with the vectorised functions NumPy offers. The expression grey_image_as_array > 3 is a vectorised comparison and results in a Boolean array of the same shape as grey_image_as_array. Next, numpy.where() extracts the indices of the True entries in this Boolean array, but the result is not in the format described above, so we need zip() to restructure it.

In C++, there's no need to avoid explicit loops, and an equivalent of numpy.where() would be rather pointless -- you just write the loops and store the result in the format of your choice.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • Sven, in my case the final destination would be C#, since I am not a C++ developer, so I don't know even how to write what you are explaining in C++. I know you have extended your answer, but could you also please write a c++ loop you are mentioning to do this >3 filtering? – Maxim V. Pavlov Feb 29 '12 at 00:24
  • @MaximV.Pavlov: That will look very similar to C#, the loops itself probably identical. The difficult part is what data type to use for the target array, and that depends on what is done with the result further on. Without more context, I can't give further advice. – Sven Marnach Feb 29 '12 at 00:28