[PYTHON] Separate color images into RGB images and display them with OpenCV

Introduction

OpenCV (Open Source Computer Vision Library) is a collection of BSD-licensed video / image processing libraries. There are many algorithms for image filtering, template matching, object recognition, video analysis, machine learning, and more.

Example of motion tracking using OpenCV (OpenCV Google Summer of Code 2015) https://www.youtube.com/watch?v=OUbUFn71S4s

Click here for installation and easy usage http://qiita.com/olympic2020/items/d5d475a446ec9c73261e

Click here for still image filtering Try edge detection with OpenCV

Click here for processing video files Try converting videos in real time with OpenCV Try converting webcam / camcorder videos in real time with OpenCV

No matter what kind of video processing you do, it is basic to filter one image. This time, as the basis of image processing, I will separate the color image into RGB images and display them.

RGB separation algorithm

I will explain the algorithm that separates color images into RGB. First, separate the color image into RGB. This can be easily done with cv2.split (). Since the separated image is for one channel, if it is displayed as it is, it will be displayed as grayscale. After separating into RGB, in order to display in each color, it is necessary to create a color image in which other colors are supplemented with a zero-filled image. Numpy.zeros () is used to create a zero-filled image, and cv2.merge () is used to combine 3 channels into one image.

I tried to make a schematic diagram of the flow of separating RGB and displaying in each color. Please refer to it when understanding the program.

separate.png

program

channels.py


import cv2
import numpy as np

#Constant definition
FILE_PREFIX = "google"
ORG_FILE_NAME = "google_org.png "

BLUE_FILE_NAME = FILE_PREFIX + "_b.png "
GREEN_FILE_NAME = FILE_PREFIX + "_g.png "
RED_FILE_NAME = FILE_PREFIX + "_r.png "

ORG_WINDOW_NAME = "org"
BLUE_WINDOW_NAME = "blue"
GREEN_WINDOW_NAME = "green"
RED_WINDOW_NAME = "red"

#Load the original image
org_img = cv2.imread(ORG_FILE_NAME, cv2.IMREAD_COLOR)

#Zero padded image array
if len(org_img.shape) == 3:
    height, width, channels = org_img.shape[:3]
else:
    height, width = org_img.shape[:2]
    channels = 1
zeros = np.zeros((height, width), org_img.dtype)

#RGB separation
img_blue_c1, img_green_c1, img_red_c1 = cv2.split(org_img)

img_blue_c3 = cv2.merge((img_blue_c1, zeros, zeros))
img_green_c3 = cv2.merge((zeros, img_green_c1, zeros))
img_red_c3 = cv2.merge((zeros, zeros, img_red_c1))

#Create window
cv2.namedWindow(ORG_WINDOW_NAME)
cv2.namedWindow(BLUE_WINDOW_NAME)
cv2.namedWindow(GREEN_WINDOW_NAME)
cv2.namedWindow(RED_WINDOW_NAME)

#Show in window
cv2.imshow(ORG_WINDOW_NAME, org_img)
cv2.imshow(BLUE_WINDOW_NAME, img_blue_c3)
cv2.imshow(GREEN_WINDOW_NAME, img_green_c3)
cv2.imshow(RED_WINDOW_NAME, img_red_c3)

#Save to file
cv2.imwrite(BLUE_FILE_NAME, img_blue_c3)
cv2.imwrite(GREEN_FILE_NAME, img_green_c3)
cv2.imwrite(RED_FILE_NAME, img_red_c3)

#End processing
cv2.waitKey(0)
cv2.destroyAllWindows()

If you specify cv2.IMREAD_UNCHANGED in cv2.imread (), you can get the information of 4 channels with α plane. On the other hand, if you specify cv2.IMREAD_COLOR in cv2.imread (), you can get the information of 3 channels of RGB plane only. This time, I specified cv2.IMREAD_COLOR to get only RGB.

Execution result

I tried to separate the original image that processed the white part of the Google homepage into black with a script into an RGB image.

google_org.png The original image

google_b.png             Blue

google_g.png             Green

google_r.png             Red

Recommended Posts

Separate color images into RGB images and display them with OpenCV
Capturing images with Pupil, python and OpenCV
Wavelet transform of images with PyWavelets and OpenCV
Display embedded images of mp3 and flac with mutagen
I made a program to convert images into ASCII art with Python and OpenCV
Read the VTK file and display the color map with jupyter.
[Python] Try to recognize characters from images with OpenCV and pyocr
Shining life with Python and OpenCV
Load gif images with Python + OpenCV
Draw shapes with OpenCV and PIL
Upload and download images with falcon
Try to separate the background and moving object of the video with OpenCV
[python, openCV] base64 Face recognition with images
[Python] Read images with OpenCV (for beginners)
Importing and exporting GeoTiff images with Python
Cut out and connect images with ImageMagick