Familiarize yourself with OpenCV: Read, display and save photos

Tram Ho

Hello everyone, today I will introduce you to the Opencv2 library, a very powerful library used in image processing, this is also the library I use mainly in the next article. And here I will code in python 3 on jupyter notebook school.


I encourage you to use anaconda there will jupyter notebook settup way you can refer to the following link here .

Install opencv2 library:

Check the version:

Or if you don’t want to waste time you can use colab

Read and display image

The picture I will use will be the following:

The output of img will now be a three-dimensional matrix but in the theory of RGB. We will have a command to view the shape and size of the image:

When displaying images, there are 2 ways:

  • Method 1 uses the function available in cv2:
  • Option 2 uses the matplotlib library.

    If the code as above, the output image will be as follows:

    Because cv2 defaults to reading color images in the order of BGR (blue, green and red). Therefore, if you want to display correctly, you must write:

Accessing image data with numpy.array

The operations we are familiar with using numpy are also used in image data. For example:

Crop out part of the image and display them:


Change the color of the area we have cropped to green:


Resizing images

We want to resize the image into an image of size 200, 200 will do the following:

Write images

Save the image file using the following command:


All the code you can see here: here

Above I introduced you on how to read, display and save an image file

Share the news now

Source : Viblo