PDA

View Full Version : Display the camera frame in real time



alex_lue
26th July 2007, 12:47
I use a FireWire-Camera and i want to display the frame in real time (15 ~ 30fps) by Qt. I have tried to display it by QGraphicsview, e.g.:

graphicsview->scene()->addPixmap(pixmap);
But it is too slow. And i have tried to use "setBackgroundBrush", e.g.:

graphicsview->scene()->setBackgroundBrush(QBrush::QBrush(qimage));
This is faster, but not real time.
Can anyone tell me some advices how to solve the tempo problem?

Thanks!:)

marcel
26th July 2007, 13:50
I think someone tried this before...
How do you acquire the frames in the GUI thread? Do you have a separate thread that communicates with the camera?
Perhaps the communication between the worker thread and the GUI can be improved...

If you can post the code for this part, the do it.

Regards

alex_lue
27th July 2007, 09:46
Here is my code:


QImage camImage;
........
.......
// Loop ...
while(camSwitch != false)
{
// Wait 5000 MillSeconds for a frame
if(Ret == FCE_NOERROR)
Ret = FGGetFrame(hCamera, &Frame, 5000);

if(Ret == FCE_NOERROR)
Ret = FGPutFrame(hCamera, &Frame);

cvNamedWindow("1", CV_WINDOW_AUTOSIZE);

camImage = QImage::QImage(Frame.pData, 640, 480, QImage::Format_Indexed8);
camImage.setColorTable(grayscales);

Display->scene()->setBackgroundBrush(QBrush::QBrush(camImage));
Display->fitInView(0,0,640,480);
cvWaitKey(50);
}

Frame.pData is the real time frame with type unsigned char *
Line 14 & 21 are 2 functions of OpenCv(I hope you know that.) Line 14 made a new window. And line 21 ist a wait function. When i don't use these 2 functions, the program run to crash. This is comic.
Display is the object of QGraphicsview. Although i have set up the size of Display in 640*480 in QtDesigner, but wenn i don't use fitInView(0,0,640,480), the frame is not displayed automatically. So i have to use fitInView.
Have i too many error?

Thanks!

marcel
27th July 2007, 09:52
Probably the delay is because you fetch the frames in the GUI thread.
The camera interface may give you the frames in real time, but displaying them is not done in real time.

I suggest using a worker thread(producer) to fetch and store frames in a buffer. The GUI thread just takes out the frames as it needs to display them. This way (hopefully) no delay will be noticed.

Regards

alex_lue
27th July 2007, 10:02
Thank you for your fast reply.:p

Do you mean i should use QThread or QBuffer?? Is there other method to display the fram? (e.g.: QImage or QPixmap)?

marcel
27th July 2007, 10:08
I think QImage is OK.
You should use a QThread that will only acquire camera frames and store them somewhere( a data structure you find suitable, even QBuffer, but you should research a bit the speed ).

The location in which you store the frames must be accessible to the GUI thread.(maybe a getter in the worker thread?, or a static object - not recommended, since you only have to threads)

The GUI thread just has to read the worker thread's frame store.

Regards

alex_lue
27th July 2007, 10:19
I have only a little experiences to use Multithreading. Is there also in Qt such function as _beginthread(....) in VC++ 6.0? Or must i new define an object von QThread ?

marcel
27th July 2007, 10:26
Threads in Qt are really easy to use.

See first the QThread documentation. You can also learn a lot from the Qt Examples.

You will have to subclass QThread and reimplement the run() method.
You should have a loop there that acquires frames and puts them in the buffer.

To start a thread just call its start() method.

Regards

alex_lue
27th July 2007, 10:31
I try to write.
Thanks.:p