Rgb to hsv python opencv

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Convert RGB colors to HSV to use with your OpenCV projects

ahadcove/Rgb2HSV

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Rgb2Hsv — Easily convert your RGB colors to HSV

OpenCV uses HSV for many of it’s conversions and comparing and is one of the ideal color ranges to develop in.

You will see yourself trying to convert using various different ways, but with the Rgb2Hsv script, you won’t have any issues

Simply enter into your command line python Rgb2Hsv.py 255 0 0 And it will convert your Rgb colors in an instant to the command line.

If you’re copying and pasting from another site an Rgb colors feel free to paste it in with the commas and it will handle it accordingly: python Rgb2Hsv.py 125, 200, 50

About

Convert RGB colors to HSV to use with your OpenCV projects

Источник

How to convert an RGB image to HSV image using OpenCV Python?

An RGB (colored) image has three channels, Red, Blue and Green. A colored image in OpenCV has a shape in [H, W, C] format, where H, W, and C are image height, width and number of channels. All three channels have a value range between 0 and 255.

The HSV image also has three channels, the Hue, Saturation and Value channels. In OpenCV, the values of the Hue channel range from 0 to 179, whereas the Saturation and Value channels range from 0 to 255.

In OpenCV, to convert an RGB image to HSV image, we use the cv2.cvtColor() function. This function is used to convert an image from one color space to another.

This function takes two arguments− first the input image and second the color conversion method. See the syntax given below −

cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)

Steps

To convert an RGB image to HSV image, follow the steps given below −

Import the required library. In all the following Python examples, the required Python library is OpenCV. Make sure you have already installed it.

Read the input RGB image using cv2.imread(). The RGB image read using this method is in BGR format. Optionally assign the read BGR image to bgr_img.

bgr_img = cv2.imread('water.jpg')

Now convert this BGR image to HSV image as below using cv2.cvtColor() function. Optionally assign the converted HSV image to hsv_img.

hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)

Display the above converted HSV image.

cv2.imshow('HSV image', hsv_img) cv2.waitKey(0) cv2.destroyAllWindows()

Input Image

We will use this image as the input file in the following example.

Example

This Python program converts an RGB image to HSV image.

import cv2 # read the input RGB image as BGR format bgr_img = cv2.imread('water.jpg') # Convert the BGR image to HSV Image hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV) cv2.imwrite('hsv_image.jpg', hsv_img) # Display the HSV image cv2.imshow('HSV image', hsv_img) cv2.waitKey(0) cv2.destroyAllWindows()

Output

When you run the above program, it will produce the following output −

Notice the difference between the original RGB image and the HSV image.

Источник

Rgb to hsv python opencv

the color conversion codes

add alpha channel to RGB or BGR image

remove alpha channel from RGB or BGR image

convert between RGB and BGR color spaces (with or without alpha channel)

convert between RGB/BGR and grayscale, color conversions

convert between RGB/BGR and BGR565 (16-bit images)

convert between grayscale to BGR565 (16-bit images)

convert between RGB/BGR and BGR555 (16-bit images)

convert between grayscale and BGR555 (16-bit images)

convert RGB/BGR to CIE XYZ, color conversions

convert RGB/BGR to luma-chroma (aka YCC), color conversions

convert RGB/BGR to HSV (hue saturation value) with H range 0..180 if 8 bit image, color conversions

convert RGB/BGR to CIE Lab, color conversions

convert RGB/BGR to CIE Luv, color conversions

convert RGB/BGR to HLS (hue lightness saturation) with H range 0..180 if 8 bit image, color conversions

backward conversions HSV to RGB/BGR with H range 0..180 if 8 bit image

backward conversions HLS to RGB/BGR with H range 0..180 if 8 bit image

convert RGB/BGR to HSV (hue saturation value) with H range 0..255 if 8 bit image, color conversions

convert RGB/BGR to HLS (hue lightness saturation) with H range 0..255 if 8 bit image, color conversions

backward conversions HSV to RGB/BGR with H range 0..255 if 8 bit image

backward conversions HLS to RGB/BGR with H range 0..255 if 8 bit image

convert between RGB/BGR and YUV

Demosaicing, see color conversions for additional information.

equivalent to RGGB Bayer pattern

equivalent to GRBG Bayer pattern

equivalent to BGGR Bayer pattern

equivalent to GBRG Bayer pattern

equivalent to RGGB Bayer pattern

equivalent to GRBG Bayer pattern

equivalent to BGGR Bayer pattern

equivalent to GBRG Bayer pattern

equivalent to RGGB Bayer pattern

equivalent to GRBG Bayer pattern

equivalent to BGGR Bayer pattern

equivalent to GBRG Bayer pattern

Demosaicing using Variable Number of Gradients.

equivalent to RGGB Bayer pattern

equivalent to GRBG Bayer pattern

equivalent to BGGR Bayer pattern

equivalent to GBRG Bayer pattern

equivalent to RGGB Bayer pattern

equivalent to GRBG Bayer pattern

equivalent to BGGR Bayer pattern

equivalent to GBRG Bayer pattern

equivalent to RGGB Bayer pattern

equivalent to GRBG Bayer pattern

equivalent to BGGR Bayer pattern

equivalent to GBRG Bayer pattern

equivalent to RGGB Bayer pattern

equivalent to GRBG Bayer pattern

equivalent to BGGR Bayer pattern

equivalent to GBRG Bayer pattern

