Hi,

I have managed to pass data from pdl to OpenCV and back by copying. I
would have preferred some kind of mapping but that is not working, see
below. So, technically, there is a working interface to OpenCV., at
least for grayscale floats.

ee the t/basic_opencv.t  for what it can do at the moment.

I will get the object tracking working next.

Video in/output is also planned, as is support for other data types, at
least 32fC1/3 8UC1/3 and basic imshow support.

I guess I will be able to generate simple opencv function support
through templates, see ufunc in poencv.pd (yes, I stole that from ops.pd).

So you can use it, but you would still need to write wrapper code
encapsulating OpenCV. I have tried - and failed - using Inline::CPP and
a simple #include <opencv2/core.hpp> which throws all sort of errors.
Most of all redefinitions like seed or cv.

Try compiling this code (g++) and you will see.

#include "EXTERN.h"

#include "perl.h"

#include "XSUB.h"

#include "INLINE.h"

#undef seed

        using namespace std;

#include <opencv2/core.hpp>



On 3/16/2022 4:55 PM, Ingo Schmid wrote:
>
> Hi Ed,
>
> thank you. I have made some progress but getting data from a cv:Mat to
> a PDL is a mystery still. If you could look at the code I would
> appreciate it. I have a valid pointer to the data, either in C or
> perl, but how to make a piddle use that as it's dataref is beyond me.
>
>
> Ingo
>
> On 3/15/22 21:50, Ed . wrote:
>>
>> Hi Ingo,
>>
>>  
>>
>> Glad you’re making progress!
>>
>>  
>>
>> The problem you’re having here is that you’re somewhat mixing your
>> metaphors. For now, PDL mainly uses SVs for storing data. The
>> “datasv” member of the struct points at the data SV, and the “data”
>> member is set to point at its PV. Within e.g. the readdata function,
>> a local C variable for the data pointer of ndarray “m” is created
>> called “m_datap”, which gets updated during broadcasting. The “$P(m)”
>> macro in PP expands to “m_datap”.
>>
>>  
>>
>> As you may now appreciate, assigning to that is not going to have any
>> long-term effect once the function returns. You’re going to need a
>> different approach. One that will operate somewhat, but isn’t the
>> correct answer, is to assign to “$PDL(m)->data” instead. But you’re
>> going to need to do memory-management properly, otherwise you’ll get
>> segfaults and/or leak memory.
>>
>>  
>>
>> A more direct way to achieve the aim, staying with the SV model, is
>> to enhance your MatWrapper Perl object (using XS) to be able to
>> return its data (and dimensions as well), and make your get_data be
>> pure-Perl that extracts dims, types and data from the MatWrapper in
>> Perl-land, and uses PDL methods setdims, set_datatype, and
>> get_dataref/upd_data to create an ndarray.
>>
>>  
>>
>> Best regards,
>>
>> Ed
>>
>>  
>>
>> *From: *Ingo Schmid <mailto:ingo...@gmx.at>
>> *Sent: *15 March 2022 19:52
>> *To: *perldl <mailto:pdl-general@lists.sourceforge.net>
>> *Subject: *[Pdl-general] set pointer to data (PP)
>>
>>  
>>
>> Hi,
>>
>> I would like to update the pointer to piddle data. I've got a C function
>>
>> void * getData(MatWrapper * mw);
>>
>> that returns the pointer to opencv Mat data. I declare the piddle $m
>> with the right size and data type in PMCode. Now, I can successfully say
>>
>> void * data = getData($COMP(mw));
>>
>> $P(m)=data;
>>
>> and print the right numbers in C.
>>
>> printf("m (48,48) %f\n",$m(k=>48,l=>48));  # 50.842896 - correct.
>>
>> However, after returning to perl, $m(48,48) is 0 and get_dataref
>> shows the wrong datdress. What's the trick to make this permanent?
>>
>> Ingo
>>
>> ==
>>
>> pp_def ('get_data',
>>         Pars=>'[o] m(k,l);',
>>         OtherPars=>'MatWrapper * mw',
>>         Code=>pp_line_numbers(__LINE__,
>>         '
>>                 PDL_Indx ks = $SIZE(k);
>>                 PDL_Indx ls = $SIZE(l);
>>                 int x = cols($COMP(mw),-1);
>>                 int y = rows($COMP(mw),-1);
>>                 printf ("x %d : ks %d / y %d : ls %d ",x,ks,y,ls);
>>                 if ((x == ks) && (y == ls))
>>                 {
>>                         void * dat;
>>                       dat = getData($COMP(mw));
>>                         $P(m)=dat;
>>                 } else {
>>                         $CROAK("Matrix size does not match!\n");
>>                 }
>>                 printf("m (48,48) %f\n",$m(k=>48,l=>48));
>>         '),
>>         PMCode=>pp_line_numbers(__LINE__,
>>         '
>>                 sub get_data {
>>                         my ($mw,$p) = @_;
>>                         unless (ref ($p) =~ "PDL") {
>>                                 say "rows $mw->rows";
>>                                 $p=zeroes(float,$mw->cols,$mw->rows);
>>                         }
>>                         _get_data_int($p,$$mw);
>>                         say "(get_data) 48,48: ",$p->slice("48","48");
>>                         $p->upd_data;
>>                         say "(get_data) 48,48: ",$p->slice("48","48");
>>                         $p->transpose;
>>                 }
>>                 ',),
>>         Doc=><<"EOD",
>>
>> =head2 set_data - update data of a Mat object. Size and type should
>> match.
>>
>> EOD
>>
>>         );
>>
>>  
>>
>>  
>>
>
>
> _______________________________________________
> pdl-general mailing list
> pdl-general@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/pdl-general
_______________________________________________
pdl-general mailing list
pdl-general@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pdl-general

Reply via email to