Originally Posted by
KiwiRider
Hello everyone,
First of all, I'm new member on here, so let me say HELLO loudly.
I try to search previous threads, but wasn't successful to find the solution so, I'm end up here to post my question.
I'm using QT to developing real time colour recognisation system and it was so far fine, until I got little performance issue here.
Here, I'm using QMainWindow, QGraphicsView and QGraphicsScene to layout my interface, and I'm having 640*480 colour image from camera interface, then this image converted to QImage for display purpose only.
I tried convert this QImage to QPixmap and addItem to Scene(so it displays) then delete it when my next frame is arrived, it was working but very slow.
Like :
scene->addPixmap(QPixmap::fromImage(capture_frame));
So, I tried use QImage as Scene's background, it was much faster than previous method but background is displaying in tile mode, and some what slow still.
scene->setBackgroundBrush(QBrush::QBrush(capture_frame)) ;
So, now I wonder, is there any better way to display QImage into (or under) QGraphicsScene without adding it as item(because it will only good for every 30ms)
Also I need to display some indication marks over this QImage, so I don't want this QImage overlap existing contents of QGraphicsScene.
Hope to read some good ideas from you guys.
Cheers
KiwiRider
Hello KiwiRider...
first of all... welcome... i'm new in this forum too...
I'm working on an object tracking system and i think i can help you... I use QImage object to get pixel access (required for tracking algorithm) and then paint the image using QPainter an QPixmap, i use 640x480 images sequence too...
Here's the way i get good results (speed in show and refresh)...
In header file declare the pixmap, qimage and things you will need:
#ifndef GUI_TRACKING_H
#define GUI_TRACKING_H
//here need to include all the classes you need to use
class paintManager;
class gui_tracking;
class thread
: public QThread//neede to create a thread for load images{
public:
gui_tracking *parent;
thread(gui_tracking *p);
virtual void run();
};
paintManager *painter;
bool needToUpdate;
int imageHeight, imageWidth;
thread *myThread;
...
public:
void customEvent (QCustomEvent *e);
...
#endif
#ifndef GUI_TRACKING_H
#define GUI_TRACKING_H
//here need to include all the classes you need to use
class paintManager;
class gui_tracking;
class thread : public QThread//neede to create a thread for load images
{
public:
gui_tracking *parent;
thread(gui_tracking *p);
virtual void run();
};
QPixmap *pixmapImagen;
QImage *imageImagen;
paintManager *painter;
bool needToUpdate;
int imageHeight, imageWidth;
thread *myThread;
...
public:
void paintEvent(QPaintEvent *);
void customEvent (QCustomEvent *e);
...
#endif
To copy to clipboard, switch view to plain text mode
In the gui_tracking implementation file the constructor look something like this:
imageHeight= 480;
imageWidth = 640;
imageImagen
= new QImage(imageWidth, imageHeight,
32);
painter = new paintManager(pixmapImagen, imageHeight, imageWidth, imageHeight, imageWidth);
myThread = new thread(this);
needToUpdate = TRUE;
imageHeight= 480;
imageWidth = 640;
imageImagen = new QImage(imageWidth, imageHeight, 32);
pixmapImagen = new QPixmap();
painter = new paintManager(pixmapImagen, imageHeight, imageWidth, imageHeight, imageWidth);
myThread = new thread(this);
needToUpdate = TRUE;
To copy to clipboard, switch view to plain text mode
My paintEvent looks like this:
{
p.drawPixmap(1,1,*pixmapImagen ,0,0,-1,-1);
}
void gui_tracking::paintEvent(QPaintEvent *)
{
QPainter p(frame_Image);
p.drawPixmap(1,1,*pixmapImagen ,0,0,-1,-1);
}
To copy to clipboard, switch view to plain text mode
I use a QThread with a while loop inside that open an image file and show it, so i need to tell "update my pic!!!" to the main thread...I use an event function for that job... this is (needed below)...
void gui_tracking::customEvent (QCustomEvent * e)
{
if ( e
->type
() == QEvent::User ) {
painter->paintImage(imageImagen);
repaint(FALSE);
needToUpdate = TRUE;
}
}
void gui_tracking::customEvent (QCustomEvent * e)
{
if ( e->type() == QEvent::User )
{
painter->paintImage(imageImagen);
repaint(FALSE);
needToUpdate = TRUE;
}
}
To copy to clipboard, switch view to plain text mode
In the thread's while loop i do:
void
parent->imageImagen->load(parent->fileName);
//filename is changing every loop with the name of the next image of the sequence
...
void thread::run()
{
while(condition)
{
...
parent->imageImagen->load(parent->fileName);
if(parent->needToUpdate)
{
QCustomEvent
*evento
= new QCustomEvent
(QEvent::User);
parent->needToUpdate = FALSE;
}
}
cout<<"FINISHED"<<endl;
}
parent->imageImagen->load(parent->fileName);
//filename is changing every loop with the name of the next image of the sequence
...
void thread::run()
{
while(condition)
{
...
parent->imageImagen->load(parent->fileName);
if(parent->needToUpdate)
{
QCustomEvent *evento = new QCustomEvent(QEvent::User);
parent->needToUpdate = FALSE;
QApplication::postEvent(parent, evento);
}
}
cout<<"FINISHED"<<endl;
}
To copy to clipboard, switch view to plain text mode
For the paintManager class... the header file is something like this:
#ifndef PAINTMANAGER_H
#define PAINTMANAGER_H
class paintManager
{
public:
paintManager
(QPixmap *pixmap,
int rowsImage,
int colsImage,
int rowsPixmap,
int colsPixmap
);
~paintManager();
void paintImage
( QImage *imagen
);
private:
int widthImagen;
int HeightImagen;
int widthPixmap;
int heightPixmap;
};
#endif
#ifndef PAINTMANAGER_H
#define PAINTMANAGER_H
class QPainter;
class QPixmap;
class QPen;
class paintManager
{
public:
paintManager(QPixmap *pixmap,int rowsImage,int colsImage,int rowsPixmap,int colsPixmap);
~paintManager();
void paintImage( QImage *imagen );
private:
int widthImagen;
int HeightImagen;
int widthPixmap;
int heightPixmap;
QPixmap *pixmap;
QPainter *pixmapPainter;
QPen *penUsed;
};
#endif
To copy to clipboard, switch view to plain text mode
and the implementation file:
#include <qpainter.h>
#include <qpixmap.h>
#include <qpen.h>
#include "paintmanager.h"
paintManager
(QPixmap *pixmap,
int rowsImage,
int colsImage,
int rowsPixmap,
int colsPixmap
){
pixmap=pixmap;
widthImagen =colsImagen;
heightImagen=rowsImagen;
widthPixmap=colsPixmap;
heightPixmap=rowsPixmap;
pixmap->resize(widthPixmap, HeightPixmap);
pixmap->fill("white");
pixmapPainter->begin(pixmap);
}
paintManager::~paintManager()
{
pixmapPainter->end();
delete pixmapPainter;
delete penUsed;
}
void paintManager
::paintImagen(QImage *imagen
) {
pixmapPainter->drawImage(0,0,*imagen,0,0,-1,-1,0);
}
#include <qpainter.h>
#include <qpixmap.h>
#include <qpen.h>
#include "paintmanager.h"
paintManager(QPixmap *pixmap,int rowsImage,int colsImage,int rowsPixmap,int colsPixmap)
{
pixmap=pixmap;
widthImagen =colsImagen;
heightImagen=rowsImagen;
widthPixmap=colsPixmap;
heightPixmap=rowsPixmap;
pixmapPainter= new QPainter();
penUsed=new QPen();
pixmap->resize(widthPixmap, HeightPixmap);
pixmap->fill("white");
pixmapPainter->begin(pixmap);
}
paintManager::~paintManager()
{
pixmapPainter->end();
delete pixmapPainter;
delete penUsed;
}
void paintManager::paintImagen(QImage *imagen)
{
pixmapPainter->drawImage(0,0,*imagen,0,0,-1,-1,0);
}
To copy to clipboard, switch view to plain text mode
And thats all... that works for me... sorry if there is something wrong... (like a spanish name) my original code is in spanish... but this is the basic idea...
Hope help you... if not... sorry...
Any question contact me...
Salu2!!!
PS: I dont recommend convert QImage to Qpixmap... that's toooooo slow... read the QImage documentation...
Bookmarks