In our previous article, you learned some basic operations such as how to read, display, and save images using OpenCV. In this article, you learn some of the most fundamental concepts in color images. Keep reading
Install OpenCV And Other Important library
!pip install opencv-python # If you use Google colab, this step is not needed.
# Import other library
import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline
from IPython.display import Image
plt.rcParams['image.cmap'] = 'gray'
Read And Display Color Images OpenCv
Letβs start with reading color images. In this example I am using the Apple logo, which is (JPG) format.
# Read the image
apple_logo = "/content/drive/MyDrive/OpenCV-Article/module_1/apple_logo.jpg"
logo_img = cv2.imread(apple_logo)
# Print the size of the image.
print("Image size is: ", logo_img.shape)
print("Image data type is: ", logo_img.dtype)
# Display logo
plt.figure(figsize = (10, 10))
plt.imshow(logo_img);
**Note π‘:**One thing notice image we see and our original image is not the same. Because OpenCV stores images in BGR format and our image is RGB.
# Display our color image
logo_img_rgb = cv2.cvtColor(logo_img,cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10,10))
plt.imshow(logo_img_rgb);
Letβs see one more example. β¬ In this time i am using OpenCV Logo and this image is PNG format.
Read And Display A PNG Image OpenCV
# Read PNG image
opencv_logo = "/content/drive/MyDrive/OpenCV-Article/module_1/opencv.png"
# Read the image.
opencv_logo = cv2.imread(opencv_logo, cv2.IMREAD_COLOR)
# Print the size of the image.
print("Image size is ", opencv_logo.shape)
# Display the image.
plt.figure(figsize = (12, 12))
plt.imshow(opencv_logo);
Noteπ‘: PNG format has one extra color channel and that is an alpha channel.
# Read the image.
opencv_logo_path = "/content/drive/MyDrive/OpenCV-Article/module_1/opencv.png"
opencv_logo = cv2.imread(opencv_logo_path, cv2.IMREAD_UNCHANGED)
# Print the size of the image.
print(opencv_logo.shape)
# Display the image.
plt.figure(figsize = (12, 12))
plt.imshow(opencv_logo);
Change Color Of The Image
I am using OpenCV useful function to convert one color space to another color space name is cvtColor ( ). Most images used to be 3 bytes for each of these color channels. If the image contains an alpha channel it means that it will be 4 bytes per pixel.
opencv_logo = cv2.cvtColor(opencv_logo,cv2.COLOR_BGRA2RGBA)
# Display the image
plt.figure(figsize=(10,10))
plt.imshow(opencv_logo)
Splitting and Marging Color Channel OpenCV
Now itβs time to closely look at each of these color channels.
# Split the image into the B,G,R components
lake_bgr = cv2.imread("/content/drive/MyDrive/OpenCV-Article/module_1/lake.jpg", cv2.IMREAD_COLOR)
b , g , r = cv2.split(lake_bgr)
# Show the channels.
plt.figure(figsize=[20,10])
plt.subplot(141); plt.imshow(r); plt.title("Red Channel")
plt.subplot(142); plt.imshow(g); plt.title("Green Channel")
plt.subplot(143); plt.imshow(b); plt.title("Blue Channel")
# Merge the individual channels into a BGR image.
lakeMerged = cv2.merge((r,g,b))
# Display the merged output
plt.subplot(144)
plt.imshow(lakeMerged)
plt.title("Lake Image Merged Output");
What Is Color Space In Computer Vision ?
In simple terms, color space represents the color channel in the image. Color made is a mathematically created color in a color space with a unique tuple number (typically 3 or 4 values represent of colors components). Two of the most popular color space channels are RGB ( Red, Green, Blue ) and HSV ( Hue, Saturation, Value ). Color images are usually represented by 3 color channels as 8-bit unisigned integers for each of these color channels. and also you know individual color components can take on value [0, 255]. So you can represent 16.77 million unique color each color space (255 * 255 * 255)
.
Keep reading π₯
Convert RGB To HSV Colorspace OpenCV
In this down below example i am using OpenCV library cvtColor ( ) function to achieve this task. If you learn more read this [ Color Space Conversions ].
# Converting BGR to HSV
img_hsv = cv2.cvtColor(lake_bgr,cv2.COLOR_BGR2HSV)
# split the image color channel and stored individual channels
h,s,v = cv2.split(img_hsv)
# Display individual channels and also original image
plt.figure(figsize=(25,25))
plt.subplot(141); plt.imshow(h); plt.title("H Channels");
plt.subplot(142); plt.imshow(s); plt.title("S Channels");
plt.subplot(143); plt.imshow(v); plt.title("V Channels");
plt.subplot(144); plt.imshow(lake_bgr[:,:,::-1]); plt.title("Original Image");
Modifying Individual Color Channels.
h_new = h + 10 # i add an extra 10 value
img_hsv_merged = cv2.merge((h_new,s,v))
img_rgb_merged = cv2.cvtColor(img_hsv_merged,cv2.COLOR_HSV2RGB)
# Display each color channel and also see medify image.
plt.figure(figsize=(25,25))
plt.subplot(141); plt.imshow(h_new); plt.title("H Channels")
plt.subplot(142); plt.imshow(s); plt.title("S Channels");
plt.subplot(143); plt.imshow(v); plt.title("V Channels");
plt.subplot(144); plt.imshow(img_rgb_merged); plt.title("Modified Image");
If you notice any changes see the above and modified image and comment now below.
Save Images In File Manager
In our previous article we explained saving an image is very easy for OpenCV because one function has achieved this task.
First we read one new image is called [ lake image ].
β€΅οΈ After that I store this image in the file manager.
# Reading once more lake image
lake_img_path = "/content/drive/MyDrive/OpenCV-Article/module_1/lake.jpg"
lake_img_bgr = cv2.imread(lake_img_path,cv2.IMREAD_COLOR)
# Convert BGR to RGB
lake_img_rgb = cv2.cvtColor(lake_img_bgr,cv2.COLOR_BGR2RGB)
plt.figure(figsize=(5,5))
plt.imshow(lake_img_rgb);
# save image
cv2.imwrite("Saved_lake_img_rgb.jpg",lake_img_rgb)
# Display saved image
Image("Saved_lake_img_rgb.jpg")
What happened to our saved image color?
Note π‘β OpenCV imwrite ( )
function is accepted in BGR format. If you save this image RGB to one more step needed. See the code below for examples on how?
# once more save image but this time I put the BGR image
cv2.imwrite("Saved_lake_img_bgr.jpg", lake_img_bgr)
# Display saved image
Image("Saved_lake_img_bgr.jpg")