Rate This Document
Findability
Accuracy
Completeness
Readability

merge

Usage

Merges multiple single-channel images into one multi-channel image. It receives an array of single-channel images and merges them into an image in a given sequence. It is often used to merge RGB or RGBA channels into a color image.

Interface

1
cv2.merge(mv)

Parameters

Parameter

Description

Value Range

Input/Output

mv

An iterable object (such as a tuple or list) that contains multiple single-channel images. Each element should be a single-channel image of the same type, and their shapes (width and height) must be the same.

Multiple single-channel images of the uint8 type. Each image has 0 to 255 pixels.

Input

Return Values

  • Success: KP_CV_SUCCESS
  • Failure: an error code

Error Codes

Error Code

Description

INVALID_PARAM_MSG

The input is null.

NOT_SUPPORT_MSG

The number of input image channels is not 3, or the image is not of the uint8 type.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import numpy as np
import cv2

# Create three channels.
img_bgr = [np.zeros((5, 5), dtype=np.uint8) for _ in range(3)]  

# Assign values to each channel.
img_bgr[0] = np.array([[255, 0, 0, 0, 255],
                      [0, 255, 255, 255, 0],
                      [0, 255, 0, 255, 0],
                      [0, 255, 255, 255, 0],
                      [255, 0, 0, 0, 255]], dtype=np.uint8)  

img_bgr[1] = np.array([[0, 255, 0, 255, 0],
                      [255, 0, 0, 0, 255],
                      [0, 0, 255, 255, 0],
                      [255, 0, 0, 0, 255],
                      [0, 255, 0, 255, 0]], dtype=np.uint8)  

img_bgr[2] = np.array([[0, 0, 255, 255, 0],
                      [255, 255, 0, 0, 255],
                      [255, 0, 0, 0, 255],
                      [255, 255, 0, 0, 255],
                      [0, 0, 255, 255, 0]], dtype=np.uint8)


# Use cv2.merge to merge the three channels into one image.
merged_img = cv2.merge(img_bgr)  
# Display the merged image.
print(merged_img)

Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[[[255   0   0]
  [  0 255   0]
  [  0   0 255]
  [  0 255 255]
  [255   0   0]]

 [[  0 255 255]
  [255   0 255]
  [255   0   0]
  [255   0   0]
  [  0 255 255]]

 [[  0   0 255]
  [255   0   0]
  [  0 255   0]
  [255 255   0]
  [  0   0 255]]

 [[  0 255 255]
  [255   0 255]
  [255   0   0]
  [255   0   0]
  [  0 255 255]]

 [[255   0   0]
  [  0 255   0]
  [  0   0 255]
  [  0 255 255]
  [255   0   0]]]