Skip to content

Beginners Opencv · Tutorials

Basic operations on images – OpenCV 3.4 with python 3 Tutorial 4

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

https://youtu.be/41YdeEFoE64
We’re going to see in this tutorial a few basic operations with the images using Opencv with Python.
Watching the video we will understand:

  1. How the computer sees the image.
  2. How to select a specific area of the image (ROI)
  3. How to print or change the value of a pixel

We start by importing the libraries.

import cv2
import numpy as np

We load the image and we get its shape. Getting the shape means tha we get the rows (the pixels in height of the image), the columns or cols (the pixels in width of the image) and the numbers channels.
If the channel is one it means it’s a grayscale image, if the channels are 3 (blue, green and red) it means it’s a colored image.

image = cv2.imread("red_panda.jpg")
rows, cols, ch = image.shape

Roi stands for Region of Interest. In this next line of code we select a specific region of the image. The first two numbers “[100: 200,” defines respectively the starting end ending row. The last two instead define start and ending of the colomn.

roi = image[100: 280, 150: 320]

 We can print or edit a specific pixel. When we print a pixel we get it’s value. To change the value of a pixel we need to specific the new value.

# Print a specific pixel of the image
# We're printing the value in the position 175, 300 
print(image[175, 300])

# Change the value of a pixe
# We are going to assign a new value
image[250, 180] = (255, 0, 0)

We finally show everything

cv2.imshow("Panda", image)
cv2.imshow("Roi", roi)
cv2.waitKey(0)
cv2.destroyAllWindows()

Files:

  1. red_panda.jpg
  2. flag.png

One comment

  1. I need a code that can be used to detect pestilence and illness of the fruit of an olive tree like an olive fly. This is a very immediate and sensitive issue. (Image processing and artificial intelligence)
    Thanks

Join the discussion