0

I have a boost::makevariantover. How can I convert that to a vector . Any samples will be helpful using boost::apply_visitor

class pixel_visitor
    : public boost::static_visitor<>
{

public:

template <typename T>
    void operator() (const ome::compat::shared_ptr<PixelBuffer<T> >& v)
    {
      std::cout << std::real(v);
    }    
};

pixelBuffer test= buf.vbuffer();    
test.apply_visitor(&pixel_visitor());

where

typedef boost::make_variant_over<pixel_buffer_types>::type     pixelBuffer;
sehe
  • 374,641
  • 47
  • 450
  • 633
Sarav
  • 139
  • 1
  • 13
  • A vector of what? Can you please provide code (small yet complete) that illustrates what you have and what you want? – VolkerK Apr 08 '16 at 07:59
  • @VolkerK i have updated the post. Right now I have trying to learn this boost apply_visitor .I have no idea on how does it work. I am trying to convert the boost::makevariant to a vector of double . It holds the pixel data of image – Sarav Apr 08 '16 at 08:06
  • Still not sure what you're looking for. `pixelBuffer test` (when the typedef is fixed) is a single value. What kind of vector do you want to create from this single value? Exactly what does `buf.vbuffer()` return? (Can you link to its documentation?) – VolkerK Apr 08 '16 at 09:15
  • sure @VolkerK http://downloads.openmicroscopy.org/bio-formats-cpp/5.1.0/api/classome_1_1bioformats_1_1FormatReader.html . What I did is, I got the variant pixel buffer using FormatReader::openbytes in this link .. I want to extract the pixel data as an numpy array from the variant pixel buffer. Any pointers to sample or tutorial would be helpful.Thanks – Sarav Apr 08 '16 at 09:31
  • Are you sure it isn't VariantPixelBuffer 's `const T * data () const` or `const PixelBuffer< T >::array_ref_type & array() const` you're looking for? – VolkerK Apr 08 '16 at 09:59

2 Answers2

2

Instead of .vbuffer() call .array(), it already applies the/a visitor "for you":

template<typename T>
inline typename PixelBuffer<T>::array_ref_type&
VariantPixelBuffer::array()
{
  detail::VariantPixelBufferVisitor<T> v;
  return boost::apply_visitor(v, buffer).array();
}

or .data() for the raw type.

VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • 1
    Ya got that so i can get boost::multiarray using the array() call .Correct? – Sarav Apr 08 '16 at 10:42
  • [Looks like](http://downloads.openmicroscopy.org/bio-formats-cpp/5.1.0/api/classome_1_1bioformats_1_1PixelBuffer.html#a99a634a844ea4a3e62b8ecc449e101af). – VolkerK Apr 08 '16 at 10:54
1

We don't know what your pixelbuffer types are.

Regardless, if you know how to convert those to vectors, you could simply return that from the visitor's call operator:

class pixel_visitor
    : public boost::static_visitor<std::vector<WhatEverYouWant> >
{

public:

template <typename T>
    result_type operator() (const ome::compat::shared_ptr<PixelBuffer<T> >& v)
    {
         std::vector<WhatEverYouWant> result;
         // do your conversion here
         return result;
    }    
};

So you can e.g.

std::vector<WhatEverYouWant> applied;

applied = boost_apply_visitor(pixel_visitor(), test);
sehe
  • 374,641
  • 47
  • 450
  • 633
  • The "thing" the OP gets from .vbuffer() is a `typedef boost::make_variant_over< pixel_buffer_types >::type`, i.e. a single variant. I think it's simply the wrong method ;-) There's a method `array()` which already applies a visitior. – VolkerK Apr 08 '16 at 10:24
  • Yes , sorry for that. array() provides the direct access to pixel data. thanks for the tip. I will try to convert these :) – Sarav Apr 08 '16 at 10:33
  • @VolkerK array already applies a visitor ? I couldn't understand that part – Sarav Apr 08 '16 at 10:38
  • yes, sorry, time for an answer instead fo comments ;-) – VolkerK Apr 08 '16 at 10:41
  • Ya , your answer worked .Thanks . I was trying for this three days. Have to brush up my C++ . thanks a lot @VolkerK – Sarav Apr 08 '16 at 10:45
  • @VolkerK Can you help me on how to return the boost::multiarray from buf.data() as numpy array ?? – Sarav Apr 09 '16 at 08:01
  • Have you looked through http://stackoverflow.com/questions/10701514/how-to-return-numpy-array-from-boostpython ? – VolkerK Apr 09 '16 at 11:44