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

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
Blueprint

Learn to build Computer Vision Software easily and efficiently.

This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.

Sign UP for FREE