Results 1 to 2 of 2

Thread: Qt 4 - simple GUI for a OpenCV program

  1. #1
    Join Date
    Sep 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt 4 - simple GUI for a OpenCV program

    I been trying to make a simple interface for an image processing task using OpenCV with C++ using Qt for the GUI. I'm able to load the image through the GUI but when I press the pushbutton_3, to convert the image to grayscale gives an error regarding OpenCV. I'm sure I'm doing something wrong. Can some one give me a help?

    Please see below the files:

    Qt Code:
    1. //mainwindow.h
    2.  
    3. #ifndef MAINWINDOW_H
    4. #define MAINWINDOW_H
    5.  
    6. #include <QMainWindow>
    7. #include <QFileDialog>
    8. #include <opencv2/core/core.hpp>
    9. #include <opencv2/highgui/highgui.hpp>
    10. #include <vector>
    11.  
    12. #include <QtCore/QCoreApplication>
    13. #include <opencv2/core/core.hpp>
    14. #include <opencv2/highgui/highgui.hpp>
    15. #include <opencv2/core/types_c.h>
    16. #include <opencv2/imgproc/imgproc.hpp>
    17.  
    18.  
    19. namespace Ui {
    20. class MainWindow;
    21. }
    22.  
    23. class MainWindow : public QMainWindow
    24. {
    25. Q_OBJECT
    26.  
    27. public:
    28. explicit MainWindow(QWidget *parent = 0);
    29. ~MainWindow();
    30.  
    31. private slots:
    32. void on_pushButton_clicked();
    33.  
    34. void on_pushButton_2_clicked();
    35.  
    36. void on_pushButton_3_clicked();
    37.  
    38.  
    39.  
    40.  
    41. private:
    42. Ui::MainWindow *ui;
    43. //Images variables
    44. cv::Mat image_Idl;
    45. cv::Mat image_Lit;
    46. cv::Mat image_Idl_G;
    47. cv::Mat image_Lit_G;
    48. double threshHold;
    49.  
    50. };
    51.  
    52. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //mainwindow.cpp
    2. #include "mainwindow.h"
    3. #include "ui_mainwindow.h"
    4. #include <iostream>
    5. #include <QtCore/QCoreApplication>
    6. #include <opencv2/core/core.hpp>
    7. #include <opencv2/highgui/highgui.hpp>
    8. #include <opencv2/core/types_c.h>
    9. #include <opencv2/imgproc/imgproc.hpp>
    10. #include <QSpinBox>
    11. #include <QSlider>
    12. #include <stdio.h>
    13. #include <stdlib.h>
    14. #include <vector>
    15.  
    16. using namespace std;
    17.  
    18.  
    19.  
    20. MainWindow::MainWindow(QWidget *parent) :
    21. QMainWindow(parent),
    22. ui(new Ui::MainWindow)
    23. {
    24. ui->setupUi(this);
    25.  
    26. ui->horizontalSlider->setRange(0,255);
    27. ui->spinBox->setRange(0,255);
    28.  
    29. connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),ui->spinBox,SLOT(setValue(int)));
    30. connect(ui->spinBox,SIGNAL(valueChanged(int)),ui->horizontalSlider,SLOT(setValue(int)));
    31.  
    32. }
    33.  
    34. MainWindow::~MainWindow()
    35. {
    36. delete ui;
    37. }
    38.  
    39.  
    40. void MainWindow::on_pushButton_clicked()
    41. {
    42. QString fileName = QFileDialog::getOpenFileName(this,tr("Load Lit Image"),".",tr("Image Files (*.png *.jpg *.jpeg *.bmp)"));
    43. image_Lit = cv::imread(fileName.toAscii().data());
    44. cv::namedWindow("Lit Image");
    45. cv::imshow("Lit Image", image_Lit);
    46. }
    47.  
    48. void MainWindow::on_pushButton_2_clicked()
    49. {
    50. QString fileName = QFileDialog::getOpenFileName(this,tr("Load Lit Image"),".",tr("Image Files (*.png *.jpg *.jpeg *.bmp)"));
    51. image_Idl = cv::imread(fileName.toAscii().data());
    52. cv::namedWindow("Ideal Lit");
    53. cv::imshow("Ideal Lit", image_Idl);
    54.  
    55. }
    56.  
    57.  
    58.  
    59.  
    60.  
    61.  
    62. void MainWindow::on_pushButton_3_clicked()
    63. {
    64. //Converstions
    65.  
    66.  
    67. //Convert Lit to gray
    68.  
    69.  
    70.  
    71. cv::cvtColor(image_Lit, image_Lit_G,CV_RGB2GRAY);
    72.  
    73. //Convert Ideal gray
    74.  
    75.  
    76. cv::cvtColor(image_Idl, image_Idl_G,CV_RGB2GRAY);
    77.  
    78.  
    79. //Threshold the Images to a designated value
    80. // Lit
    81.  
    82. threshHold = ui->horizontalSlider->value();
    83.  
    84. cv::threshold(image_Lit_G,image_Lit_G, threshHold,255,cv::THRESH_BINARY);
    85. cv::namedWindow("Gray Scaled Image");
    86. cv::imshow("Gray Scaled Image", image_Lit_G);
    87.  
    88.  
    89. }
    To copy to clipboard, switch view to plain text mode 

    qt_GUI_homo.jpg


    Error from the compiler:

    Invalid parameter passed to C runtime function. Invalid parameter passed to C runtime function. OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file C:\OpenCV\modules\imgproc\src\color.cpp, line 2834 The program has unexpectedly finished.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt 4 - simple GUI for a OpenCV program

    Shouldn't you be asking that question on an OpenCV forum?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Qt + OpenCV, simple example
    By stampede in forum Newbie
    Replies: 33
    Last Post: 4th August 2015, 18:06
  2. Qt Creator Can't debug the program with OpenCV in Qt Creator
    By nimingzhe2008 in forum Qt Tools
    Replies: 6
    Last Post: 15th June 2012, 16:04
  3. Replies: 2
    Last Post: 25th March 2012, 23:57
  4. Simple C/C++ program
    By ct in forum General Programming
    Replies: 4
    Last Post: 25th October 2007, 15:11
  5. how to use progressBar in simple program
    By jyoti in forum Newbie
    Replies: 1
    Last Post: 1st December 2006, 13:16

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.