How to get a standalone executable using opencv and eclipse (windows)? - c++

I have written a small program to prepare an image which is later processed by a FPGA-Board. I need to have it run on any windows machine, even if opencv is not installed. I have read a lot of tutorials and answers on stack overflow, but none of that seems to work for me. I compiled opencv myself using Cmake and MinGW to build the static libraries (.a files). How do I get those in my project, which ones do I need and how do I link them? It seems like everytime I try to link the libraries I used before (when using dlls) seems to have some kind of dependency to other ones.
Thanks in advance,
Brillow
The error message is:
g++ "-LE:\\opencv\\staticlibs\\lib" -static-libgcc -static-libstdc++ -mwindows -o img_prep.exe main.o -lIlmImf -ljasper -lpng -ltiff -ljpeg -lz -lopencv_highgui249 -lopencv_imgproc249 -lopencv_core249
E:\opencv\staticlibs\lib/libopencv_highgui249.a(grfmt_jpeg.cpp.obj):grfmt_jpeg.cpp:(.text.unlikely._ZN2cvL16my_jpeg_load_dhtEP22jpeg_decompress_structPhPP9JHUFF_TBLS5_.constprop.52+0xce): undefined reference to `jpeg_alloc_huff_table'
E:\opencv\staticlibs\lib/libopencv_highgui249.a(grfmt_jpeg.cpp.obj):grfmt_jpeg.cpp:(.text$_ZN2cv11JpegEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0xe2): undefined reference to `jpeg_CreateCompress'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: E:\opencv\staticlibs\lib/libopencv_highgui249.a(grfmt_jpeg.cpp.obj): bad reloc address 0xe2 in section `.text$_ZN2cv11JpegEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
I have linked the following libraries in the MinGW linker:
Here's my code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <fstream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
if (argc != 2) {
cout << "Usage: img_prep InputImage" << endl;
return -1;
}
Mat img, img_gray;
Size size(800,600);
img = imread(argv[1], CV_LOAD_IMAGE_COLOR);
if (!img.data) {
cout << "Could not read the image!" << endl;
return -1;
}
cvtColor(img, img_gray, CV_RGB2GRAY);
resize(img_gray, img, size);
cout << "Image successfully converted!" << endl;
ofstream output;
output.open("output.img", ios::binary);
output.write((char *) img.data, img.rows * img.cols);
output.close();
imwrite("output.bmp", img);
cout << "Image saved!" << endl;
//Ergebnis anzeigen:
namedWindow("Display", WINDOW_AUTOSIZE);
imshow("Display", img);
waitKey(0);
return 0;
}

Related

OpenCV C++ cannot open video unless program is run as root

I'm trying to open a video file in Opencv C++, but it doesn't work in a weird way.
I'm on Fedora, and opencv is installed through dnf's opencv-devel package.
Here is the test code I'm using :
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/videoio/videoio.hpp>
int main(int argc, char *argv[])
{
std::string imagePath = "/home/me/picture.png";
cv::Mat image = cv::imread(imagePath);
std::cout << image << std::endl;
std::string videoPath = "/home/me/video.mp4";
cv::VideoCapture cap{ videoPath };
if(!cap.isOpened())
{
std::cout << "Error while opening video" << std::endl;
return -1;
}
cv::Mat startFrame;
cap >> startFrame;
std::cout << startFrame;
cap.release();
return 0;
}
Then, I Compile it with the libraries:
g++ -c -Wall -Wno-unknown-pragmas -I/usr/include/opencv4/ -o dist/obj/main.o src/main.cpp
g++ -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_videoio dist/obj/main.o -o dist/bin/main
And when I run it, the image is read fine, but the video shows an error.
However, when I run it as sudo, the video reads just fine. I searched around a lot and couldn't find someone with a similar problem, so I'm posting this here.
Also, the python version works fine, so it shouldn'ttm be a codec problem.

How to compile/run a cpp file in mac

I downloaded a webcam_face_pose_ex.cpp file from GitHub and now i want to compile and run it on my mac.
#include <dlib/opencv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <X11/Xlib.h>
using namespace dlib;
using namespace std;
int main()
{
try
{
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
cerr << "Unable to connect to camera" << endl;
return 1;
}
image_window win;
// Load face detection and pose estimation models.
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor pose_model;
deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
// Grab and process frames until the main window is closed by the user.
while(!win.is_closed())
{
// Grab a frame
cv::Mat temp;
if (!cap.read(temp))
{
break;
}
// Turn OpenCV's Mat into something dlib can deal with. Note that this just
// wraps the Mat object, it doesn't copy anything. So cimg is only valid as
// long as temp is valid. Also don't do anything to temp that would cause it
// to reallocate the memory which stores the image as that will make cimg
// contain dangling pointers. This basically means you shouldn't modify temp
// while using cimg.
cv_image<bgr_pixel> cimg(temp);
// Detect faces
std::vector<rectangle> faces = detector(cimg);
// Find the pose of each face.
std::vector<full_object_detection> shapes;
for (unsigned long i = 0; i < faces.size(); ++i)
shapes.push_back(pose_model(cimg, faces[i]));
// Display it all on the screen
win.clear_overlay();
win.set_image(cimg);
win.add_overlay(render_face_detections(shapes));
}
}
catch(serialization_error& e)
{
cout << "You need dlib's default face landmarking model file to run this example." << endl;
cout << "You can get it from the following URL: " << endl;
cout << " http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
cout << endl << e.what() << endl;
}
catch(exception& e)
{
cout << e.what() << endl;
}
}
I tried g++ webcam_face_pose_ex.cpp command but I get:
webcam_face_pose_ex.cpp:30:10: fatal error: 'dlib/opencv.h' file not found
#include <dlib/opencv.h>
^~~~~~~~~~~~~~~
1 error generated.
Was Wondering what I could do to fix this?
The Example File Is Not Meant to be Compiled Using g++
Read the following to learn a bit about the -I flag and #include statements:
The webcam_face_pose_ex.cpp is part of a larger project and you won't be able to compile it on its own because it depends on other files. The #include directive specifies that in order to compile this program, code from the file specified by #includemust be compiled first. This means the entire dlib must be downloaded before compiling webcam_face_pose_ex.cpp. This project also requires opencv2 so we can download it and place the opencv2 folder in the dlib project folder.
Now we can open terminal and change directory into the dlib project folder and compile the file using the following command:
g++ -I. examples/webcam_face_pose_ex.cpp
Note we're specifying the directory of where to find the files specified by #include using the -I parameter as -I. this means to search the current working directory for the files. There it will find the dlib folder and dlib/opencv.h.
How ever, this isn't enough. When you execute the command, you'll encounter an error opencv2/opencv_modules.hpp: No such file or directory.
Solution
The dlib project documentation states that the examples should be built using cmake. Make sure to use cmake to compile the examples.

