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.