🌐
OpenCV
docs.opencv.org › 3.4 › d8 › d01 › group__imgproc__color__conversions.html
OpenCV: Color Space Conversions
The function converts an input image from one color space to another. In case of a transformation to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR). Note that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the ...
🌐
OpenCV
docs.opencv.org › 4.x › df › d9d › tutorial_py_colorspaces.html
OpenCV: Changing Colorspaces
In this tutorial, you will learn how to convert images from one color-space to another, like BGR \(\leftrightarrow\) Gray, BGR \(\leftrightarrow\) HSV, etc. In addition to that, we will create an application to extract a colored object in a video · You will learn the following functions: cv.cvtColor(), cv.inRange(), etc. There are more than 150 color-space conversion methods available in OpenCV.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-opencv-cv2-cvtcolor-method
Python OpenCV | cv2.cvtColor() method - GeeksforGeeks
August 11, 2025 - Always choose a color conversion code that matches your source image format. ... Here’s a simple Python code using OpenCV to read an image, convert it to grayscale and display it in a window.
🌐
Roboflow
roboflow.com › use opencv › convert color space with cv2.cvtcolor
Convert Color Space with cv2.cvtcolor (OpenCV)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # convert BGR to RGB image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # convert BGR to HSV · OpenCV can be used with the open source supervision Python package. supervision provides an extensive range of functionalities for working with computer vision models.
🌐
Stack Overflow
stackoverflow.com › questions › 57786832 › how-to-convert-color-with-opencv
python - How to convert color with OpenCV? - Stack Overflow
I am struggling with converting red color to another color, blue, yellow, green and so on. I've already achieved to convert red color to gray scale color as bellow. img2 = cv2.imread('output2...
🌐
Educative
educative.io › answers › what-is-the-cv2cvtcolor-method
What is the cv2.cvtcolor() method?
Line 9: We perform color space conversions using the cv2.cvtColor() method. First, we load the input image as a BGR image (commonly used in OpenCV). Then, we convert it to an RGB image using the cv2.COLOR_BGR2RGB conversion code.
🌐
Jancy Standard Library
vovkos.github.io › doxyrest-showcase › opencv › sphinx_rtd_theme › page_imgproc_color_conversions.html
Color conversions — OpenCV Documentation
The output RGB components of a pixel are interpolated from 1, 2, or 4 neighbors of the pixel having the same color. There are several modifications of the above pattern that can be achieved by shifting the pattern one pixel left and/or one pixel up. The two letters \(C_1\) and \(C_2\) in the conversion constants CV_Bayer \(C_1 C_2\) 2BGR and CV_Bayer \(C_1 C_2\) 2RGB indicate the particular pattern type.
🌐
Roboflow
blog.roboflow.com › opencv-color-spaces
OpenCV Color Spaces and Conversion: An Introduction
November 11, 2025 - Learn how to convert data from the default BGR OpenCV color space to RGB, HSV, and more.
🌐
Note.nkmk.me
note.nkmk.me › home › python › opencv
Convert BGR and RGB with Python, OpenCV (cvtColor) | note.nkmk.me
May 14, 2019 - The parameter code when converting from RGB to BGR is cv2.COLOR_RGB2BGR. Use this when reading an image file as a PIL.Image, convert it to ndarray, and save it using OpenCV imwrite().
Find elsewhere
🌐
OpenCV-Python Tutorials
opencv24-python-tutorials.readthedocs.io › en › latest › py_tutorials › py_imgproc › py_colorspaces › py_colorspaces.html
Changing Colorspaces — OpenCV-Python Tutorials beta documentation
In this tutorial, you will learn how to convert images from one color-space to another, like BGR Gray, BGR HSV etc. In addition to that, we will create an application which extracts a colored object in a video · You will learn following functions : cv2.cvtColor(), cv2.inRange() etc. There are more than 150 color-space conversion methods available in OpenCV...
🌐
OpenCV-Python Tutorials
opencv24-python-tutorials.readthedocs.io › en › stable › py_tutorials › py_imgproc › py_colorspaces › py_colorspaces.html
Changing Colorspaces — OpenCV-Python Tutorials 1 documentation
In this tutorial, you will learn how to convert images from one color-space to another, like BGR Gray, BGR HSV etc. In addition to that, we will create an application which extracts a colored object in a video · You will learn following functions : cv2.cvtColor(), cv2.inRange() etc. There are more than 150 color-space conversion methods available in OpenCV...
🌐
LearnOpenCV
learnopencv.com › home › tutorial › color spaces in opencv (c++ / python)
Color spaces in OpenCV (C++/Python) | LearnOpenCV
November 14, 2024 - Conversion between RGB, HSV, LAB and YCrCb color spaces and how to choose among them using OpenCV ( python and C++ )
🌐
PyImageSearch
pyimagesearch.com › home › blog › opencv color spaces ( cv2.cvtcolor )
OpenCV Color Spaces ( cv2.cvtColor ) - PyImageSearch
June 8, 2023 - This function accepts two arguments: the actual image that we want the convert, followed by the output color space. Since OpenCV represents our image in BGR order rather than RGB, we specify the cv2.COLOR_BGR2HSV flag to indicate that we want to convert from BGR to HSV.
🌐
Rust
docs.rs › opencv › latest › opencv › imgproc › fn.cvt_color.html
cvt_color in opencv::imgproc - Rust
pub fn cvt_color( src: &impl ToInputArray, dst: &mut impl ToOutputArray, code: i32, dst_cn: i32, hint: AlgorithmHint, ) -> Result<()> ... Converts an image from one color space to another.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › programming languages tutorial › opencv cvtcolor
OpenCV cvtColor | Concept of cvtColor() function with Definition, Syntax
April 19, 2023 - OpenCV program in python to demonstrate cvtColor() function using which we are going to change the color space of a given image to different color space and display the resulting image with changed color space as the output on the screen: ... ...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-color-spaces-in-opencv-using-cplusplus
How to convert color spaces in OpenCV using C++?
March 10, 2021 - Here, we used a new header named 'imgproc.hpp'. This 'imgproc.hpp' is the abbreviation of Image Processing. To convert color spaces, we need to use 'cvtColor()' function of OpenCV. This function is defined in 'imgproc' header file.
🌐
OpenCV
opencv.org › home › news › color spaces in opencv
Color spaces in OpenCV
April 30, 2025 - When working with OpenCV, video frames are read in BGR format by default. If we convert these frames to RGB using cv2.cvtColor and save them directly with cv2.VideoWriter, the colors appear distorted during playback. This happens because VideoWriter still expects BGR frames, and writing RGB ...
Top answer
1 of 4
24

