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:
- How the computer sees the image.
- How to select a specific area of the image (ROI)
- 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()
This site uses Akismet to reduce spam. Learn how your comment data is processed.
I’ve just launched the shop.
If you’re a beginner and you need a device to start learning computer vision, the Jetson Nano COMPUTER VISION PACK might be a good solution for you.
You can go on the shop to buy it!
Opencv Beginner Tutorial
- 2) Loading Video and Webcam
- 3) Drawing and writing on images
- 4) Basic operations on images
- 5) Add images and Threshold
- 6) Blending images
- 7) Bitwise Operators
- 8) Trackbars
- 9) Object detection using HSV Color space
- 10) Basic Thresholding
- 11) Histograms
- 12) Basic geometric transformations
- 13) Perspective transformation
- 14) Affine transformation
- 15) Adaptive thresholding5
- 16) Smoothing images
- 17) Morphological transformation
- 18) Edge detection
- 19) Find and Draw Contours
- 20) Template matching
- 21) Lines detection with Hough Transform
- 22) Corners detection
- 23) Image Pyramids
- 24) Image Pyramids (Blending and reconstruction)
- 25) Feature detection (SIFT, SURF, ORB)
- 26) Feature Matching (Brute-Force)
- 27) Mouse Events
- 28) Histogram and Back Projection
- 29) Object tracking with Mean-shift
- 30) Object tracking with Camshift
- 31) Optical Flow with Lucas-Kanade method
- 32) Background Subtraction
- 33) k-Nearest Neighbour classification
- 34) Object tracking using Homography
- 35) Fourier Transform
- 36) Knn handwritten digits recognition
- 37) Face detection using Haar Cascades