本篇紀錄如何將IplImage影像顯示於Gtk介面的Image物件
drawing.h
#ifndef DRAWING_H_INCLUDED
#define DRAWING_H_INCLUDED
#include <gtkmm.h>
#include <cv.h>
#include <highgui.h>
class drawing : public Gtk::DrawingArea{
public:
drawing();
virtual ~drawing();
virtual bool on_expose_event(GdkEventExpose* event);
void SetImage(IplImage* img);
private:
IplImage* Image;
};
#endif // DRAWING_H_INCLUDED
drawing.cpp
#include "drawing.h"
drawing::drawing():Image(NULL)
{
}
drawing::~drawing()
{
if(Image!=NULL)
cvReleaseImage(&Image);
}
bool drawing::on_expose_event(GdkEventExpose* event)
{
Glib::RefPtr window = get_window();
Glib::RefPtr gc=get_style()->get_bg_gc(Gtk::STATE_NORMAL) ;
if(Image!=NULL){
window->draw_rgb_image(gc,0,0,Image->width,Image->height,Gdk::RGB_DITHER_NONE,(const guchar* )Image->imageData,Image->widthStep);
queue_draw();
Glib::usleep(30000);
}
return true;
}
void drawing::SetImage(IplImage* img)
{
if(img)
{
if(Image)
cvCopy(img,Image);
else
Image=cvCloneImage(img);
queue_draw();
}
}
main.cpp
#include <iostream>
#include <gtkmm.h>
#include <libglademm.h>
#include <cv.h>
#include <highgui.h>
#include "drawing.h"
//#include "CameraDS.h"
Gtk::Window *window;
Gtk::VBox *box;
Gtk::Button *button;
Glib::Thread* thread;
drawing *pDraw;
static bool state=false;
void thread_func()
{
CvCapture* capture;
IplImage* img;
capture=cvCaptureFromCAM(CV_CAP_ANY);
while(state)
{
img=cvQueryFrame(capture);
cvCvtColor(img,img,CV_BGR2RGB);
pDraw->SetImage(img);
//std::cout << "width: " << width << " height: " << height << "\n"; } cvReleaseCapture(&capture); std::cout << "Thread excuted.\n"; } void on_my_clicked() { if(!state){ state=true; thread=Glib::Thread::create(sigc::ptr_fun(&thread_func),true); }else{ state=false; } std::cout << "clicked\n"; } int main(int argc, char *argv[]){ if(!Glib::thread_supported()) Glib::thread_init(); Gtk::Main main(argc,argv); Glib::RefPtr refXml = Gnome::Glade::Xml::create("windows.glade");
refXml->get_widget("window1", window);
refXml->get_widget("vbox1", box);
refXml->get_widget("button1", button);
pDraw=new drawing();
button->signal_clicked().connect(sigc::ptr_fun(&on_my_clicked));
box->add(*pDraw);
//box->add(*button);
//window->add(box);
window->resize(800,600);
window->show_all();
main.run(*window);
return 0;
}
沒有留言:
張貼留言