Opencv2 cvtColor() does not work - qt

I am using OpenCV2 on Ubuntu 12.04. I can successfully run image read-display codes.
However I am not able to run codes with inbuilt functions eg. cvtColor()
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
int main(int argc, char *argv[])
{
cv::Mat image = cv::imread("img.jpg");
if( image.data == NULL )
{
printf( "file cannot be loaded\n");
return 1;
}
cv::namedWindow("My");
cv::imshow("My", image);
cv::Mat result;
cv::cvtColor(image, result, CV_BGR2Luv);
cv::imwrite("outImg.jpg", result);
cv::waitKey(0);
return 0;
}
I am using Qt-creator for my OpenCV
After compiling with --libs, --cflags I get following compiler error:
make: Entering directory `/home/swaroop/Work/ai-junkies/cuda/uc_davis/opencv2.x/OpenCV2Test'
g++ -g -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4 -I/usr/include/opencv -I. -o main.o main.cpp
main.cpp: In function 'int main(int, char**)':
main.cpp:22:29: error: 'CV_BGR2Luv' was not declared in this scope
main.cpp:22:39: error: 'cvtColor' was not declared in this scope
Please help me fix this.

cvtColor declared in opencv2/imgproc/imgproc.hpp
keep in mind it's #include not #import
#include <opencv2/imgproc/imgproc.hpp>

Alternatively, if you are testing things and not concerned with overdoing the includes, you can simply have one line:
#include <opencv2/opencv.hpp>
and it will include most opencv2 headers.

Related

Separate class for Qt app

Under Fedora 27 Linux I want to try out a simple Hello World example with Qt and C++. I've created a separate GUI class which is populated with a label and a button. The init macro “Q_OBJECT” was commented out, because it produces an error message. ("undefined reference to vtable for MainWindow") After compiling the sourcecode, a window is shown on the screen, but the label and the button is missing. I mean, the compilation process works, the gui starts, but the result is not as expected.
If i put all the commands in the main-function without creating a separate class, Qt works great. The problem is to define a separate class which inherits from QMainWindow. Most of the example tutorials out there are working with Qt-Creator, but i want to do it from scratch on the command line level. Any hints are welcome.
// compile: clang++ -std=c++14 -lQtCore -lQtGui file.cpp
#include <QtGui/QApplication>
#include <QtGui/QPushButton>
#include <QtGui/QTextEdit>
#include <QtGui/QLabel>
#include <QtGui/QMainWindow>
#include <iostream>
class MainWindow : public QMainWindow {
//Q_OBJECT
public:
QWidget window;
MainWindow() {
window.resize(500, 400);
QLabel label1( "input:" , &window);
QPushButton button ("run", &window);
button.move(0,300);
}
void show() {
window.show();
}
};
int main(int argc, char **argv)
{
QApplication app (argc, argv);
MainWindow mywindow;
mywindow.show();
return app.exec();
}
QLabel label1 and QPushButton button are local variables in the constructor MainWindow::MainWindow(). Hence, when constructor returns they are going out of scope and are destroyed/deleted. You have to make them member variables. (This is actually a C++ issue.)
Additionally, I recommend to learn about layouting in Qt. Qt doc. provides examples e.g. Basic Layouts Example
Here is an even smaller I composed by modifying the OP:
testQMainWindow.cc:
#include <QtWidgets>
class MainWindow: public QMainWindow {
private:
QWidget central;
QHBoxLayout hBox;
QLabel label;
QPushButton button;
public:
MainWindow();
};
MainWindow::MainWindow():
hBox(this),
label("input:", this),
button("run", this)
{
hBox.addWidget(&label, 1);
hBox.addWidget(&button, 0);
central.setLayout(&hBox);
setCentralWidget(&central);
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MainWindow win;
win.show();
return app.exec();
}
testQMainWindow.pro:
SOURCES = testQMainWindow.cc
QT += widgets
Compile and test:
$ qmake-qt5 testQMainWindow.pro
$ make
g++ -c -fno-keep-inline-dllexport -D_GNU_SOURCE -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtWidgets -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -I/usr/lib/qt5/mkspecs/cygwin-g++ -o testQMainWindow.o testQMainWindow.cc
g++ -o testQMainWindow.exe testQMainWindow.o -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
$ ./testQMainWindow
I'm working on Windows 10. I did the sample in cygwin which is the closest to Linux I have at hand.
i want to do it from scratch on the command line level
In this case you either have to take care for all the necessary additional build steps, includes and linkage yourself, as Dmitry already hinted in a comment. Or you just use qmake, which is highly recommended, because it takes care of all the Qt specific stuff.

Cannot convert objects to pointers C++

I've stayed up all night trying to figure this out (it's now 7am where I'm at...).
I'm having trouble setting the address of an instantiated object to a pointer. Here's the main function:
#include "position_vector.h"
int main(){
PositionVector res = PositionVector(10);
PositionVector * ptr;
ptr = &res; // <--- WHERE IT BREAKS
}
A stripped down version of the h file "position_vector.h":
#include<iostream>
typedef uint32_t word_t;
class PositionVector {
public:
word_t * vec;
/*some other member variables */
PositionVector();
PositionVector(size_t len);
PositionVector & operator & ();
PositionVector & operator !();
~PositionVector();
/*some other member functions*/
void resize(size_t len);
};
I have another cpp file that defines all the methods in the class.
This is part of some larger set of code but here's the compile that fails:
g++-4.9 -std=c++11 -Werror -Wall -Wextra -g -Isrc -ggdb -c -o bin/main.o src/main.cpp
It fails with the error:
g++-4.9 -std=c++11 -Werror -Wall -Wextra -g -Isrc -ggdb -c -o bin/main.o src/main.cpp
src/main.cpp: In function ‘int main()’:
src/main.cpp:27:9: error: cannot convert ‘PositionVector’ to ‘PositionVector*’ in assignment
ptr = &res;
^
I must be missing something super basic but I've just pulled an all nighter and I have to run to work... so I cant really think full well any more.
You've overloaded operator& in your class:
class PositionVector {
// ...
PositionVector & operator & ();
};
When you than try to take the address, the compiler calls your overloaded member function which returns a PositionVector&. This cannot be assigned to a PositionVector* and thus you get the error.

Qt Moc generates code with undefined behaviour

I have a simple class which has a QT signal that gives me trouble when compiling the moc generated code. I don't use the qmake buildsystem, but my scons build script calls qt' moc command directly.
The relevant source file "write_qstring.h" is:
#ifndef LOG_WRITE_QSTRING_POLICY_H
#define LOG_WRITE_QSTRING_POLICY_H
#include <QObject>
#include <QString>
namespace Log
{
class WriteQString : public QObject
{
Q_OBJECT
public:
signals:
void Changed(QString newString);
};
}
#endif // LOG_WRITE_QSTRING_POLICY_H
I run the moc compiler with the command:
/opt/Qt/5.2.0/gcc_64/bin/moc -DQT_CORE_LIB
-I/opt/Qt/5.2.0/gcc_64/include/QtCore -I/opt/Qt/5.2.0/gcc_64/include -o moc_write_qstring.cc write_qstring.h
and then I compile the generated moc_write_qstring.cc using clang++:
clang++ -o moc_write_qstring.o -c -Weverything -pedantic -g -std=c++11
-fcxx-exceptions -pthread -fdiagnostics-fixit-info -fPIC -Wno-c++98-compat -Wno-documentation-unknown-command -Wno-documentation -Wno-padded -Wno-weak-vtables -Wno-exit-time-destructors -Wno-global-constructors -isystem/opt/Qt/5.2.0/gcc_64/include -isystem/opt/Qt/5.2.0/gcc_64/include/QtCore -DQT_CORE_LIB -I/opt/Qt/5.2.0/gcc_64/include/QtCore moc_write_qstring.cc
This gives a warning about undefined behaviour:
warning: dereference of type '_t ' (aka 'void
(Log::WriteQString::*)(QString)') that was reinterpret_cast from type
'void **' has undefined behavior [-Wundefined-reinterpret-cast]
With the relevant line being:
if (*reinterpret_cast<_t *>(func) ==
static_cast<_t>(&WriteQString::Changed)) {
Since the compiler is warning about undefined behaviour I can only think the code is broken, but why is the metacompiler emitting broken code? What did I do wrong here? is it a c++11 problem??
Output of clang++ --version
Ubuntu clang version 3.5-1~exp1 (trunk) (based on LLVM 3.5) Target:
x86_64-pc-linux-gnu Thread model: posix
Output of qmake --version
QMake version 3.0 Using Qt version 5.2.0 in /opt/Qt/5.2.0/gcc_64/lib

Using QApplication with command line arguments

QApplication::QApplication ( int & argc, char ** argv )
Initializes the window system and constructs an application object
with argc command line arguments in argv.
Warning: The data referred to by argc and argv must stay valid for the
entire lifetime of the QApplication object. In addition, argc must be
greater than zero and argv must contain at least one valid character
string.
From this link: http://doc.qt.io/qt-4.8/qapplication.html#QApplication
What can be the arguments to the executable file? Any examples?
I tried specifying something like:
anisha#linux-dopx:~/Desktop/notes/qt> make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../qtsdk-2010.05/qt/mkspecs/linux-g++-64 -I. -I../../../qtsdk-2010.05/qt/include/QtCore -I../../../qtsdk-2010.05/qt/include/QtGui -I../../../qtsdk-2010.05/qt/include -I. -I. -o widgets.o widgets.cpp
g++ -m64 -Wl,-O1 -Wl,-rpath,/home/anisha/qtsdk-2010.05/qt/lib -o qt widgets.o -L/home/anisha/qtsdk-2010.05/qt/lib -lQtGui -L/home/anisha/qtsdk-2010.05/qt/lib -L/usr/X11R6/lib64 -lQtCore -lpthread
anisha#linux-dopx:~/Desktop/notes/qt> ./qt 2 f g
anisha#linux-dopx:~/Desktop/notes/qt>
Nothing special happened, nor I knew what I was doing or what I was supposed to do.
EDIT 1: The code on which I tried the ./qt -style=windows.
#include <QtGui>
int main (int argc, char *argv[])
{
QApplication app (argc, argv);
QWidget objQWidget;
objQWidget.show ();
objQWidget.resize (320, 240);
objQWidget.setWindowTitle ("Text to be shown on the title bar\n");
// Adding a "child" widget.
QPushButton *objQPushButton = new QPushButton ("Text to be shown on the button", &objQWidget);
objQPushButton->move (100, 100);
objQPushButton->show ();
return app.exec ();
}
The arguments passed in the constructor are later accessible through the static method
QStringList QCoreApplication::arguments(). By this, command line arguments can be handled everywhere in your code.
Continue reading that documentation. The set of flags QApplication acts on is listed there.
Try for example:
./qt -style=windows
The arguments that QApplication doesn't deal with are simply left alone. The ones it does process are removed (which is why that function takes non-const arguments).
The suggestion about using QCoreApplication is only recommended of you have a console application. If you are using a QApplication instead, and want to access command-line arguments from inside a QWidget, you can do it with the global pointer qApp:
Here you can find the documentation from Nokia, or here from qt-project.org . In the documentation browser of Qt Creator I couldn't find it, so it is at best not that easily accessible.
so you can find:
int my_argc = qApp->arguments().count();
QString my_argv_0 = qApp->arguments.at(0);
...
and so on.
I know this question is old, but took me some time to find a way to do it from within my Main Window, so hope this helps someone else.
Thanks, Dissident penguin! This helped me a lot!
Just note that:
QString my_argv_0 = qApp->arguments.at(0);
should be replaced with:
QString my_argv_0 = qApp->arguments().at(0);
(note the additional () after 'arguments')

Problems on Qt on NetBeans

After having solved a Qt configured problem on my system, I have now installed NetBeans and everything is OK. It is just that I have the following problem. Here is the code:
#include <QtGui/QApplication>
#include <QDir>
#include <QFileInfo>
#include <QtDebug>
int main(int argc, char **argv) {
foreach(QFileInfo drive,QDir::drives()){
qDebug()<<"Drive: "<<drive.absolutePath();
QDir dir=drive.dir();
dir.setFilter(QDir::Dirs);
foreach(QFileInfo rootDirs,dir.entryInfoList())
qDebug()<< " "<<rootDirs.fileName();
}
return 0;
// return app.exec();
}
and errors are
/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/david/NetBeansProjects/QtApplication_1'
/usr/bin/qmake VPATH=. -o qttmp-Debug.mk nbproject/qt-Debug.pro
mv -f qttmp-Debug.mk nbproject/qt-Debug.mk
/usr/bin/make -f nbproject/qt-Debug.mk dist/Debug/GNU-Linux-x86/QtApplication_1
make[2]: Entering directory `/home/david/NetBeansProjects/QtApplication_1'
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -Inbproject -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -Inbproject -I. -o build/Debug/GNU-Linux-x86/Qt1.o Qt1.cpp
Qt1.cpp:7: warning: unused parameter ‘argc’
Qt1.cpp:7: warning: unused parameter ‘argv’
g++ -o dist/Debug/GNU-Linux-x86/QtApplication_1 build/Debug/GNU-Linux-x86/Qt1.o build/Debug/GNU-Linux-x86/main.o -L/usr/lib -lQtGui -lQtCore -lpthread
build/Debug/GNU-Linux-x86/main.o: In function `main':
/home/david/NetBeansProjects/QtApplication_1/main.cpp:10: multiple definition of `main'
build/Debug/GNU-Linux-x86/Qt1.o:/home/david/NetBeansProjects/QtApplication_1/Qt1.cpp:7: first defined here
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/QtApplication_1] Error 1
make[2]: Leaving directory `/home/david/NetBeansProjects/QtApplication_1'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/david/NetBeansProjects/QtApplication_1'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
How can I fix this problem?
According to that output, you are compiling two source files Qt1.cpp and main.cpp, both of which define the function main(). You need to remove one of those two files from your project or remove the definition of main() from one of them.
As an added step to get rid of the warning about unused parameters in main(), change it to
main(int /*argc*/, char **/*argv*/)
until you need to use those arguments.

Resources