Introduction
Welcome to this tutorial on OpenCV-Python! This tutorial will cover some basic operations on images using the OpenCV library in Python.
Welcome to this tutorial on OpenCV-Python! This tutorial will cover some basic operations on images using the OpenCV library in Python.
Before starting this lab, challengers need to open the Python Shell in terminal in /home/labex/project
cd /home/labex/project
python3
First, let's start by loading an image and displaying it using OpenCV:
import cv2
## Load an image
image = cv2.imread('image.jpg')
## Display the image
cv2.imwrite('Original Image.jpg', image)
To access a pixel value, we simply need to provide its coordinates (x
, y
) as follows:
pixel_value = image[y, x]
The pixel value is an array containing the color channels (B
, G
, R
). To modify a pixel value, just assign a new value to the corresponding coordinates.
Here is an example to set the pixel [0:9, 0:9] to red:
for x in range(10):
for y in range(10):
image[x, y] = [0, 0, 255] ## Set the pixel to red
cv2.imwrite('Modified Image.jpg', image)
The original image looks like:
Image after modifying pixel values at the range of [0:9,0:9]:
Users can check the correctness of the result by running the code below.
Image properties include the number of rows, columns, and channels, as well as the image data type and the total number of pixels.
To access the shape of the image (rows
, columns
, and channels
), use the shape
attribute:
height, width, channels = image.shape
print(height, width, channels)
## output
## 360 640 3
To access the total number of pixels, use the size
attribute:
total_pixels = image.size
print(total_pixels)
## output
## 691200
To access the image data type, use the dtype
attribute:
image_dtype = image.dtype
print(image_dtype)
## output
## uint8
A Region of Interest (ROI) is a sub-image that we can extract from the original image. To set an ROI, we just need to provide the starting and ending coordinates.
roi = image[start_y:end_y, start_x:end_x]
For example, to extract an ROI of 100x100 pixels from the top-left corner, we can do the following:
roi = image[0:100, 0:100]
cv2.imwrite('ROI.jpg', roi)
The generated image ROI.jpg
looks like:
To split an image into its color channels (B
, G
, R
), use the cv2.split()
function:
b, g, r = cv2.split(image)
We can now display the individual channels using cv2.imwrite()
.
To merge the channels back into a single image, use the cv2.merge()
function:
merged_image = cv2.merge((b, g, r))
For example, to swap the red and blue channels, we can do the following:
swapped_image = cv2.merge((r, g, b))
cv2.imwrite('Swapped Channels Image.jpg', swapped_image)
The generated image Swapped Channels Image.jpg
looks like:
And that's it! You've learned some basic operations on images using OpenCV-Python. These are important concepts to understand when working with images in computer vision and image processing applications. By mastering these basic operations, you will be well on your way to developing more advanced image processing algorithms.