Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCV Basic Image Operations

OpenCV Basic Image Operations

This article will introduce in detail four basic image operations: accessing and modifying pixel values, image ROI (Region of Interest) operations, separating and merging image channels, and image scaling, rotation, translation, and flipping.

Common methods:

Operation Function/Method Description
Accessing pixel values image[y, x] Get or modify pixel values.
Image ROI image[y1:y2, x1:x2] Get or modify a rectangular region in an image.
Channel splitting and merging cv2.split() / cv2.merge() Separate or merge image channels.
Image scaling cv2.resize() Resizes the image.
Image rotation cv2.getRotationMatrix2D() Rotate an image.
Image translation cv2.warpAffine() Translate an image.
Image flipping cv2.flip() Flip an image.
Image Addition cv2.add() Perform addition on two images.
Image Subtraction cv2.subtract() Perform subtraction on two images.
Image Blending cv2.addWeighted() Perform weighted blending on two images.
Thresholding cv2.threshold() Perform thresholding on an image.
Smoothing cv2.blur() / cv2.GaussianBlur() Perform smoothing on an image.

1. Accessing and modifying pixel values

An image is a matrix composed of pixels, each of which has one or more values representing color or grayscale. In a grayscale image, each pixel has only one value representing grayscale intensity; in a color image, each pixel typically has three values representing the intensities of the red, green, and blue (RGB) channels.

Accessing pixel values

In Python, you can use the OpenCV library to access image pixel values. Suppose we have a grayscale image img, you can use img[y, x] to access the pixel value at (x, y) position. For a color image, you can use img[y, x, c] to access the pixel value of a specific channel c , where c is 0 (blue), 1 (green), or 2 (red).

Example

import

cv2

Read an image

img

=

cv2.

imread

(

'image.jpg'

)

Accessing pixel values

pixel_value

=

img

[

100

,

150

]

 

Access the pixel value at position (150, 100)

print

(

pixel_value

)

Modify pixel values

Modifying pixel values is just as simple: just assign a new value to the corresponding pixel position.

Example

Modify pixel values

img

[

100

,

150

]

=

[

255

,

255

,

255

]

 

Set the pixel value at position (150, 100) to white

2. Image ROI (Region of Interest) operations

ROI refers to the region of interest in an image. By extracting an ROI, we can process only a specific part of the image, thereby improving processing efficiency.

Extract ROI

In OpenCV, you can extract an ROI through slicing operations. Suppose we want to extract a 100x100 pixel region in the upper-left corner of an image:

Example

Extract ROI

roi

=

img

[

0

:

100

,

0

:

100

]

Modify ROI

After extracting an ROI, you can modify it and then put the modified ROI back into the original image.

Example

Modify ROI

roi

[

:

,

:

]

=

[

0

,

255

,

0

]

 

Set the ROI region to green

Put the modified ROI back into the original image

img

[

0

:

100

,

0

:

100

]

=

roi

3. Separating and merging image channels

Color images usually consist of multiple channels, such as the three channels of an RGB image. Sometimes we need to separate these channels for individual processing and then merge them back into the original image.

Channel splitting

In OpenCV, you can use the cv2.split() function splits the channels of an image.

Example

Channel splitting

b

,

g

,

r

=

cv2.

split

(

img

)

Channel merging

The separated channels can be merged back into the original image using the cv2.merge() function.

Example

Channel merging

merged_img

=

cv2.

merge

(

[

b

,

g

,

r

]

)

4. Image scaling, rotation, translation, and flipping

Geometric transformations of images are common operations in image processing, including scaling, rotation, translation, and flipping.

Image scaling

Image scaling can be achieved through the cv2.resize() function. You can specify the scaled image size or scaling ratio.

Example

Image scaling

resized_img

=

cv2.

resize

(

img

,

(

200

,

200

)

)

 

Scale the image to 200x200 pixels

Image rotation

Image rotation can be achieved through the cv2.getRotationMatrix2D() and cv2.warpAffine() function. You need to specify the rotation center and rotation angle.

Example

Image rotation

(

h

,

w

)

=

img.

shape

[

:

2

]

center

=

(

w //

2

,

h //

2

)

M

=

cv2.

getRotationMatrix2D

(

center

,

45

,

1.0

)

 

Rotate 45 degrees

rotated_img

=

cv2.

warpAffine

(

img

,

M

,

(

w

,

h

)

)

Image translation

Image translation can be achieved through the cv2.warpAffine() function. You need to specify the translation matrix.

Example

Image translation

M

=

np.

float32

(

[

[

1

,

0

,

100

]

,

[

0

,

1

,

50

]

]

)

 

Translate 100 pixels to the right and 50 pixels down

translated_img

=

cv2.

warpAffine

(

img

,

M

,

(

w

,

h

)

)

Image flipping

Image flipping can be achieved through the cv2.flip() function. You can specify the flip direction (horizontal, vertical, or both).

Example

Image flipping

flipped_img

=

cv2.

flip

(

img

,

1

)

 

Horizontal flip