Skip to content

Colorspace Manipulation

Abhishek Thakur edited this page Apr 30, 2020 · 50 revisions

VidGear Logo

Colorspace Manipulation for VideoCapture Gears

All VideoCapture Gears namely CamGear, PiGear, ScreenGear, and VideoGear supports internal on-the-fly Source ColorSpace conversion/manipulation.

 

 

Table of Contents

 

 

Altering Colorspace

There are two ways to alter Source Colorspace:

  • By using the colorspace parameter: (Recommended)

    Primarily, the colorspace (string) parameter of the respective videocapture API can be used to easily altering the Colorspace of the input Source during that API's initialization.

  • By using color_space global variable:

    Alternatively, at any instance, the color_space (integer) global variable can be used for direct Source colorspace conversion at runtime.

    ⚠️ Using color_space global variable is disabled in VideoGear!

 

 


⚠️ Important Information:

  • The color_space global variable is Disabled in VideoGear API, calling it will result in AttribueError!

  • Incorrect colorspace parameter or color_space global variable value, will immediately revert Source colorspace to default BGR.


 

 

Supported colorspace conversions:

Any conversion from default video source colorspace (i.e. BGR in case of OpenCV) to any other colorspace and vice-versa(use None to revert back), is possible and thereby supported.

The following table exhibits conversion-values for a default BGR colorspace source:

Supported Conversion-Values for a OpenCV source (with default BGR-colorspace):

💡 You can directly substitute these conversion values in colorspace(string) parameter of the respective videocapture API.

Supported Conversion Values Description
COLOR_BGR2BGRA BGR to BGRA
COLOR_BGR2RGBA BGR to RGBA
COLOR_BGR2RGB BGR to RGB backward conversions to RGB/BGR
COLOR_BGR2GRAY BGR to GRAY
COLOR_BGR2BGR565 BGR to BGR565
COLOR_BGR2BGR555 BGR to BGR555
COLOR_BGR2XYZ BGR to CIE XYZ
COLOR_BGR2YCrCb BGR to luma-chroma (aka YCC)
COLOR_BGR2HSV BGR to HSV (hue saturation value)
COLOR_BGR2Lab BGR to CIE Lab
COLOR_BGR2Luv BGR to CIE Luv
COLOR_BGR2HLS BGR to HLS (hue lightness saturation)
COLOR_BGR2HSV_FULL BGR to HSV_FULL
COLOR_BGR2HLS_FULL BGR to HLS_FULL
COLOR_BGR2YUV BGR to YUV
COLOR_BGR2YUV_I420 BGR to YUV 4:2:0 family
COLOR_BGR2YUV_IYUV BGR to IYUV
COLOR_BGR2YUV_YV12 BGR to YUV_YV12
None Back to default colorspace i.e. BGR

💡 You can check the rest of OpenCV Colorspace Codes here.

 

 

Basic Usage: 🔨

In Vidgear, we easily manipulate Source Colorspace with colorspace parameter and the global color_space variable for runtime Source Colorspace conversions

In this code we will start HSV (a.k.a HSV(hue, saturation, value)) as Source Colorspace in CamGear API (similiar usage in other Gears too), and then will display GRAY (a.k.a Grayscale) colorspace when w key is pressed, and then display LAB (a.k.a CIELAB) colorspace when e key is pressed, finally default colorspace (i.e. BGR) when s key is pressed. Also, quit when q key is pressed.

# import required libraries
from vidgear.gears import CamGear
import cv2

# Open any source of your choice, like Webcam first index(i.e. 0) and change its colorspace to `HSV`
stream = CamGear(source=0, colorspace = 'COLOR_BGR2HSV', logging=True).start()

# loop over
while True:
  
    # read HSV frames
    frame = stream.read()

    # check for frame if Nonetype
    if frame is None:
        break


    # {do something with the HSV frame here}


    # Show output window
    cv2.imshow("Output Frame", frame)

    # check for key if pressed
    key = cv2.waitKey(1) & 0xFF

    # check if 'w' key is pressed
    if key == ord("w"):
        #directly change colorspace at any instant
        stream.color_space = cv2.COLOR_BGR2GRAY #Now colorspace is GRAY
      
    # check for 'e' key is pressed
    if key == ord("e"):
        stream.color_space = cv2.COLOR_BGR2LAB  #Now colorspace is CieLAB
   
    # check for 's' key is pressed
    if key == ord("s"):
         stream.color_space = None #Now colorspace is default(ie BGR)

    # check for 'q' key is pressed
    if key == ord("q"):
      break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.stop()