OpenCV 3 Video Capture & Switching Colorspaces RGB / HSV - 2020
Có thể bạn quan tâm
Video Capture & Switching Colorspaces RGB / HSV
bogotobogo.com site search: RGB vs HSVHSV, unlike RGB, separates the image intensity (lima), from the color information (chroma). This can be very useful if we want to focused on the intensity component, and leave the color components alone. Actually, in computer vision, we sometimes want to separate color components from intensity for various reasons, such as robustness to lighting changes, or removing shadows.
Picture source: HSL_and_HSV
Note: For HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255].
Why is color segmentation easier on HSV"The big reason is that it separates color information (chroma) from intensity or lighting (luma). Because value is separated, you can construct a histogram or thresholding rules using only saturation and hue. This in theory will work regardless of lighting changes in the value channel. In practice it is just a nice improvement. Even by singling out only the hue you still have a very meaningful representation of the base color that will likely work much better than RGB. The end result is a more robust color thresholding over simpler parameters." - from Why is color segmentation easier on HSV?
bogotobogo.com site search: Colorspace changeIn OpenCV, there are several colorspace conversions (more thant 150): RGB ↔ GRAY, RGB ↔ CIE, RGB ↔ YCrCb, RGB ↔ HSV, RGB ↔ HSL etc. But in this chapter, we'll be focused on the most widely used ones: BGR ↔ Gray and BGR ↔ HSV.
To convert colorspace, we'll use cv2.cvtColor(input_image, flag) where flag determines the type of conversion:
- BGR → Gray : cv2.COLOR_BGR2GRAY
- BGR → HSV :cv2.COLOR_BGR2HSV
The following code is to capture video which could be either live stream or file stream. To capture a video, we need to create a VideoCapture object. Its argument can be either the device index or the name of a video file. Device index is just the number to specify which camera. Normally one camera will be connected (index = 0) or the 2nd camera (index = 1).
# capture.py import numpy as np import cv2 # Capture video from camera # cap = cv2.VideoCapture(0) # Capture video from file cap = cv2.VideoCapture('BlueUmbrella.webm') while(cap.isOpened()): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the resulting frame cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()The video is available: BlueUmbrella.webm.
In next section, we will modify the code slightly to play with color space.
Tracking Blue ObjectIn this section, we want to track blue umbrella from the Pixar movie clip. How?
After converting BGR image to HSV, we extract a blue colored object. The reason for convderting to HSV colorspace is because it is more easier to represent a color than RGB colorspace. Here are the steps to take:
- Take each frame of the video.
- Convert from BGR to HSV color-space.
- We threshold the HSV image for a range of blue color.
- Now extract the blue object alone, we can do whatever on that image we want.
Here is the output from the code:
- Original image
- Threshold the HSV image to get only blue colors - mask
- Bitwise-AND mask and original image - res
Original files in other formats:
- cspace_blue.mp4
- cspace_blue.webm
- cspace_blue.ogv
Từ khóa » Hsv Color Space Python
-
Color Spaces In OpenCV | Python - GeeksforGeeks
-
Choosing The Correct Upper And Lower HSV Boundaries For Color ...
-
Changing Colorspaces - OpenCV Documentation
-
Learn The Working Of HSV Range In OpenCV - EduCBA
-
Changing Colorspaces - OpenCV-Python Tutorials - Read The Docs
-
The HSV Color Space Using OpenCV In Python | Delft Stack
-
Image Segmentation Using Color Spaces In OpenCV + Python
-
OpenCV Color Spaces ( tColor ) - PyImageSearch
-
Color Spaces In OpenCV (C++/Python) - LearnOpenCV
-
Detecting Colors (Hsv Color Space) - Opencv With Python - YouTube
-
Colorsys — Conversions Between Color Systems — Python 3.10.6 ...
-
Detecting Colors (Hsv Color Space) - Opencv With Python - Pysource
-
Python Math: Convert RGB Color To HSV Color - W3resource