MATLAB Answers

GPU Coder Error with OpenCV

12 views (last 30 days)
I'm trying to use the example "Semantic Segmentation on NVIDIA DRIVE" just modifying it to be executing on a Jetson Nano board. I train my own network, then Ijust load my network instead of the one defined on the example, nevertheless when I try to deploy it to the Nano I have the following error:
/ fatal error: opencv2/opencv.hpp: No such file or directory
#include "opencv2/opencv.hpp"
I check on the nano to see if the installation of OpenCV is correct:
alvaro@alvaro-nano:~$ pkg-config --modversion opencv
The installation is correct, also when I call the code generation I pass the path as an -I command:
codegen -config cfg autoSemanticSegmentation -args {img} -I /usr/include/opencv4 -report -v
And I saw that this is inluded on the generated make file.
How can I fix this issue to be able to compile and execute the example on the Nano?


Show 1 older comment
Alvaro Izaguirre Serrano
Alvaro Izaguirre Serrano on 6 Mar 2020
Hello Darshan,
Thanks for your answer, I can locate the opencv.hpp file on the target on the path "/usr/include/opencv4/opencv2" which is the standard location. I already thry this, I try the following confguration for the GPU Coder:
% Create the configuration for the code generation
cfg = coder.gpuConfig('exe');
cfg.Hardware = coder.hardware('NVIDIA Jetson');
cfg.Hardware.BuildDir = '~/remoteBuildDir';
cfg.CustomInclude = fullfile('/usr/include/opencv4');
cfg.CustomSource = fullfile('segnet_deploy_Jetson', '');
When I generate the code with GPU coder the path is included on the file. I also try to call the code generation with the option:
-I '/usr/include/opencv4'
Also the make file incudes the defined, but I have the same error at compilation, for example on the make file I can see the following:
As you can see the path '/usr/include/opencv4' and '/usr/include/opencv4/opencv2' are included to call the compiler.
Even on the target if I check the version of opencv with " pkg-config --modversion opencv" I have the answer of the version, and if I try to use it in Phyton with import cv2 I can.
alvaro@alvaro-nano:~$ pkg-config --modversion opencv
Then I don't know what else to do.
Walter Roberson
Walter Roberson on 11 Mar 2020
alvaro@alvaro-nano:~$ pkg-config --modversion opencv
opencv pkg is not the same as openvc2 pkg. opencv still exists as a package
Alvaro Izaguirre Serrano
Alvaro Izaguirre Serrano on 11 Mar 2020
yes, actually I work with opencv4 and now I'm able to compile

Sign in to comment.

Accepted Answer

Hariprasad Ravishankar
Hariprasad Ravishankar on 9 Mar 2020
Hi Alvaro,
I believe the issue is due to the line below,
cfg.CustomInclude = fullfile('/usr/include/opencv4');
For hardware deployment workflow, cfg.CustomInclude prepends the host working directory structure to create the following structure
Rather than,
To resolve the issue you can try one of the two approaches
1) Rename /usr/include/opencv4/opencv2 to /usr/include/opencv2
mv /usr/include/opencv4/opencv2 /usr/include/opencv2
This should ensure that the compiler is able to find opencv2/opencv.hpp under /usr/include/
2) Use coder.updateBuildInfo('addIncludePaths', '/usr/include/opencv4/');
function out = segnet_predict(in)
% A persistent object mynet is used to load the DAG network object.
% At the first call to this function, the persistent object is constructed and
% setup. When the function is called subsequent times, the same object is reused
% to call predict on inputs, thus avoiding reconstructing and reloading the
% network object.
% Update buildinfo with the OpenCV library flags.
opencv_link_flags = '`pkg-config --cflags --libs opencv`';
coder.updateBuildInfo('addIncludePaths', '/usr/include/opencv4/');
persistent mynet;
if isempty(mynet)
mynet = coder.loadDeepLearningNetwork('SegNet.mat');
% pass in input
out = predict(mynet,in);


Show 9 older comments
Hariprasad Ravishankar
Hariprasad Ravishankar on 11 Mar 2020
Hi Alvaro,
Glad to know you got the example working. As for the performance, it may be limited by the network architecture and hardware used.
You can try training a new semantic segmentation network based on DeepLabV3+ and using a base network like resnet18 or mobilenetv2.
Side note: There are ways to overclock the CPU and GPU on Jetson boards using the script, which can help improve performance, but it would be good to proceed with caution when taking this route.
David Brisson Andersen
David Brisson Andersen on 16 Mar 2020
Hi you two,
I have refalshed my jetson to jetpack 4.2.3 which includes the old opencv 3.something.something and now everything works.
Hariprasad Ravishankar
Hariprasad Ravishankar on 16 Mar 2020
Hi David,
Thats good to know. We will look into updating the example to work with opencv4 in a future release.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!