‘Mat’ was not declared in this scope opencv - c++

I followed this guide to install openCV on Ubuntu:
https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html
When I try to execute the following program:
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <math.h>
#include <tiffio.h>
using namespace std;
int main(){
string imageName("images/400nm.tif");
TIFF* tif = TIFFOpen(imageName.c_str(), "r");
Mat image;
return 0;
}
I get the following error executing the command "g++ ssim.cpp -o ssim -ltiff":
ssim.cpp: In function ‘int main()’: ssim.cpp:19:3: error: ‘Mat’ was
not declared in this scope Mat image; ^~~ ssim.cpp:19:3: note:
suggested alternative: In file included from
/usr/local/include/opencv2/core.hpp:59:0,
from /usr/local/include/opencv2/core/core.hpp:48,
from ssim.cpp:2: /usr/local/include/opencv2/core/mat.hpp:771:18: note: ‘cv::Mat’
class CV_EXPORTS Mat
Does somebody know why I get this and how to solve it?
I'm new at using opencv and libtiff so I have no idea about what to do to solve...

It's not really necessary to build openCV from source. Try installing it with
sudo apt-get install libopencv-dev
and try to compile it again.
Also like you said in your comment, make sure you either use namespace cv or cv::Mat.

Using the command:
g++ ssim.cpp -o ssim -ltiff -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs
I don't get compiler errors but when I try to execute the command ./ssim I get:
./ssim: error while loading shared libraries: libopencv_core.so.3.4: cannot open shared object file: No such file or directory

Related

opencv2/core/eigen.hpp Eigen: does not name a type [duplicate]

I have trouble in using Eigen3 Library along with OpenCV application in C++.
I have installed Eigen3 library on my Ubuntu using the following command:
sudo apt-get install libeigen3-dev
I am able to compile and use sample Eigen3 applications (Eigen3 library is installed and it works) when I use the following command to compile.
g++ -I/usr/include/eigen3 Eig.cpp -o Eig
I want to use the installed Eigen library with OpenCV.
I compiled OpenCV source with following flags:
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=OFF -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON USE_EIGEN=/usr/include/eigen3 ..
My OpenCV code includes the following headers and namespace:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <assert.h>
#include <opencv2/core/eigen.hpp>
using namespace cv;
using namespace std;
using namespace Eigen;
However, when I normally compile OpenCV application, my compiler gives me the following error:
In file included from Read.cpp:6:
/usr/local/include/opencv2/core/eigen.hpp:54: error: expected ‘,’ or ‘...’ before ‘::’ token
/usr/local/include/opencv2/core/eigen.hpp: In function ‘void cv::eigen2cv(int)’:
/usr/local/include/opencv2/core/eigen.hpp:56: error: ‘src’ was not declared in this scope
/usr/local/include/opencv2/core/eigen.hpp:56: error: ‘Eigen’ is not a class or namespace
/usr/local/include/opencv2/core/eigen.hpp:60: error: ‘dst’ was not declared in this scope
/usr/local/include/opencv2/core/eigen.hpp:66: error: ‘dst’ was not declared in this scope
How do I solve this problem?
I just had to include
#include <Eigen/Dense>
before including OpenCV headers thats all. I compiled them by including the Eigen lib headers and OpenCV lib headers.
First i would double check that the eigen include directions are found.
You can use a CMakeList.txt to do so (and you should use the cmake functions to find headers and link to libraries instead of compiler flags)
Next you could try to remove the using namespaces
using namespace cv;
using namespace std;
using namespace Eigen;

how to fix the error: ‘::max_align_t’?

I get the error
"/usr/include/c++/5/cstddef:51:11: error: ‘::max_align_t’ has not been declared
using ::max_align_t;
^"
So I should update the libraries because I find this solution:
"A workaround until libraries get updated is to include <cstddef> or <stddef.h> before any headers from that library."
I wrote some command on the Ubuntu terminal such as:
bash $ sudo apt-get install apt-file
bash $ sudo apt-file update
bash $ apt-file search stddef.h
Then still the error exist.
Thank you
In the .cpp file where this compile error occurs you need to add
#include <cstddef>
before any of the other headers, e.g.
main.cpp (broken)
#include <cstdio>
int main()
{
using ::max_align_t;
puts("Hello World");
return 0;
}
Try to compile that:
$ g++ -std=c++11 -o test main.cpp
main.cpp: In function ‘int main()’:
main.cpp:5:10: error: ‘::max_align_t’ has not been declared
using ::max_align_t;
^
Then fix it:
main.cpp (fixed)
#include <cstddef>
#include <cstdio>
int main()
{
using ::max_align_t;
puts("Hello World");
return 0;
}
Compile and run it:
$ g++ -std=c++11 -o test main.cpp
$ ./test
Hello World
I compiled some code with GNU C++ 4.9 on CentOS, and the issue was not solved by ensuring top position #include (or by the older header name stddef.h).
Weird enough, I searched all header files of the compiler libraries for the global definition of max_aling_t as declared in the offending using declaration... and found none! Could it be in some 'internal compiled header?
So I simply commented-out the "using ::max_align_t;" line in the standard header (not proud of doing this indeed) and it solved the problem... and code is running...
if anyone can explain what is the meaning/impact of this max_align_t ?
I also commented-out the using ::max_align_t; line in /usr/include/c++/4.9/cstddef, while, code is running, but I don't know if there are any consequences by doing this...