Trying to compile OpenCV program but returns "cannot find -lippicv" (Complete linux beginner)

So I have OpenCV 3.1.0 set up on my computer running on Ubuntu 16.04 and I'm trying to run some very simple code to try and test OpenCV and whilst it recognises the opencv #include files, I continue to have compiler errors.
Here is my C++ code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
int main(int argc, char** argv) {
Mat color = imread("lena.jpg");
Mat gray = imread("lena.jpg",0);
imwrite("lenaGray.jpg", gray);
int myRow = color.cols-1;
int myCol = color.rows-1;
Vec3b pixel = color.at<Vec3b>(myRow, myCol);
cout << "Pixel value (B,G,R): (" << (int)pixel[0] << "," << (int)pixel[1] << "," << (int)pixel[2] << ")" << endl;
imshow("Lena BGR", color);
imshow("Lena Gray", gray);
waitKey(0);
return 0;
}
And I attempt to compile it on the Linux terminal as such:
g++ `pkg-config opencv --cflags --libs` main.cpp -o main
And this is the error that's returned:
/usr/bin/ld: cannot find -lippicv
collect2: error: ld returned 1 exit status
As the title said, I am a complete beginner at the Linux operating system whilst I am relatively competent at using the OpenCV library. I simply want to be able to use the OpenCV library and I am unable to with this error and I am very frustrated with it.
Thanks in advance for any help!

OpenCV example compilation results in core dump

I am compiling an example for OpenCV with the following code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
The compilation code is:
g++ -I/usr/local/include/opencv2 `pkg-config --cflags --libs opencv` -L /usr/local/share/OpenCV/3rdparty/lib/ opencv.cpp -o opencv -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_features2d -lopencv_imgcodecs
If I omit one of the libraries I add additionally, I get linking errors. When trying to run the program, I get an"Invalid machine code" error. How can that be solved?

crash without error message opencv under Qt

I have a problem under Qt, Actually I want to use opencv under it (ubuntu) and there is a crash.
If I compile under the terminal :
g++ pkg-config --cflags opencv example.cc -o output_file pkg-config --libs opencv
All is all right but under QT there is a crashed problem and I just read this message error :
Starting /home/quentin/build-test_opencv-Desktop_Qt_5_2_1_GCC_64bit-Release/test_opencv...
The program has unexpectedly finished.
/home/quentin/build-test_opencv-Desktop_Qt_5_2_1_GCC_64bit-Release/test_opencv crashed
This is my .pro :
QT += core
QT -= gui
TARGET = test_opencv
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
CONFIG += link_pkgconfig
PKGCONFIG += opencv
SOURCES += main.cpp
INCLUDEPATH += -I /usr/local/include/opencv
LIBS += `pkg-config opencv --libs`
and this is my main.cpp :
#include <QCoreApplication>
#include <iostream>
#include "cv.h"
#include "highgui.h"
using namespace std;
int main(int argc, char *argv[])
{
IplImage* img = cvLoadImage( "lena.png" );
cout << "Image WIDTH = " << img->width << endl;
cout << "Image HEIGHT = " << img->height << endl;
cvReleaseImage( &img );
return 0;
}
Most likely cvLoadImage fails and returns nullptr. You never bother checking for that.
What version of openCV do you use? What berak means is that IplImage is not used in the newer versions of openCV. Use Mat instead of IplImage. Try this code and tell me what happens:
#include <QCoreApplication>
#include <iostream>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include "opencv2/highgui/highgui.hpp"
int main(int argc, char *argv[])
{
Mat* img = new cv::Mat(cv::imread("lena.png",CV_LOAD_IMAGE_COLOR));
if(img == NULL){
perror("Could not load image");
}
std::cout << "Image WIDTH = " << img->cols << std::endl;
std::cout << "Image HEIGHT = " << img->rows << std::endl;
img->release();
return 0;
}
This will work in opencv 2.4.X. Also make sure your image is in the same folder as your program. Please tell me of any error.