Reading image using OpenCV in NVIDIA Jetson Nano

To start with, we’ll take a very easy project, that is, to read any image file and display it. So, we’ll be using OpenCV, Python, Numpy, everything in NVIDIA Jetson Nano. So, I have created one folder on my desktop on which I’ll saving all my work. Let’s do the coding part first. First, i’ll get inside the folder where I want my work to be stored.

Now type gedit. This will open the editor to code. You can also use nano editor. But gedit is basic and more flexible to do editing then nano editor. Of course you can use other Linux supported editors as well.

Now it’s time to write the code in your editor.

import cv2
import numpy as np

im = cv2.imread(‘lenna.jpg’)

cv2.imshow(‘im’, im)
cv2.waitKey(0)
cv2.destroyAllWindows()

The code is very easy. It’s traditional OpenCV code. But since this is our first code we’ll dissect it:

  1. We are importing numpy and opencv libraries first. In this specific code, numpy is not doing anything so we can ignore it. But I am habitual to call it so, no problem.
  2. Then using imread (image read) we are reading the image file. Here, the file name is Lenna and it’s extension is .jpg. Now do remember that the file is stored in the same folder which is ocv in my case. So, I don’t need to specify the complete path. In the later posts, we’ll see how to give complete path also.
  3. Using imshow (image show), the image will be displayed. Pass in the variable name which is used to read the image file.
  4. After displaying the image the question is till how much time you want to display it. For this, waitkey is used. As the name suggest, it will wait for a specific time in milliseconds until you press any key on the keyboard. If you want that the image should be displayed for indefinite time period, then write waitkey (0).
  5. Finally, we will close all windows using destroyAllWindows ().  If you have multiple windows open and you do not need those to be open, you can use cv2.destroyAllWindows() to close those all.

Waitkey can also destroy the windows but in many cases, it cannot. Especially in python scripts running from terminals. So, it’s good practice to use destroyAllWindows ()

After writing it, save the file with any non-python keyword. Save it using .py extension. I am saving it with the name f1.py. Time to execute the code now. On the terminal, write python fi.py and press enter

And the image will be displayed as shown above. As you can see, there is no provision in the code to terminate the program other than the keyboard interrupt but you can add few lines also to terminate the program by pressing any specific key. We’ll see this also in the coming posts. i am trying to go step by step without a rush and with minimum python knowledge.

That’s it. This was our first project using OpenCV. Now you can do this on any OS and on any python editor. But our purpose here is to see what NVIDIA Jetson Nano is capable of. That’s why we are using this.

In the next article we’ll explore more of OpenCV and Python in NVIDIA Jetson Nano 2 GB Kit.

Design a site like this with WordPress.com
Get started