Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Open CV problem

by russellp (Initiate)
on Dec 09, 2010 at 15:10 UTC ( #876264=perlquestion: print w/replies, xml ) Need Help??
russellp has asked for the wisdom of the Perl Monks concerning the following question:

Hi All.

I've looked around quite a bit and can't find a Perl Module that works with Webcam's. So I have decided to write one using OpenCV2.0 and Im having a problem.

My module:

package OpenCV::Webcam; our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); our $VERSION = '0.01'; use Inline C => Config => MYEXTLIB => 'C:\OpenCV2.0\lib\libcv200.dll.a C:\OpenCV +2.0\lib\libhighgui200.dll.a C:\OpenCV2.0\lib\libcxcore200.dll.a', INC => '-IC:\OpenCV2.0\include\opencv'; use Inline C => <<'END_OF_C_CODE'; #include "cv.h" #include "highgui.h" CvCapture *capture = 0; IplImage *frame = 0; int* height, width, step, channels, depth; int new() { capture = cvCaptureFromCAM( 0 ); printf("\nSetting Capture!\n"); } char* GetImage() { char *data; printf("\nGetting Image Data!\n"); frame = cvQueryFrame( capture ); cvCvtColor(frame, frame, CV_BGR2RGB); data = (char*)frame->imageData; return(data); } int Release() { printf("\nReleasing Capture!\n"); cvReleaseCapture( &capture ); } END_OF_C_CODE

The problem Im having is in the function GetImage() I return the binary image data back to Perl, their I use Gtk2 to provide a window to display the image.

use Gtk2 '-init'; use Glib qw(TRUE FALSE); use OpenCV::Webcam ; $window = Gtk2::Window->new(); $capture = OpenCV::Webcam::new(); $data = OpenCV::Webcam::GetImage(); $pixbuf = Gtk2::Gdk::Pixbuf->new_from_data ($data, 'rgb', FASLE, 8, 64 +0, 480, 1920); $image = Gtk2::Image->new_from_pixbuf ($pixbuf); $window->add($image); $window->show_all(); Gtk2->main;

The error I get is -

GLib-GObject-CRITICAL **: g_param_spec_float: assertion `default_value + >= minimum && default_value <= maximum' failed at line 8. Gtk-CRITICAL **: gtk_widget_class_install_style_property: assertion `G +_IS_PARAM_SPEC (pspec)' failed at line 8.

But sometimes I get an image, But most of the time I get the runtime error.

Im using Perl5.0008 And Gtk2 from lost mind

My question is this: Is the IplImage *frame->imageData from C compatible with Perl? And should it be suitable for Gtk2::Gdk::Pixbuf->new_from_data?

Any help on this is greatly appreciated for it will be FREE software when Im done!


Replies are listed 'Best First'.
Re: Open CV problem
by Tux (Abbot) on Dec 09, 2010 at 15:42 UTC

    Will this also work on non-Windows? Your code is very Windows oriŽnted.

    Most character or data pointers in C cannot be used just as such in perl. They will have to be converted to a SCALAR value, in case of a string this will be a PV.

    You will have to read the Inline::C man-page and the perlguts manpage. Good luck!

    Enjoy, Have FUN! H.Merijn
      Most character or data pointers in C cannot be used just as such in perl. They will have to be converted to a SCALAR value

      The nice thing about Inline::C is that it should take care of this, i.e. the necessary .xs (and .c) code doing the conversions is created behind the scenes - at least for simple types such as char*.

      For example, the following function returning a char* is mapped to a normal scalar on the Perl side:

      #!/usr/bin/perl use Inline C => Config => BUILD_NOISY => 1, CLEAN_AFTER_BUILD => 0; use Inline C => <<'END_OF_C_CODE'; char* GetImage() { char *data = "test-test-test"; return(data); } END_OF_C_CODE my $data = GetImage(); print "$data\n"; # "test-test-test"

      (btw, the CLEAN_AFTER_BUILD => 0 option I've specified above is very useful when you want to look at the auto-generated XS/C wrapper code for debugging purposes)

      So, if the data as returned by the OP's GetImage() function is in fact char*, things should just work fine in this regard. In other words, I suspect the problem lies elsewhere...

        When I do a printf(data); in C, I get the same out put from Perl when I do print("$data\n"); Sample out put is below.

        This look like binray data to me. But not sure if Gtk2::Pixfuf new_from_data can use this directly. I.E is their a header it looks for or somthing..... Not sure.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://876264]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2017-11-23 06:08 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (328 votes). Check out past polls.