Jumaat, 31 Januari 2014

OpenCV with gtkmm3

After being looking for few weeks, i'm still not found how to make opencv working with gtkmm, but after few times try an error i manage to done it. feel free to see my code.

main.cpp

#include "include/gui.h"

int main(int argc, char *argv[]) {
    Gtk::Main kit( argc, argv );
    GUI gui;
    Gtk::Main::run( gui );

    return EXIT_SUCCESS;
}

gui.h

#ifndef GUI_H
#define GUI_H

#ifndef CV_BGR2RGB
#define CV_BGR2RGB 4
#endif // CV_BGR2RGB

#include <gtkmm.h>
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include <waktu.h>
#include <linux.h>
#include "orec.h"

using namespace std;

class GUI : public Gtk::Window
{
    public:
        /** Default constructor */
        GUI();
        /** Default destructor */
        virtual ~GUI();
        /** Copy constructor
         *  \param other Object to copy from
         */
        GUI(const GUI& other);
        /** Assignment operator
         *  \param other Object to assign from
         *  \return A reference to this
         */
        GUI& operator=(const GUI& other);
    protected:
    private:
        cv::VideoCapture vc;
        cv::Mat mat_input;
 
        ORec orec;
        Waktu waktu;
        Linux lin;

        int sample_width;
        int sample_height;

        Gtk::Frame frame_sample;
        Gtk::Image image_sample;
        Gtk::VBox vbox_sample;
        Gtk::Label label_mspf;

        Glib::RefPtr pixbuf_sample;
        Gdk::InterpType interp_type;

        // Signal
        bool Run();
};

#endif // GUI_H

gui.cpp

#include "../include/gui.h"

GUI::GUI() {
    try {
        pixbuf_sample = Gdk::Pixbuf::create_from_file( "image.jpg" )->scale_simple( sample_width = 500, sample_height = 375, Gdk::INTERP_BILINEAR );
    } catch(const Glib::FileError& ex) {
        std::cerr << "FileError: " << ex.what() << std::endl;
    } catch(const Gdk::PixbufError& ex) {
        std::cerr << "PixbufError: " << ex.what() << std::endl;
    }

    image_sample.set( pixbuf_sample );

    label_mspf.set_label( "Speed" );

    vbox_sample.set_border_width( 10 );
    vbox_sample.pack_start( image_sample, false, Gtk::SHRINK );
    vbox_sample.pack_start( label_mspf, false, Gtk::SHRINK );

    frame_sample.set_label( "Image" );
    //frame_sample.set_border_width( 10 );
    frame_sample.add( vbox_sample );

    set_title( "Object Recognition" );
    set_border_width( 10 );
    add( frame_sample );
    show_all_children();

    sleep( 2 );

    Glib::signal_idle().connect( sigc::mem_fun(*this, &GUI::Run ) ); 
 
    // opencv
    vc.open( 0 ); 
}

GUI::~GUI() {
    //dtor
}

GUI::GUI(const GUI& other) {
    //copy ctor
}

GUI& GUI::operator=(const GUI& rhs) {
    if (this == &rhs) return *this; // handle self assignment
    //assignment operator
    return *this;
}

int i = 0;

bool GUI::Run() {  
    if( !vc.isOpened() ) return true;
 
    vc >> mat_input;
 
    if( !mat_input.empty() ) return true;
    cv::cvtColor( mat_input, mat_input, CV_BGR2RGB ); 
 
    Glib::RefPtr win = get_window();
    if( win ) {
        try {
            pixbuf_sample = Gdk::Pixbuf::create_from_data( ( guint8* )( mat_input.data ), Gdk::COLORSPACE_RGB, false, 8, mat_input.cols, mat_input.rows, mat_input.step )->scale_simple( sample_width, sample_height, Gdk::INTERP_BILINEAR );
        } catch(const Glib::FileError& ex) {
                std::cerr << "FileError: " << ex.what() << std::endl;
        } catch(const Gdk::PixbufError& ex) {
                std::cerr << "PixbufError: " << ex.what() << std::endl;
        }

        image_sample.set( pixbuf_sample );

        double speed = waktu.Speed( 10 );
        label_mspf.set_label( lin.String( speed ) + " fps / " + lin.String( 1.0 / speed ) + " spf" );
    }

    return true;
}
 

So that all, but for those who ask for function like waktu, orec and lin, you can comment those line and subtitute with you own code.

Tiada ulasan:

Catat Ulasan