1# channel split cv2.split

Import libraries

import numpy as np import argparse import cv2

Construct a parameter parser

ap = argparse.ArgumentParser() ap.add_argument(“-i”, “–image”, required=True, help=”Path to the image”) args = vars(ap.parse_args())

Load the image

image = cv2.imread(args[“image”])

Channel separation, note that the order BGR is not RGB

(B, G, R) = cv2.split(image)

Displays the separated channels

Cv2.imshow (“Red”, R) cv2.imshow(“Green”, G) cv2.imshow(“Blue”, B) cv2.waitKey(0)

image.png

Green channel:

image.png

Red channel:

image.png

Strange, why is the color of the separated images all gray? Shouldn’t the blue channel be blue, the green channel be green, the red channel be red? Cv2. split split B, G, R is a single channel image, we can add the following two lines of code to verify:

Print “image shape:”,image. Shape print “single channal shape:”, r.shape

Image shape: (370, 690, 3) Single Channal Shape: (370, 690, 3) After cv2. Split, each channel is 370690 single channel image.

2# channel merge cv2.merge

In fact, the cv2.split image I originally imagined would look something like this:

Blue channel (component value of other channels is 0) :

PNG green channel (other channel component value is 0) :

Image.png Red channel (other channel component value is 0) :

image.png

The original above image is a three-channel image, but on the basis of the image separated by cv2.split, the other two channels are expanded, but the value of the other two channels is 0, and the above image is obtained. The code is as follows:

Generates a single-channel array with a value of 0

zeros = np.zeros(image.shape[:2], dtype = “uint8”)

Extend B, G, and R to form three channels. The other two channels are filled with an array with the value 0 above

cv2.imshow(“Blue”, cv2.merge([B, zeros, zeros])) cv2.imshow(“Green”, cv2.merge([zeros, G, zeros])) cv2.imshow(“Red”, Cv2. merge([Zeros, Zeros, R]) Cv2.waitkey (0) Summarizes the cv2.split function to separate the gray values of each channel (single channel image). The cv2.merge function merges single channels into multiple channels (multiple multi-channel images cannot be merged).

Original link: blog.csdn.net/eric_pycv/a…