In Python OpenCV Tutorial, Explained How to split and merge image using numpy indexing and python OpenCV cv2.split() & cv2.merge() function?
Syntax: cv2.split(m[, mv]) -> mv
Parameters:
. @overload . @param m input multi-channel array. . @param mv output vector of arrays; the arrays themselves are reallocated, if needed.
Syntax: cv2.merge(mv[, dst]) -> dst
Parameters:
. @param mv input vector of matrices to be merged; all the matrices in mv must have the same . size and the same depth. . @param dst output array of the same size and the same depth as mv[0]; The number of channels will . be the total number of channels in the matrix array.
Split image using NumPy indexing
import cv2
img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\girl-eye.jpg"
img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))
img.shape ## image shape
img[:,:,0] # Blue image channel
img[:,:,1] # Green Image channel
img[:,:,2] # Red Image Channel
Split image using cv2.split()
b, g, r = cv2.split(img)
Time Calculation to split Image
For Split fucntion
import time
start_time = time.time()
for i in range(100):
r, g, r = cv2.split(img)
end_time = time.time()
print("Total time taken to split 100 images: ", end_time - start_time)
For Numpy Indexing
import time
start_time = time.time()
for i in range(100):
b = img[:, :, 0]
g = img[:, :, 1]
r = img[:, :, 2]
end_time = time.time()
print("Total time taken to split 100 images: ", end_time - start_time)
Show single channel
import cv2
img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\girl-eye.jpg"
img = cv2.imread(img_path)
img = cv2.resize(img, (400, 400))
cv2.imshow("Model Blue Image", img[:,:,0])
cv2.imshow("Model Green Image", img[:,:,1])
cv2.imshow("Model Red Image", img[:,:,2])
cv2.waitKey(0)
import cv2
img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\girl-eye.jpg"
img = cv2.imread(img_path)
img = cv2.resize(img, (400, 400))
b, g, r = cv2.split(img)
cv2.imshow("Model Blue Image", b)
cv2.imshow("Model Green Image", g)
cv2.imshow("Model Red Image", r)
cv2.waitKey(0)
cv2.destroyAllWindows()
Merge Image
img_merge = cv2.merge([b,g,r])
import cv2
img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\girl-eye.jpg"
img = cv2.imread(img_path)
img = cv2.resize(img, (400, 400))
b, g, r = cv2.split(img)
img_merge = cv2.merge([b,g,r])
cv2.imshow("Model Blue Image", b)
cv2.imshow("Model Green Image", g)
cv2.imshow("Model Red Image", r)
cv2.imshow("Merge Model Image", img_merge)
cv2.waitKey(0)
cv2.destroyAllWindows()