Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Inline::CPP + OpenCV = problems

by bliako (Monsignor)
on Nov 19, 2019 at 22:21 UTC ( [id://11108912]=perlquestion: print w/replies, xml ) Need Help??

bliako has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed Monks,

I wanted to experiment with calling OpenCV v4.1.2 C++-api functions from within Perl via Inline::CPP

I have encountered a couple of problems, the last of which is insurmountable to me.

The first problem is that OpenCV defines a seed(unsigned s) function in /usr/local/include/opencv4/opencv2/core.hpp which conflicts with Perl's seed macro. That can be overcome by adding #undef seed at the top of my CPP section (see https://github.com/Leont/libperl--/issues/5). Although it passes that, it's no good news at all.

The second problem is that OpenCV uses internally the namespace cv (e.g. /usr/local/include/opencv4/opencv2/core/cvdef.h) which conflicts with Perl's struct cv in perl5/CORE/sv.h.

Any ideas or is that the end of the road for my efforts?

CPAN module Cv has not been updated since 2013 and is based on a very outdated OpenCV C-api (it includes #include <opencv/cv.h>) which no longer exists in version 3 or 4 of OpenCV. Just mentioning that that's not an option ...

Here is a sample code

#!/usr/bin/perl use strict; use warnings; use Inline CPP => DATA => CCFLAGS => '-std=c++11', LIBS => '-L/usr/local/lib64 -lopencv_imgcodecs -lopencv_core', INC => '-I/usr/local/include/opencv4' ; my $scrabble_image = 'IMAGES/scrabble_board1.jpg'; my $x = scrabbler($scrabble_image); __END__ __CPP__ #undef seed #include <iostream> #include <stdexcept> #include <stdlib.h> #include <math.h> #include <unordered_map> #include <map> #include <opencv2/core/types_c.h> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/features2d/features2d.hpp> int scrabbler(char *imname){ cv::Mat myInputImage = cv::imread(imname); int W = myInputImage.cols; int H = myInputImage.rows; std::cout << "read image '" << imname << "': W=" << W << ", H=" << + H << std::endl; return 1; }

Many thanks, bliako

Update: added corrected cv::imread to sample code

Replies are listed 'Best First'.
Re: Inline::CPP + OpenCV = problems
by bliako (Monsignor) on Nov 20, 2019 at 00:10 UTC

    If I pack all OpenCV function calls inside a very high-level function in a new C++ library and avoid any inclusion of OpenCV headers in the perl script then it works, obviously.

      Cpp code before inline/xs code in sourcefile avoids collisions? Also XSpp

        Thanks for the idea but the order is determined by Inline::CPP when it creates the XS file in folder _Inline/build. Perl headers come first. However I did change the order of the includes manually (e.g. without re-perling, I went into said folder and edited the XS file). The first error (seed redefined) is gone (something tells me it will reappear if/when I make Perl call its seed) but the second error (e.g. namespace cv clashes with struct cv) persists (obviously).

        My knowledge of XS is zero, but wouldn't using XS/XSpp require including those OpenCV headers? And still get the clash with Perl's headers?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11108912]
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-20 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found