Struggling with dlib, linker errors and save_jpeg in XCode - c++

First, I am new to C++ and dlib but I have successfully built the examples and started working on my own project. Things have been progressing smoothly until I try to save a jpeg. Attempting to compile code using dlib::save_jpeg throws a linker error and I cannot track down the solution. I have attempted to add #define DLIB_JPEG_SUPPORT above and below my #includes but no luck. I am using XCode and used cmake -G "Xcode" .. when I compiled the examples. Relevant code below. Since I am on a Mac, I have added header and library search paths for X11 (for dlib gui), OpenCV, and DLIB. I have libjpeg.dylib and linked that to my project with and without #define DLIB_JPEG_SUPPORT in main.cpp. Is there some other build setting I need to specify? Thank you in advance for your help.
Finally, I have seen other questions and pages about dlib and libjpeg issues but no luck yet. And yes I have source.cpp included in the project.
// the standard stuff
#include <string>
#include <iostream>
#include <unistd.h>
// opencv mat object
#include <opencv2/opencv.hpp>
// dlib>
#include <dlib/opencv.h>
#include <dlib/image_io.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_transforms.h>
int main(int argc, const char * argv[]) {
// retrieving images from a TCP connection
// decode data stream
img = cv::imdecode(rawImage, CV_LOAD_IMAGE_COLOR);
// perform image processing
dlib::cv_image<dlib::bgr_pixel> d_image(img);
// finally save the result to jpg
std::string fname = argv[1] + std::to_string(image_id) + ".jpg";
dlib::save_jpeg(d_image, fname); // <- line that won't compile
return 0;
}

After quit a bit of struggling and side-by-side comparisons I finally found the issue. In XCode go to to Build Settings and modify Other Linker Flags, Run Search Paths, and Other C++ Flags to match the compiled and working face_ex example. I wholesale copied all of those flags and included a missing libjpeg.dylib and was able to get things running. It should look something like this for the C++ flags . Hope this helps the next person.

Related

Asio .hpp not found in VSCode, but is included in path

