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.
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.
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.
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.
The original image
Blue
Green
Red
Recommended Posts