Demosaicing with alpha channel.

equivalent to RGGB Bayer pattern

equivalent to GRBG Bayer pattern

equivalent to BGGR Bayer pattern

equivalent to GBRG Bayer pattern

equivalent to RGGB Bayer pattern

equivalent to GRBG Bayer pattern

equivalent to BGGR Bayer pattern

equivalent to GBRG Bayer pattern

Function Documentation

◆ cvtColor()

Converts an image from one color space to another.

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 bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on.

The conventional ranges for R, G, and B channel values are:

  • 0 to 255 for CV_8U images
  • 0 to 65535 for CV_16U images
  • 0 to 1 for CV_32F images

In case of linear transformations, the range does not matter. But in case of a non-linear transformation, an input RGB image should be normalized to the proper value range to get the correct results, for example, for RGB \(\rightarrow\) L*u*v* transformation. For example, if you have a 32-bit floating-point image directly converted from an 8-bit image without any scaling, then it will have the 0..255 value range instead of 0..1 assumed by the function. So, before calling cvtColor , you need first to scale the image down:

If you use cvtColor with 8-bit images, the conversion will have some information lost. For many applications, this will not be noticeable but it is recommended to use 32-bit images in applications that need the full range of colors or that convert an image before an operation and then convert back.

If conversion adds the alpha channel, its value will set to the maximum of corresponding channel range: 255 for CV_8U, 65535 for CV_16U, 1 for CV_32F.

Parameters

src input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC. ), or single-precision floating-point.
dst output image of the same size and depth as src.
code color space conversion code (see ColorConversionCodes).
dstCn number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code.

See also Color conversions Examples: samples/cpp/camshiftdemo.cpp, samples/cpp/edge.cpp, samples/cpp/facedetect.cpp, samples/cpp/ffilldemo.cpp, samples/cpp/lkdemo.cpp, samples/cpp/train_HOG.cpp, samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp, samples/cpp/tutorial_code/ImgTrans/houghlines.cpp, samples/cpp/tutorial_code/ImgTrans/Sobel_Demo.cpp, samples/cpp/tutorial_code/ml/introduction_to_pca/introduction_to_pca.cpp, samples/cpp/watershed.cpp, samples/dnn/colorization.cpp, samples/dnn/text_detection.cpp, and samples/tapi/hog.cpp.

◆ cvtColorTwoPlane()

void cv::cvtColorTwoPlane ( InputArray src1,
InputArray src2,
OutputArray dst,
int code
)
Python:
cv.cvtColorTwoPlane( src1, src2, code[, dst] ) -> dst

Converts an image from one color space to another where the source image is stored in two planes.

This function only supports YUV420 to RGB conversion as of now.

◆ demosaicing()

void cv::demosaicing ( InputArray src,
OutputArray dst,
int code,
int dstCn = 0
)
Python:
cv.demosaicing( src, code[, dst[, dstCn]] ) -> dst

main function for all demosaicing processes

Parameters

src input image: 8-bit unsigned or 16-bit unsigned.
dst output image of the same size and depth as src.
code Color space conversion code (see the description below).
dstCn number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code.

The function can do the following transformations:

  • Demosaicing using bilinear interpolation COLOR_BayerBG2BGR , COLOR_BayerGB2BGR , COLOR_BayerRG2BGR , COLOR_BayerGR2BGRCOLOR_BayerBG2GRAY , COLOR_BayerGB2GRAY , COLOR_BayerRG2GRAY , COLOR_BayerGR2GRAY
  • Demosaicing using Variable Number of Gradients. COLOR_BayerBG2BGR_VNG , COLOR_BayerGB2BGR_VNG , COLOR_BayerRG2BGR_VNG , COLOR_BayerGR2BGR_VNG
  • Edge-Aware Demosaicing. COLOR_BayerBG2BGR_EA , COLOR_BayerGB2BGR_EA , COLOR_BayerRG2BGR_EA , COLOR_BayerGR2BGR_EA
  • Demosaicing with alpha channel COLOR_BayerBG2BGRA , COLOR_BayerGB2BGRA , COLOR_BayerRG2BGRA , COLOR_BayerGR2BGRA

doxygen

Generated on Thu Jul 27 2023 02:07:44 for OpenCV by 1.8.13

Источник

How to convert RGB to HSV in OpenCV Python

You can convert an image from RGB to HSV using OpenCV Python by following the given steps. I highly recommend you get the “Computer Vision: Models, Learning, and Inference Book” to learn Computer Vision.

How to convert RGB to HSV in OpenCV Python

Step 1

Import the OpenCV library. If OpenCV is not installed in your system then first install it using This Method.

import cv2 #cv2 is used for OpenCV library

Step 2

Now read the image from the location. In my case “F:\\AiHints” is the location and “apple.jpg” is the name of the image. Change it according to your image location and name.

image = cv2.imread("C://AiHints//apple.jpg") #imread is use to read an image from a location

Step 3

Now convert the RGB image into HSV.

hsv_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)

Step 4

Now display the original, and HSV image using the following code.

cv2.imshow("Original Image", image) cv2.imshow("HSV Image", hsv_image)

Step 5

waitKey() open the image for a specific time in milliseconds until you press any key. The function cv2.destroyAllWindows() will destroy all the windows that we created.

cv2.waitKey(0) cv2.destroyAllWindows()

Original Image HSV Image

Источник

Читайте также:  Php генерация массива чисел
Оцените статью