This is currently set up to expect a grayscale image as input. I think that you are asking how to adapt it to accept a colour input image and return a colour output image. You don't need to change much:

cv::Mat CVCircles::detectedCirclesInImage(cv::Mat img, double dp, double minDist, double  param1, double param2, int min_radius, int max_radius) {

  if(img.empty())
        {
        cout << "can not open image " << endl;
        return img;
        }
  Mat img;

  if (img.type()==CV_8UC1) {
              //input image is grayscale
    cvtColor(img, cimg, CV_GRAY2RGB);

  } else {
              //input image is colour
    cimg = img;
    cvtColor(img, img, CV_RGB2GRAY);
  }

the rest stays as is.

If your input image is colour, you are converting it to gray for processing by HoughCircles, and applying the found circles to the original colour image for output.

2 of 4
1

The cvtImage routine will simply copy your gray element to each of the three elements R, G, and B for each pixel. In other words if the pixel gray value is 26, then the new image will have R = 26, G = 26, B = 26.

The image presented will still LOOK grayscale even though it contains all 3 color components, all you have essentially done is to triple the space necessary to store the same image.

If indeed you want color to appear in the image (when you view it), this is truly impossible to go from grayscale back to the ORIGINAL colors. There are however means of pseudo-coloring or false coloring the image.

http://en.wikipedia.org/wiki/False_color

http://blog.martinperis.com/2011/09/opencv-pseudocolor-and-chroma-depth.html

http://podeplace.blogspot.com/2012/11/opencv-pseudocolors.html

🌐
DataFlair
data-flair.training › blogs › image-conversion-using-opencv
Image Conversion using OpenCV - Colored, Grayscale and Binary - DataFlair
March 4, 2024 - The cv2.cvtColor() function in OpenCV is specifically designed for color space conversion. Bypassing the cv2.COLOR_BGR2GRAY flag as the conversion parameter, we instruct the function to convert a BGR color image to grayscale.
🌐
MachineLearningMastery
machinelearningmastery.com › home › blog › how to read, write, display images in opencv and converting color spaces
How to Read, Write, Display Images in OpenCV and Converting Color Spaces - MachineLearningMastery.com
January 30, 2024 - OpenCV Color Conversion Codes, https://docs.opencv.org/4.x/d8/d01/group__imgproc__color__conversions.html#func-members · In this tutorial, you familiarised yourself with the most basic OpenCV operations that are essential when working with images. ... How a digital image is formulated in terms of its spatial coordinates and intensity values. How an image is read and displayed in OpenCV. How an image’s pixel values can be accessed. How an image may be converted from one color space to another.