Qt + OpenCV grayscale error - c++

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);
}
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.
Pop up window error:
Microsoft Visual C++
This application as requested to terminate in an unusual way.

#include "iostream"
#include "cv.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("C:\\lena.jpg");
cvtColor(image,image,CV_RGB2GRAY);
imshow("test",image);
waitKey();
return 0;
}
above code(console app in Qt..) is working....it shows a gray image...try to include the cv.h file...

I have recompiled the above program after updating my windows7 machine and astonishing it worked!
Its really weird... I didn't made any changes to the program (at least I can't remenber doing anythin significantly different) but after reinstalling comodo firewal it worked.....
Thanks guys, for your help.

Related

GUI gets blank while processing

I have an application which runs a function in background, Here is my main class
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "gdal_priv.h"
#include "cpl_conv.h" // for CPLMalloc()
#include "ogr_srs_api.h"
#include <QDir>
#include <QFile>
#include <QDebug>
#include <iostream>
#include <cstring>
#include <string>
#include <QFileDialog>
#include <QThread>
#include <QtConcurrent/QtConcurrent>
#include "databasemanager.h"
#include <sqlite_connection.h>
#include "locationinfo.cpp"
#include "gdal_wrap.h"
#include "gdalwarper.h"
#include "gdal_dem.h"
#include "shapefile_importer.h"
#include "gdal_contour.h"
using namespace QtConcurrent;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->startProcess, SIGNAL (released()), this, SLOT (StartProcess()));
}
void MainWindow::StartProcess(){
sendinfo("Starting Process... Note that the input images <b>must<\b> be in ESPG:3857.");
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
if(dir==""){
sendwarning("nothing is selected.");
return;
}
sendinfo("The selected folder is: "+dir);
QFuture<void> f1 = run(std::bind1st(std::mem_fun(&MainWindow::RunProcess), this),dir);
f1.waitForFinished();
}
void MainWindow::RunProcess(QString dir){
......
}
MainWindow::~MainWindow()
{
delete ui;
}
As you can see I am running my function in a QtConcurrent function, so based on my googles there must not any blank GUI but when I run application it makes GUI blank in linux and if I do not click some place on GUI it will close my application.
What am I doing wrong here?

How can scrollable image widget custom on Qt

I have problem to scrollable for my custom image Widget.
This is my custom image Widget.
imagewidget.h:
#ifndef IMAGEWIDGET_H
#define IMAGEWIDGET_H
#include <QWidget>
#include <QImage>
#include <QString>
#include <QPaintEvent>
#include <QAbstractScrollArea>
class ImageWidget: public QWidget
{
public:
explicit ImageWidget(QWidget *parent = 0);
QImage m_Image;
void loadImage(const QString &fileName);
protected:
void paintEvent(QPaintEvent *event);
};
#endif // IMAGEWIDGET_H
imagewidget.cpp:
#include "imagewidget.h"
#include <QPainter>
#include <QPoint>
#include <QDebug>
#include <QScrollBar>
ImageWidget::ImageWidget(QWidget *parent): QWidget(parent)
{
}
void ImageWidget::loadImage(const QString &fileName)
{
if(!fileName.isNull()){
m_Image.load(fileName);
this->update();
qDebug()<<"Load Image"<<endl;
}
}
void ImageWidget::paintEvent(QPaintEvent *event)
{
QPainter p(this);
if(!m_Image.isNull()){
p.drawImage(QPoint(0,0),m_Image);
}
qDebug()<<"Paint Event"<<endl;
}
And this is mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QScrollArea>
#include <QString>
#include <QFileDialog>
#include <QWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
scrollArea(new QScrollArea())
{
ui->setupUi(this);
m_Image = new ImageWidget(this);
scrollArea->setWidgetResizable(true);
scrollArea->setWidget(m_Image);
setCentralWidget(scrollArea);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "/", tr("Image Files (*.png *.jpg *.bmp *.tif *.tiff)"));
m_Image->loadImage(fileName);
m_Image->adjustSize();
this->update();
}
With a large image, the image cannot diplay with scroll bar.
My result is below. Thank you for reading.
You forget to set ImageWidget minimumsize.
void ImageWidget::loadImage(const QString &fileName)
{
if(!fileName.isNull()){
m_Image.load(fileName);
setMinimumSize(m_Image.size()); //add this line
this->update();
qDebug()<<"Load Image"<<endl;
}
}
ScrollArea shows scrollbars when its size is smaller than content's widget (ImageWidget in this case). You just load image and paint it on widget but not change widget's size.
:) My first answer on stackoverflow.

qt5 error occur Poppler::Document* document

I'm making PDF viewer using qt and poppler library but, the error occurred running this program
error was 'The program has unexpectedly finished.' error
occur Poppler::Document* document =
Poppler::Document::load(filename);
When I erase Poppler::Document::load(filename) error not occurred
How to solve it?
I found an error code below but follow this example poppler Qt5
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/opencv.hpp>
#include <QFileDialog>
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>
#include <poppler-qt5.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
cv::Mat image = cv::imread("/home/googolhkl/사진/IMG_20150925_1.jpg");
cv::imshow("display",image);
QString filename;
Poppler::Document* document = Poppler::Document::load(filename);
if (!document || document->isLocked()) {
// ... error message ....
delete document;
return;
}
}
You have to load existing file, but in your example the file name is empty:
QString filename;

How to rotate the second hand of the clock with Qt

I am using the code below to try to rotate the pointer simulating the second hand of the clock, but when he turns it cuts, the square of the background is fixed and it seems I'm not able to rotate all:
QPixmap shipPixels(":/new/prefix1/imagem/ponteiro.png");
QPixmap rotatePixmap(shipPixels.size());
rotatePixmap.fill(Qt::transparent);
QPainter p(&rotatePixmap);
p.translate(rotatePixmap.size().width() / 2, rotatePixmap.size().height() / 2);
p.rotate(90);
p.translate(-rotatePixmap.size().width() / 2, -rotatePixmap.size().height() / 2);
p.drawPixmap(0, 0, shipPixels);
p.end();
shipPixels = rotatePixmap;
ui->label->setPixmap(rotatePixmap);
The pointer looks like this:
Now with it rotated 90 º
Qt Analog Clock example:
http://qt-project.org/doc/qt-5/qtwidgets-widgets-analogclock-example.html
Maybe before rotating QPixmaps, try drawing a line. After the line is in place and drawing correctly work backwards from there.
UPDATE:
Some sample code for rotating images.
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPaintEvent>
#include <QPixmap>
#include <QTime>
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
signals:
public slots:
void paintEvent(QPaintEvent *);
private:
QPixmap bg;
QPixmap second_hand;
QTime time;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <QPainter>
#include <QTimer>
#include <QTime>
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
time.restart();
this->resize(256, 256);
// both images are 256x256 in this example
bg.load("./images/bg.png");
second_hand.load("./images/second_hand.png");
QTimer * t = new QTimer;
t->setSingleShot(false);
t->setInterval(15);
QObject::connect(t,SIGNAL(timeout()), this, SLOT(update()));
t->start();
}
void Widget::paintEvent(QPaintEvent * e)
{
QPainter p(this);
p.drawPixmap(QPoint(0,0),bg);
qreal seconds = ((qreal)(time.elapsed() % 60000))/1000;
p.translate(this->width()/2, this->height()/2);
p.rotate(seconds/60*360);
p.drawPixmap(QPoint(-this->width()/2, -this->height()/2),second_hand);
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
Hope that helps.

How to save bandwidth when using QGraphicsview with X-redirection

I have a large QGraphicsScene that I want to display, currently done by QGraphicsView.
When using locally everything is fine, but as soon as I use the App with ssh -X it is terribly slow. Of course some data always has to be transferred, but how could the bandwidth be saved if nothing changes, for example when only panning the image?
This is my QGraphicsView:
#include "graphicsview.h"
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QDebug>
#include <QMouseEvent>
#include <QWheelEvent>
#include <cmath>
#include <QPixmapCache>
GraphicsView::GraphicsView(QWidget *parent) :
QGraphicsView(parent)
{
QPixmapCache::setCacheLimit(16777216); //did not do the trick either
this->setMouseTracking(true);
setTransformationAnchor(AnchorUnderMouse);
this->setDragMode(QGraphicsView::ScrollHandDrag);
scene = new QGraphicsScene(this);
setScene(scene);
background = QPixmap(8000,8000);
qDebug() << background.rect();
scene->setSceneRect(background.rect());
scene->setBackgroundBrush(QBrush(background));
show();
}
void GraphicsView::wheelEvent(QWheelEvent* event) {
scale(pow(2,event->delta()/240.0), pow(2,event->delta()/240.0));
}