2010年9月17日 星期五

用指令切換視窗工作區

透過下指令的方式切換視窗工作區
基本上可透過wnckprop或wmctrl做到工作區切換目的

1. wnckprop適用於Metacity管理介面
  切換桌面

wnckprop --workspace=1 --activate
  顯示桌面
wnckprop --screen=0 --show-desktop
2. wmctrl適用於Compiz管理介面
  切換桌面
wmctrl -o 1440,0
  顯示桌面
wmctrl -k on

2010年9月6日 星期一

OpenCV影像顯示於Gtk介面

在OpenCV中影像結構被定義為IplImage

本篇紀錄如何將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;
}