OpenCV with Qt C++ Examples (Showing Image)

See this post if you want to convert the image in gray.
1. Create a new Qt GUI Application project.
2. Add a new Label and Push Button in the form.
3. Do the rest with writing the program:

main.cpp:
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}


 mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv/cv.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/highgui.h>
#include <QFileDialog>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void changeEvent(QEvent *e);
    QString     FileOpName; //declare FileOpName as IplImage
    IplImage*   imagerd; //declare imagerd as IplImage

private slots:
    void on_pushButton_clicked();
    void openImage(); //add a new openImage function to the project

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H



mainwindow.cpp:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv/cv.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/highgui.h>
#include <QFileDialog>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void changeEvent(QEvent *e);
    QString     FileOpName; //declare FileOpName as IplImage
    IplImage*   imagerd; //declare imagerd as IplImage

private slots:
    void on_pushButton_clicked();
    void openImage(); //add a new openImage function to the project

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
---------------------------------------OR-------------------------------------------------
DOWNLOAD THE PROJECT (.zip)

8 comments:

  1. Thanks - this was the first sample code to compile on OS/X and Ubuntu (after a few minor hurdles that had to do with configuration issues).

    ReplyDelete
  2. You are welcome, happy coding by the way..

    ReplyDelete
  3. hi,

    i am in need of some help from you. can you help me out?

    thanx.

    ReplyDelete
  4. It depends on your problem, I'm not an expert by the way.
    I will do my best.

    ReplyDelete
  5. thanx for your reply..i need to implement as you did but i need to add another push button, in which i have to convert image to grayscale if the button is pressed. Can you help me out in this? because i am getting an error alwayz.

    thanx

    ReplyDelete
    Replies
    1. hei, I've made a new post that might relate to your problem. Check it out: http://hxr99.blogspot.com/2013/01/qt-opencv-displaying-images-and-convert.html

      Delete
  6. maybe this link would help you out:
    http://qt-project.org/faq/answer/how_can_i_convert_a_colored_qpixmap_into_a_grayscaled_qpixmap

    I still have an exam, after the exam I will post another tutorial with Opencv and Qt.

    ReplyDelete
  7. Hi in pro file u have written `pkg-config opencv --libs --cflags` for configuring libs file rit is it for Linux . if it is for Linux how abt windows bec i am always keep on copy and pasting the libs file and include file can u please let me now

    ReplyDelete