Install OpenCV on Jetson nano

I installed opencv 4.5.2 in my Jetson Nano.

import cv2
print(cv2.version)
4.5.2

It imports the cv2 package when I write the command in python shell. But to check my installation I’m following the JetsonHacksNano repository: buildOpenCV/Examples at master · JetsonHacksNano/buildOpenCV · GitHub. I ran the gstreamer_view.cpp to check the installation.

To compile gstreamer_view.cpp:

$ gcc -std=c++11 pkg-config --cflags opencv pkg-config --libs opencv gstreamer_view.cpp -o gstreamer_view -lstdc++ -lopencv_core -lopencv_highgui -lopencv_videoio

I get the following error:

/usr/bin/ld: cannot find -lopencv_contrib
/usr/bin/ld: cannot find -lopencv_legacy
/usr/bin/ld: cannot find -lgstreamer_view.cpp
collect2: error: ld returned 1 exit status

Can anyone give me an idea what might be the reason for that?

Hi raihan29s,

Sorry for the late response, have you managed to get OpenCV installed successfully?

Hello @kayccc, I couldn’t resolve my problem yet. Please let me know if you are aware of any troubleshooting methods.

$ pkg-config --modversion opencv

If nothing is displayed, pls add this flag when you run cmake:
-DOPENCV_GENERATE_PKGCONFIG=ON

And when you build C++ example, try as follow:
$ g++ -o example /usr/local/share/opencv4/samples/cpp/example.cpp $(pkg-config opencv4 --libs --cflags)

Hello @MtHiker , pkg-config --modversion opencv shows 2.x.x output. And I was building C++ example I got the following error:

g++ -o gstreamer gstreamer_view.cpp $(pkg-config opencv --libs --cflags)
g++: error: missing argument to ‘-l’

Since, I’m not passing any -l argument, What might be the possible reason for it? Thanks in advance.

As you have opencv4, you would try to adjust pkg-config command as in the command provided by @MtHiker :

pkg-config --modversion opencv4
g++ -o example /usr/local/share/opencv4/samples/cpp/example.cpp $(pkg-config opencv4 --libs --cflags)

Hello @Honey_Patouceul, when I ran with opencv4, still getting the following error:

g++ -o gstreamer gstreamer_view.cpp $(pkg-config opencv4 --libs --cflags)
g++: error: missing argument to ‘-l’

What gives:

pkg-config opencv4 --libs --cflags

-I/usr/include/opencv -I/usr/include/opencv2 -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l

This is weird. Seems the list of libs is truncated. You may try to reinstall opencv4.5. Be sure you’ve configured the build with -D OPENCV_GENERATE_PKGCONFIG=ON.

1 Like

@raihan29s ,

I doubt you might have two different OpenCVs on your Jetson.
Would you scan how many Opencv version are installed ?

Hello @MtHiker, import cv2 print cv2.__version__ in python shell only returns 4.5.2

$ dpkg -l | grep opencv

I guess it shows 2.x.x version opencv not 4.5.2.
Remove the old version before you build/install new one.

Find it is there: /usr/local/lib/pkgconfig/opencv.4pc
and try to export PKG_CONFIG_PATH

@MtHiker I’m facing one strange issue. When I run sudo apt-get purge libopencv* to remove previously installed opencv files. It shows nothing is upgraded/no changes made. I ran dpkg -l | grep opencv to check opencv versions on my nano, it returns nothing. But when I run import cv2 on my python shell, it does import cv2.

What might be reason for that?

If you’ve manually installed Opencv-4.5 (sudo make install after building opencv), then apt has no knowledge of your version installed in python, so you would have to remove yourself.
Opencv in python is installed as a library, and you can check this lib dependencies with ldd

# Python2.7
ldd /usr/lib/python2.7/dist-packages/cv2/python-2.7/cv2.so

# Python3.6
ldd /usr/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so

These should be overwritten next time you install opencv if having built with python support.

In case you want to be sure about any opencv library on your system, you can try:

# Install locate tool that builds a database of your filesystems
sudo apt update
sudo apt install locate

# Build initial DB excluding tmpfs filesystems. This may take a few minutes on first time
updatedb --prunefs='tmpfs'

# Now find any file libopencv_* on your system and provide a list of unique directories:
locate libopencv_ | awk 'BEGIN{FS=OFS="/"}{NF--; print}' | sort -u

Hello @Honey_Patouceul, My /usr/lib/python*/dist-packages/ folder doesn’t have any cv2 folder. But when I ran the

locate libopencv_ | awk ‘BEGIN{FS=OFS="/"}{NF–; print}’ | sort -u

command, I saw my libopencv* files are under two folders /usr/local/lib and /home/xxx/lib. If I remove all libopencv* files from this two folder, will my previous opencv files be deleted?

Moreover, does having older opencv files in my system effect my new installation? or it overwrites the previous files on its way to the installation?

*stands for python version 2.7/3.7

Sorry I’m a bit surprized. What gives:

locate cv2.so | awk 'BEGIN{FS=OFS="/"}{NF--; print}' | sort -u
locate cv2.cpython-36m-aarch64-linux-gnu.so | awk 'BEGIN{FS=OFS="/"}{NF--; print}' | sort -u

If you don’t have any and you can import cv2 in python, then there is another way I’m not aware of.

Hello @Honey_Patouceul, Running the command you asked to run gives the following result,

Command:
locate cv2.so | awk ‘BEGIN{FS=OFS="/"}{NF–; print}’ | sort -u

Result:
/home/xxx/lib
/usr/local/lib/python2.7/dist-packages/cv2/python-2.7

Command:
locate cv2.cpython-36m-aarch64-linux-gnu.so | awk ‘BEGIN{FS=OFS="/"}{NF–; print}’ | sort -u

Result:
/home/xxx/lib/python3
/usr/local/lib/python3.6/site-packages/cv2/python-3.6

Ok, got it, you have python installed in /usr/local.
So now you can use ldd on each to see what opencv version is linked to your python2 or python3 :

# Python2.7
ldd  /usr/local/lib/python2.7/dist-packages/cv2/python-2.7

# Python3.6
ldd /usr/local/lib/python3.6/site-packages/cv2/python-3.6

Hello @Honey_Patouceul, I attached my output from both ldd input. Both them got libopencv* extensions. Could you please tell how can I distinguish them?

python2.7.txt (18.4 KB) python3.6.txt (18.4 KB)