Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Help using Allegro and OpenCV together

This thread is locked; no one can reply to it. rss feed Print
Help using Allegro and OpenCV together
toastedbread
Member #13,842
December 2011

I'm thinking of using OpenCV to grab an object's location from the webcam and then returning that information to Allegro to control a game.

However, I can't seem to get both libraries to work together. When I compile the file from the terminal, OpenCV can't seem to grab any frames from my webcam. But when the OpenCV code is compiled separately (without compiling the Allegro library) it works fine.

Does anyone have experience working with Allegro and OpenCV together? If not, is it possible for Allegro to grab live-feed information? I've found cases where people have saved the webcam info into a BITMAP, but any ideas whether this method will be fast enough for controlling the game? And I'm worried about the amount of resources this will consume.

I'm using Allegro 5; programming on a MacOSX and compiling and running codes on a terminal. When compiling an allegro program, I used the command:

g++ Code.c -o Tester -L/usr/local/lib -lallegro -lallegro_main

The command used to compile an OpenCV program:

g++ -bind_at_load `pkg-config --cflags opencv` Code.c -o Tester `pkg-config --libs opencv`

So when I write a program using both Allegro and OpenCV libraries, I used the terminal command:

g++ -bind_at_load `pkg-config --cflags opencv` Code.c -o Tester -L/usr/local/lib -lallegro -lallegro_main `pkg-config --libs opencv`

The program compiles successfully, but when I run it, OpenCV's cvQueryFrame returns NULL. I'm not sure what's going on... It'd be awesome if someone can shade some light on this? This is my first time using these two libraries, or developing games in general really, so I apologise if I'm using the terminologies wrongly.

Thanks!

Timorg
Member #2,028
March 2002

If you show some code, I would be interested investigating this. I have both libs installed and have an interest in it. Do you use the c++ or c api of OpenCV?

Edit: oh your file has a .c extension, so I am guessing C.

Edit: I am a windows user though, so my experiences might come across differently.

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

toastedbread
Member #13,842
December 2011

It's a really simple code for now as I'm trying to get the 2 to work together. The Allegro side of things displays a window and the OpenCV side returns frames captured with a webcam.

Yap, using the C api of OpenCV. Any insight will be awesome :)

#SelectExpand
1#include <allegro5/allegro.h> 2#include <stdio.h> 3#include "cv.h" 4#include "highgui.h" 5 6int main(int argc, char **argv) { 7 ALLEGRO_DISPLAY *display = NULL; 8 9 if (!al_init()) { 10 fprintf(stderr, "failed to initalise allegro \n"); 11 return -1; 12 } 13 14 display = al_create_display(640, 480); 15 if (!display) { 16 fprintf(stderr, "failed to create display \n"); 17 return -1; 18 } 19 20 al_clear_to_color(al_map_rgb(0,0,0)); 21 al_flip_display(); 22 23 //initilise cam 24 CvCapture* capture = cvCaptureFromCAM( 0 ); 25 if (!capture) { 26 fprintf( stderr, "Failed to capture \n" ); 27 getchar(); 28 return -1; 29 } 30 31/*this is where cvQueryFrame fails when compiled together with allegro libs. 32It'll work if all the allegro components are removed from this source code*/ 33 while (1) { 34 IplImage* frame = cvQueryFrame(capture); 35 if (!frame) { 36 fprintf(stderr, "Failed to grab a frame \n"); 37 //getchar(); 38 break; 39 } 40 cvShowImage( "captured", frame); 41 42 if ((cvWaitKey(10) & 255) == 27) { 43 break; 44 } 45 46 47 } 48 49 al_destroy_display(display); 50 cvReleaseCapture( &capture ); 51 return 0; 52}

Timorg
Member #2,028
March 2002

Unfortunately the code works on my windows machine. So I am not sure where your issue could be. I have no idea how HighGUI works on the mac, but if its anything like windows, it sits in a separate thread, that is idle until the main thread yields. When this happen, it attempts to grab a frame, if not enough time is spent resting, no frame is grabbed. I can only suggest try upping your cvWaitKey(10) to a higher value and see if it will grab a frame.

I guess its time to look at taking a OpenCV matrix and convert it to an allegro bitmap. :)

I wish you luck in your search for the problem.

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

toastedbread
Member #13,842
December 2011

Glad to hear that there's nothing wrong with the code! I've tried upping the cvWaitKey(10) but no luck. I will do some more debugging... worst case scenario, I'll just switch to programming on a windows platform ;)

Thanks heaps for taking the time to help debug this issue!

EDIT: Actually, I increased it all the way up to 3 seconds, moved the cvWaitKey(3000) to before the cvQueryFrame line and it now works! Seems like your explanation was spot on :) Tho the frame capture is not really up to date now. Will have to debug some more...

EDIT: Woah. I put in a conditional loop for it to wait 3 seconds during it's first time through the while loop and it now captures frames in real time! Yay! You're a legend :D

Thomas Fjellstrom
Member #476
June 2000
avatar

Timorg said:

it sits in a separate thread, that is idle until the main thread yields. When this happen, it attempts to grab a frame, if not enough time is spent resting, no frame is grabbed. I can only suggest try upping your cvWaitKey(10) to a higher value and see if it will grab a frame.

The crap? In what world does that make sense? I know its not like it was your decision or anything... But really, that's stupid.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Timorg
Member #2,028
March 2002

Quote:

Note: This function is the only method in HighGUI that can fetch and handle events, so it needs to be called
periodically for normal event processing unless HighGUI is used within an environment that takes care of event
processing.

The events run in another thread, I guess until it gets the event, it doesn't retrieve any frames. If there are too many other events waiting in the queue, it might not get a chance to process them all, and actually get around to processing the message where it grabs the frame you asked for.

I have no sure why the design decision was made, if its some sort of requirement for QT or somone thought it was good enough at the time. HighGUI is a strange beast, but its one of those things that gets the job done. Maybe it was just a case of they needed a cross platform GUI and that is what was come up with.

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

elantrix
Member #13,864
December 2011

I am new at allegro and have a problem about compiling allegro and opencv together in DevC++. My project controll pong game by recognizing hand gesture. I can recognize hand gestures through opencv. It works fine and now I try to combine pong code(i found it from web )and my opencv code in allegro app(static) but there is some errors. Can you help me?thank you.

There is part of my code and errors:
#include <allegro.h>
#include <cstdlib>
#include <time.h>
#include <iostream>
#include "cv.h"
#include "highgui.h"

49 C:\Dev-Cpp\include\windows.h:52, from C:\OpenCV2.1\include\opencv\highgui.h In file included from C:/Dev-Cpp/include/windows.h:52, from C:/OpenCV2.1/include/opencv/highgui.h
6 C:\OpenCV2.1\include\opencv\highgui.h:49, from main.cpp from C:/OpenCV2.1/include/opencv/highgui.h:49, from main.cpp
6 C:\Dev-Cpp\main.cpp from main.cpp
1222 C:\Dev-Cpp\include\wingdi.h conflicting declaration 'typedef struct tagBITMAP BITMAP'
276 C:\Dev-Cpp\include\allegro\gfx.h 'BITMAP' has a previous declaration as `typedef struct BITMAP BITMAP'

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

elantrix
Member #13,864
December 2011

It works.. Thank youu very much

Go to: