Python OpenCV Color Conversion Library Explorer
A Visual Guide to cv2.cvtColor Conversion
OpenCV’s color conversions can feel like alphabet soup—BGR, RGB, HSV, LAB, XYZ, YUV, Bayer patterns… and dozens of packed/planar variants used by cameras and codecs. If you’ve ever thought “which code do I use for NV12 again?” this post is for you.
You’ll find a visual reference that displays 128 transformations for cv2.cvtColor().
BGR vs. RGB confusion. OpenCV loads as BGR. Most other libraries (matplotlib, PIL, web) assume RGB. Convert before showing.
“Full” vs. “non-full” HSV/HLS. Codes like
HSV_FULLuse the complete 0–255 channel range. Use them for precise color pickers and grading.YUV alphabet soup.
Planar 4:2:0: I420 / IYUV / YV12 (Y plane, then U/V planes).
Semi-planar 4:2:0: NV12 / NV21 (Y plane + interleaved UV).
Packed 4:2:2: YUY2 / UYVY / YVYU (luma and chroma interleaved per pixel pair).
Pick the code that matches your camera/decoder output; otherwise colors will be wildly wrong.
Bayer patterns. RAW sensors use patterns like RGGB, BGGR, GRBG, GBRG. Choose the correct demosaicing constant—or you’ll get checkerboard artifacts or tinted images.
Q: Which YUV should I use for Android camera frames?
A: Most camera2 outputs are NV21 or YUV_420_888 (often NV12-like). Start with COLOR_YUV2BGR_NV21 or COLOR_YUV2BGR_NV12, then verify by eye.
Q: LAB or HSV for color thresholding?
A: Use HSV for quick hue/saturation masks. Use LAB when you want more perceptual uniformity (e.g., subtle color differences, color correction, grading).

