PDA

View Full Version : Qt 4 - simple GUI for a OpenCV program



antrofite
24th September 2012, 11:49
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:


//mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFileDialog>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>

#include <QtCore/QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/types_c.h>
#include <opencv2/imgproc/imgproc.hpp>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButton_clicked();

void on_pushButton_2_clicked();

void on_pushButton_3_clicked();




private:
Ui::MainWindow *ui;
//Images variables
cv::Mat image_Idl;
cv::Mat image_Lit;
cv::Mat image_Idl_G;
cv::Mat image_Lit_G;
double threshHold;

};

#endif // MAINWINDOW_H





//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QtCore/QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/types_c.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <QSpinBox>
#include <QSlider>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

ui->horizontalSlider->setRange(0,255);
ui->spinBox->setRange(0,255);

connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),ui->spinBox,SLOT(setValue(int)));
connect(ui->spinBox,SIGNAL(valueChanged(int)),ui->horizontalSlider,SLOT(setValue(int)));

}

MainWindow::~MainWindow()
{
delete ui;
}


void MainWindow::on_pushButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,tr("Load Lit Image"),".",tr("Image Files (*.png *.jpg *.jpeg *.bmp)"));
image_Lit = cv::imread(fileName.toAscii().data());
cv::namedWindow("Lit Image");
cv::imshow("Lit Image", image_Lit);
}

void MainWindow::on_pushButton_2_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,tr("Load Lit Image"),".",tr("Image Files (*.png *.jpg *.jpeg *.bmp)"));
image_Idl = cv::imread(fileName.toAscii().data());
cv::namedWindow("Ideal Lit");
cv::imshow("Ideal Lit", image_Idl);

}






void MainWindow::on_pushButton_3_clicked()
{
//Converstions


//Convert Lit to gray



cv::cvtColor(image_Lit, image_Lit_G,CV_RGB2GRAY);

//Convert Ideal gray


cv::cvtColor(image_Idl, image_Idl_G,CV_RGB2GRAY);


//Threshold the Images to a designated value
// Lit

threshHold = ui->horizontalSlider->value();

cv::threshold(image_Lit_G,image_Lit_G, threshHold,255,cv::THRESH_BINARY);
cv::namedWindow("Gray Scaled Image");
cv::imshow("Gray Scaled Image", image_Lit_G);


}

8242


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.

wysota
24th September 2012, 16:09
Shouldn't you be asking that question on an OpenCV forum?