Install OpenCV on Jetson nano

Seems ok, don’t remove more for now. python2 and python3 both have cv2 linked to an opencv-4.5 build installed in /usr/local.

So your problem is probably just related to your opencv4.pc pkg-config file.
What gives

# This is the expected file
cat /usr/local/lib/pkgconfig/opencv4.pc

# This one if there may interfere
cat /usr/lib/pkgconfig/opencv4.pc

# This is what was generated by your build
cat /home/xxx/lib/pkgconfig/opencv4.pc

Hello @Honey_Patouceul, I have the opencv4.pc file in /usr/local/lib/pkgconfig/ location. opencv4.pc contents are following:

prefix=/usr
exec_prefix={prefix} includedir={prefix}/local/include
libdir=${exec_prefix}/lib

Name: opencv
Description: The opencv library
Version: 2.x.x
Cflags: -I${includedir}/opencv4
#Cflags: -I${includedir}/opencv -I${includedir}/opencv2
Libs: -L${libdir} -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy

Since opencv4.pc was not on my pkg-config search path, I added that file later from forum support. Could you see any problem in it? Can In line# 8, Version: 2.x.x be a problem?

Seems an incomlete mix…
Is the file generated during your build /home/xxx/lib/pkgconfig/opencv4.pcmore complete ?
If yes, you would just replace with this one.

For reference, here is my opencv4.pc (for a 4.5.1 build, libraries may differ from your build):

# Package Information for pkg-config

prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.5.1
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_dnn -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
Libs.private: -lm -lpthread -L/usr/lib/aarch64-linux-gnu -lGL -lGLU -lcudart_static -ldl -lrt -lnppc -lnppial -lnppicc -lnppicom -lnppidei -lnppif -lnppig -lnppim -lnppist -lnppisu -lnppitc -lnpps -lcublas -lcudnn -lcufft -L-L/usr/local/cuda -llib64 -L-L/usr/lib -laarch64-linux-gnu
Cflags: -I${includedir}

Hello @Honey_Patouceul, after my cv2 installation I was missing opencv4.pc in /usr/local/lib/pkgconfig/ location. So I generated opencv4.pc file in the following location. Could you please give me an idea, if it’s for the Libs flags I’m getting the errors?

Still getting the same error when compiling a .cpp file.

Seems it is looking for opencv package, while it should be looking for opencv4. Adapt your pkg-config command to opencv4:

pkg-config opencv4 --libs --cflags

Adding opencv4, gives the following error:

gcc -std=c++11 `pkg-config --cflags opencv4` `pkg-config --libs opencv4` gstreamer_view.cpp -o gstreamer_view -lstdc++ -lopencv_core -lopencv_highgui -lopencv_videoio
/usr/bin/ld: cannot find -lopencv_contrib
/usr/bin/ld: cannot find -lopencv_legacy
collect2: error: ld returned 1 exit status

What gives:

pkg-config opencv4 --libs --cflags

pkg-config opencv4 --libs --cflags

gives following output:

-I/usr/local/include/opencv4 -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy

Do you have a opencv4.pc file in /usr/lib/pkgconfig ? If yes it may be the culprit. It seems also truncated. Are you editing these ?

Also post what gives;

echo $PKG_CONFIG_PATH

Probably you would try to rebuild and reinstall opencv with this script:

Hello @Honey_Patouceul, I didn’t understand what you mean by truncated? I don’t have opencv4.pc file in /usr/lib/pkgconfig location. And when I echo $PKG_CONFIG_PATH gets the following output:

/usr/local/lib/pkgconfig

Again, thanks a lot for your help.

Good so far. What gives:

cat /usr/local/lib/pkgconfig/opencv4.pc

prefix=/usr
exec_prefix={prefix} includedir={prefix}/local/include
libdir=${exec_prefix}/lib

Name: opencv
Description: The opencv library
Version: 2.x.x
Cflags: -I${includedir}/opencv4
#Cflags: -I${includedir}/opencv -I${includedir}/opencv2
Libs: -L${libdir} -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy

This is weird. Did you edit this file yourself ? If yes, check again if there was a file /home/xxx/lib/pkgconfig/opencv4.pc generated by your opencv build. If yes, check if it looks better.
Where is your opencv library supposed to be installed ?

Again, rebuilding a new version with the script from the link above may take about one hour, but would save you many problems later.

I did edit this myself, since my build didn’t generate any opencv4.pc file. My opencv is installed in /usr/local. I could try to rebuild myself from scratch but it won’t undo my shortcomings. I just wanted to learn why my build would not work, since it’s already imported cv2 on python shell.

Maybe it’s correctly installed in python, but not correctly installed in pkgconfig.
You may edit your opencv4.pc file, but be sure about what you do.
Rebuilding opencv with the right option -D OPENCV_GENERATE_PKGCONFIG=ON would generate the correct pkgconfig file, and installing opencv would install the pkgconfig file for opencv4.

After all, if you just want to compile your opencv application, you can do without pkgconfig (since it is broken for opencv on your system). Assuming opencv is installed in /usr/local:

g++ -std=c++11 -o gstreamer_view -I/usr/local/include/opencv4 gstreamer_view.cpp -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_videoio -lstdc++

Does this work ?

g++: error: lopencv_highgui: No such file or directory

This is the error, I’m getting now. I think, I’m finally giving up. But Thanks a lot for your patient answers.

There was a typo in previous command. Edited previous post for fixing that.

when you configure cmake, use this flag,
-D OPENCV_PC_FILE_NAME=opencv.pc
then you can access OpenCV with opencv instead of opencv4 at pkg-config.