Basic Operations on Image

OpenCVOpenCVBeginner
Practice Now

Introduction

Welcome to this tutorial on OpenCV-Python! This tutorial will cover some basic operations on images using the OpenCV library in Python.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL opencv(("`OpenCV`")) -.-> opencv/BasicOperationsGroup(["`Basic Operations`"]) opencv/BasicOperationsGroup -.-> opencv/imread("`Image Read`") opencv/BasicOperationsGroup -.-> opencv/size("`Size Property`") opencv/BasicOperationsGroup -.-> opencv/dtype("`Dtype Property`") opencv/BasicOperationsGroup -.-> opencv/split("`Image Split`") opencv/BasicOperationsGroup -.-> opencv/shape("`Shape Property`") opencv/BasicOperationsGroup -.-> opencv/merge("`Image Merge`") opencv/BasicOperationsGroup -.-> opencv/imwrite("`Image Write`") subgraph Lab Skills opencv/imread -.-> lab-67174{{"`Basic Operations on Image`"}} opencv/size -.-> lab-67174{{"`Basic Operations on Image`"}} opencv/dtype -.-> lab-67174{{"`Basic Operations on Image`"}} opencv/split -.-> lab-67174{{"`Basic Operations on Image`"}} opencv/shape -.-> lab-67174{{"`Basic Operations on Image`"}} opencv/merge -.-> lab-67174{{"`Basic Operations on Image`"}} opencv/imwrite -.-> lab-67174{{"`Basic Operations on Image`"}} end

Accessing and Modifying Pixel Values

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:

example_image_origin

Image after modifying pixel values at the range of [0:9,0:9]:

example_image_origin

Users can check the correctness of the result by running the code below.

Accessing Image Properties

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

Setting a Region of Interest (ROI)

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:

example_image_ROI

Splitting and Merging Images

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:

example_image_Swapped_Channels_Image

Summary

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.

Other OpenCV Tutorials you may like