Tesseract-ocr, baseapi.h fatal error: platform.h file not found

I am working on engineering work on license plate recognition and therefore started from a simple code. Namely, I downloaded all the libraries or OpenCV, Tesseract and Leptonica but when you try to compile I have a problem with the file baseapi.h located in the library Tesseract.
Compiles the following command: g++ main.cpp -lopencv_core -lopencv_highgui -lopencv_imgproc -ltesseract
Error The compiled: tesseract-ocr/api/baseapi.h:27:22: fatal error: platform.h: No such file or directory
The beginning of my code:
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "leptonica-1.69/src/allheaders.h"
#include "tesseract-ocr/api/baseapi.h"
using namespace std;
using namespace tesseract;
using namespace cv;
...
try to install dev packages
$ sudo apt install tesseract-ocr-dev
$ sudo apt install libleptonica-dev

C++ Opencv Undefied Refrence contrib module

I am getting the error Undefined refrence to cv::createFisherFaceRecognizer.
My header looks like this
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
The line that is causing the error is
Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
I have also tried this with the same error
Ptr<FaceRecognizer> model = createFisherFaceRecognizer(0, DBL_MAX);
I'm compiling the program with:
g++ -L/usr/lib/uv4l/uv4lext/armv61 -luv4lext -Wl,-rpath,'/usr/lib/uv4l/uv4lext/armv6l' opencv_test.cpp -lopencv_core -lopencv_contrib -lopencv_imgproc -lopencv_objdetect -lopencv_highgui -o opencv_test
and I am getting the error:
opencv_test.cpp:(.text+0x5d8):undefined refrence to 'cv::createFisherFaceRecognizer(int,double)'
The strange thing is that the other libs seem to be linked fine so I don't know how just one library would be improperly linked.
This is running in on a raspberry pi using the raspbian distro
Thanks for any help.
After trying to figure out what was happening... I used apt-get remove to remove the contrib module and then re-installed it... I now believe that this was a symptom of a larger linking error because I have 2 different versions of opencv running on the same machine... In any event I hope this helps someone out.

Using Eigen Library with OpenCV 2.3.1

I have trouble in using Eigen3 Library along with OpenCV application in C++.
I have installed Eigen3 library on my Ubuntu using the following command:
sudo apt-get install libeigen3-dev
I am able to compile and use sample Eigen3 applications (Eigen3 library is installed and it works) when I use the following command to compile.
g++ -I/usr/include/eigen3 Eig.cpp -o Eig
I want to use the installed Eigen library with OpenCV.
I compiled OpenCV source with following flags:
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=OFF -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON USE_EIGEN=/usr/include/eigen3 ..
My OpenCV code includes the following headers and namespace:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <assert.h>
#include <opencv2/core/eigen.hpp>
using namespace cv;
using namespace std;
using namespace Eigen;
However, when I normally compile OpenCV application, my compiler gives me the following error:
In file included from Read.cpp:6:
/usr/local/include/opencv2/core/eigen.hpp:54: error: expected ‘,’ or ‘...’ before ‘::’ token
/usr/local/include/opencv2/core/eigen.hpp: In function ‘void cv::eigen2cv(int)’:
/usr/local/include/opencv2/core/eigen.hpp:56: error: ‘src’ was not declared in this scope
/usr/local/include/opencv2/core/eigen.hpp:56: error: ‘Eigen’ is not a class or namespace
/usr/local/include/opencv2/core/eigen.hpp:60: error: ‘dst’ was not declared in this scope
/usr/local/include/opencv2/core/eigen.hpp:66: error: ‘dst’ was not declared in this scope
How do I solve this problem?
I just had to include
#include <Eigen/Dense>
before including OpenCV headers thats all. I compiled them by including the Eigen lib headers and OpenCV lib headers.
First i would double check that the eigen include directions are found.
You can use a CMakeList.txt to do so (and you should use the cmake functions to find headers and link to libraries instead of compiler flags)
Next you could try to remove the using namespaces
using namespace cv;
using namespace std;
using namespace Eigen;