Skip to content

Instantly share code, notes, and snippets.

@palaashatri
Created August 12, 2020 06:01
Show Gist options
  • Save palaashatri/c1c26e05c998eb3a90e4af61f969e547 to your computer and use it in GitHub Desktop.
Save palaashatri/c1c26e05c998eb3a90e4af61f969e547 to your computer and use it in GitHub Desktop.
Demonstration of BGR and HSV channels of an image in OpenCV
import numpy as np
import cv2
color = cv2.imread("image.jpg",1) # Read image in color (1) mode
cv2.imshow("Image",color)
cv2.moveWindow("Image",0,0)
print(color.shape)
height,width,channels = color.shape
b,g,r = cv2.split(color) # Split image into Blue, Green and Red channels
rgb_split = np.empty([height,width*3,3],'uint8') # Initialize an empty NumPy array of height,width and 3 channels
rgb_split[:,0:width] = cv2.merge([b,b,b]) # array of Blue pixels
rgb_split[:,width:width*2] = cv2.merge([g,g,g]) # array of Green pixels
rgb_split[:,width*2:width*3] = cv2.merge([r,r,r]) # array of Red pixels
cv2.imshow("Channels",rgb_split) # show combined image of split channeled images
cv2.moveWindow("Channels",0,height)
hsv = cv2.cvtColor(color,cv2.COLOR_BGR2HSV) # Comvert BGR images to Hue-Saturation-Value (HSV) image
h,s,v = cv2.split(hsv)
hsv_split = np.concatenate((h,s,v),axis = 1) # axis = 1 : to tell NumPy to orgainse these images side-by-side
cv2.imshow("Split HSV",hsv_split)
cv2.waitKey(0) # wait for user input (Press any Key)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment