我一直在尝试为使用 OpenCV 和 C++ 的图像处理任务制作一个简单的界面,并使用 Qt 作为 GUI。我可以通过 GUI 加载图像,但是当我按下 pushbutton_3 时,将图像转换为灰度会导致有关 OpenCV 的错误。我确定我做错了什么。有人可以帮我吗?
请参阅以下文件:
//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);
}
来自编译器的错误:
传递给 C 运行时函数的参数无效。传递给 C 运行时函数的参数无效。OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file C:\OpenCV\modules\imgproc\src\color.cpp, line 2834 程序意外完成。
弹窗错误:
微软视觉 C++
此应用程序按要求以不寻常的方式终止。