I'm trying to use asio in VSCode with C++. I keep getting the warning:
fatal error: 'asio.hpp' file not found
#include <asio.hpp>
Using the code:
#include <iostream>
#include <asio.hpp>
#include <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>
int main(){
asio::error_code ec;
//Create a 'context which is like a platform specific interface
asio::io_context context;
//Get the address of somewhere we wish to connect to
asio::ip::tcp::endpoint endpoint(asio::ip::make_address("93.184.216.34", ec),80);
return 0;
}
In the include path UI settings, I have the following paths listed which should take care of things:
${workspaceFolder}/**
/opt/homebrew/Cellar/asio/**
/opt/homebrew/Cellar/asio/1.24.0_1/include
/opt/homebrew/Cellar/boost/**
/opt/homebrew/Cellar/boost/1.81.0/include/boost
/opt/homebrew/Cellar/boost/1.81.0/include
/Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/usr/include
(there are redundant paths here but I am trying everything at this point)
Finally, it should be able to find the file since the file is there:
ls /opt/homebrew/Cellar/asio/1.24.0_1/include ls /opt/homebrew/Cellar/asio/1.24.0_1/include
Gives:
/opt/homebrew/Cellar/asio/1.24.0_1/include:
asio asio.hpp
I would expect it to just find those files. I have also installed boost using brew. It is available at the boost path included.
I am using Mac M1 with Monterey 12.5.1, and VSCode Version: 1.74.2.
Thanks for the help!
The includePath element in the json file is for intelliSense and not your compiler. If you aren't using CMake you will have to go into tasks.json, there you can specify additional compiler flags.
You want to add a flag: -Ipath/to/asio.

Files/directories to include in Visual Studio C++ to use CUDA?

I want to use CUDA/GPU in OpenCV in Visual Studio. For example, cuda::GpuMat. I successfully build OpenCV with the extra modules with CUDA enabled
I tried the following code
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/photo/cuda.hpp>
#include <opencv2/photo.hpp>
using namespace std;
using namespace cv;
int main(){
string imageName("input.bmp");
//CPU version
Mat image = imread(imageName.c_str(), IMREAD_GRAYSCALE);
//CUDA version
cuda::GpuMat imageGPU;
cuda::GpuMat downloadGPU;
Mat buff;
imageGPU.upload(image);
downloadGPU.download(buff);
imwrite("gpu.bmp", buff);
return 0;
}
But I get an unhandled exception error.
I originally downloaded OpenCV in C:\Users\me\Downloads\opencv
I then downloaded and installed the latest OpenCV extra modules with CUDA on in
In Property Pages->C/C++->General->Additional Include Directories, I have:
C:\Users\me\Downloads\opencv\build\include\opencv
C:\Users\me\Downloads\opencv\build\include\opencv2
C:\Users\me\Downloads\opencv\build\include\
In Property Pages->Linker->General->Additional Library Directories, I have:
C:\Users\me\Downloads\opencv\build\x64\vc15\lib
and in Property Pages->Linker->Input->Additional Dependencies, I have:
opencv_world343d.lib
opencv_world343.lib
what else am I supposed to include so I can get GpuMat to work properly?
Most of the cases, yes, but you need to know which library you need to add, it may be cufft.lib, cublas.lib, cudnn.lib, etc. It depends of the function you use inside your code.
Opencv includes a cmake include file that would set all of this up for you if you use cmake to build you VS test project. This file will be in the root of the opencv install directory, i.e. after building opencv running cmake --install or the equivalent in VS. The file is OpenCVConfig.cmake, and it would be included in the CMakeLists.txt file for your project. Then you would call FindPackage(OpenCV), which would locate the OpenCV install, setup a few variables, which you would then use to link against your app.
I can post a sample CMakeList.txt file if you feel that would help.

openCV libraries call local path that does not exist

i am making a simple c++ program with openCV library included. Eclipse IDE recognises openCV commands and library locations, but when i try to build the project, compiler gives external error, referring to opencv.hpp or core.hpp file calling a "opencv2/core.hpp" path, which does not exist in opencv folder. I figured out that the problem is linked to the way core.hpp is called, but the library files are read-only.
From what i saw in opencv.hpp file, this relative "opencv2/[module].hpp" reference is not only for the core, but all other modules as well. There is no opencv2 folder inside the one to where openCV is installed at all, in fact.
I've tried reinstalling and remaking openCV with different making arguments, using a different IDE and including direct search folders in eclipse. The problem, apparently, lies in the files themselves, or the way it maybe gets installed in the system the wrong way. The problem persists on both my main ubuntu machine and the ARMbian orange pi.
i get this error when trying to include any openCV library that contains
#include "opencv2/[opencv module].hpp" in it
as a result, compilation is terminated with the error message stating "/usr/local/include/opencv4/opencv2/opencv.hpp:52:28: fatal error: opencv2/core.hpp: No such file or directory"
edit 1: GCC c++ compiler options are -Iusr/local/include/opencv4/opencv2 -O3 -Wall -c -fmessage-length=0 and linker's options are -L/usr/local/lib.
The code is a simple displayImage
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread( argv[1], 1 );
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
edit 2: $ pkg-config --libs opencv does not see openCV as installed in the system, altho i've made sure to run make install and ldconfig on the path. This may be a signal of faulty installation, but this is just a sidenote, not entirely related to main problem. I have tried reinstalls and to different folders, but this also persists as well as a main problem
apparently, #sgarizvi 's comment was the answer. I just needed to set the include path to I/usr/local/include/opencv4 and it worked. After that, the error was fixed.
I am replying to my own question to close the case, as i cannot upvote/veryfy a comment
In your case, since your include path is /usr/local/include/opencv4/opencv2
Replace the first three lines
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
by
#include <opencv.hpp>
#include <imgproc.hpp>
#include <highgui.hpp>

Compile C++ with static lib

this will probably a dumb question for you guy's but I have no experience in C++ what so ever. I'm using an open source project osrm (which is awesome). Still to request a route, you have make an http request. To reduce the running time, I would like to build a wrapper around the code and call it using the command line. So I googled a bit and found that osrm already creates a static lib (.a file) when compiling the project. I also found a piece of code that points me in the right directions for building a wrapper. So to begin I build a simple hello world program (see below) that includes some files from that static lib. To compile I followed this tutorial.
My directory structure looks like this:
./helloWorld.cpp
./libs/libOSRM.a
And the command to compile is this:
gcc –static helloworld.cpp –L ./libs –l libOSRM.a
The code it selve:
#include "Router.h"
#include "boost/filesystem/path.hpp"
#include "ServerPaths.h"
#include "ProgramOptions.h"
#include <InternalDataFacade.h>
#include <viaroute.hpp>
#include <iostream.h>
main()
{
cout << "Hello World!";
return 0;
}
the exact error I got:
fatal error: ServerPaths.h: No such file or directory #include "ServerPaths.h"
Add the -IPathToTheHeaderFiles to the compiler options. So it will find the files to be included. Replace PathToTheHeaderFiles with the path where your file ServPaths.h resides.
Edit: Add as many -I as you need for further header files.
Additionally it would be worth to read a book about C++ or/and the GCC manual1
1 Section 3.11 will help.

Fatal error in starting up OpenCV 2.4.6 on VS 2008, file not found

I am using this tutorial to get started with OpenCV 2.4.6 on VS 2008:
http://docs.opencv.org/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.html
I followed all instructions (my OpenCV is not in the default Program Files (x86) folder, it's in
C:\opencv_built
Unlike in the tutorial, I put very simple code, just to make sure all included files are reachable and if it builds successfully etc:
#include "stdafx.h"
#include <iostream> // for standard I/O
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp> // OpenCV window I/O
using namespace std;
using namespace cv;
double getPSNR ( const Mat& I1, const Mat& I2);
Scalar getMSSIM( const Mat& I1, const Mat& I2);
int main(int argc, char *argv[])
{
return 0;
}
But I get a fatal error when I try to build:
fatal error C1083: Cannot open include file: 'opencv2/imgproc/imgproc.hpp': No such file or directory c:\Users\Administrator\Documents\Visual Studio 2008\Projects\firstopencv\firstopencv\firstopencv.cpp 17
This is obviously referring to this line:
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
I don't know where to find the dll files, or what to do next? I know this must be really easy but I've searched for any of the dll files, eg.
opencv_core243d.lib
but I get no search results.
1. Check your path to \vc10 folder. It should be either:
C:\opencv_built\build\x86\vc10
or
C:\opencv_built\x86\vc10
2. Go to
Start>Edit environment variables for your account>Under System variables > New...
Variable Name: OPENCV_DIR
Variable Value: Insert your path from step 1 here.
3. Open Visual Studio, make new project, go to Property Pages
4. Under C/C++ > Additional Include Directories
Insert $(OPENCV_DIR)\..\..\include
5. Under Linker > General > Additional Include Directories
Insert $(OPENCV_DIR)\lib
6a. (For DEBUG property!) Under Linker > Input > Additional Dependencies
Insert
opencv_core246d.lib
opencv_imgproc246d.lib
opencv_highgui246d.lib
opencv_ml246d.lib
opencv_video246d.lib
opencv_features2d246d.lib
opencv_calib3d246d.lib
opencv_objdetect246d.lib
opencv_contrib246d.lib
opencv_legacy246d.lib
opencv_flann246d.lib
6b. (For RELEASE property!) Under Linker > Input > Additional Dependencies
Insert
opencv_core246.lib
opencv_imgproc246.lib
opencv_highgui246.lib
opencv_ml246.lib
opencv_video246.lib
opencv_features2d246.lib
opencv_calib3d246.lib
opencv_objdetect246.lib
opencv_contrib246.lib
opencv_legacy246.lib
opencv_flann246.lib
This should be enough. If you get missing .dll window after running code, copy desired .dll from your C:\opencv_built\build\x86\vc10\bin or C:\opencv_built\x86\vc10\bin to your project folder.
It's not a dll file, it's a header file. The file is called imgproc.hpp
If you compiler can't find it it's either because it isn't there, or it's because you haven't told your compiler where to find it.
The important part for VS is the “Additional Include Directories”, look at that part again on the web page you followed.
Okay the fix was this:
Under Linker -> General -> Additional Library Dependencies
I put:
C:\opencv_built\lib\Debug