Skip to content

Beginners Opencv · Tutorials

Loading images – OpenCV 3.4 with python 3 Tutorial 1

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

Show images

Load and show images with Opencv is a really simple operation.
On Line 1 we import the opencv library.

On Line 3 we load the image into a variable. You can put just the title of the image and the format (example .jpg) if the image is in the same folder as the python file, otherwise you need to insert the full path, if the image is on another folder.
For example if you have it on the desktop, it would look something like this cv2.imread(“C:\Users\myusername\Desktop\red_panda.jpg”)

On Line 3 we convert it into a black and white image, or better to say gray scale image as there will be different gradients of black and of white.

Finally on Line 6 and Line 7 we show both images, the one with original color and the one in gray scale format.

On Line 8 we have the function that keeps the images open. cv2.waitKey(0) is literally waiting that you press a key. If you press for example “ESC”, it will close all the windows.

On Line 9 it destroy all the windows. It won’t make any difference in this simple code, as the windows will be destroied anyway because the code will finish its execution, but for example if you have a bigger program and later there are some other lines of code running, then the windows might still be open.
So it’s always a good practice to put that line at the end.

 

Save images

Save images is also a really simple operation.

After we load the image, on Line 6 we are going to save it.
You need to insert two parameters. The first one is the title, followed by the format of the image “gray_panda.jpg” if you want to save it in jpg format or “gray_panda.png” if you want png format.
Second parameter is the image that you want to save, in this case we’re saving the gray image.

6 comments

  1. Hi, thanks for your tutorial, but i have a cuestión when I try this example:
    Feature detection (SIFT, SURF, OBR) – OpenCV 3.4 with python 3 Tutorial 25
    I have a wrong:
    surf = cv2.xfeatures2d.SURF_create()
    AttributeError: ‘module’ object has no attribute ‘xfeatures2d’

    How could I fix it? thanks for your time

  2. How do we load a datset of images and then pre process it and then apply ORB on the whole data set insted of one image?
    Could you please share the tutorial for it.

Join the discussion