9

I am a beginner in opencv. I am using opencv v2.1. I have converted an RGB image to HSV image. Now I want to obtain single channels Hue, Value and Saturation separately. What should I do? I have seen similar questions here but No-one answered that. Kindly help.

diggy
  • 351
  • 1
  • 5
  • 23
userXktape
  • 227
  • 3
  • 5
  • 15

3 Answers3

14

You can access the same way you were accessing for RGB image where 1st channel will be for H, 2nd channel for S and 3rd channel for V.

If you are using OpenCV 2.1, you must be using IplImage then, right? like if your HSV image is IplImage *src.

IplImage* h = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
IplImage* s = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
IplImage* v = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 1 );
// Split image onto the color planes
cvSplit( src, h, s, v, NULL );

cvSplit function splits a multichannel array into several single channels. Correct me if I am wrong. I would recommend using OpenCV 2.4. It has structs like cvMat which are very easy to handle just like 2D arrays.

EDIT: If you are using Mat then you can separate the channels out easily. Let's say your hsv mat is Mat img_hsv. Then :

vector<Mat> hsv_planes;
split( img_hsv, hsv_planes );
hsv_planes[0] // H channel
hsv_planes[1] // S channel
hsv_planes[2] // V channel

See if you can work out with this.

diggy
  • 351
  • 1
  • 5
  • 23
  • So after you do what you've gotta do, how to you recombine the H,S,V Mats into a composite image again? – Kevin_TA Feb 18 '15 at 19:47
  • 1
    Hi, for the second method, the datatype of each of the channels is `Mat`. It prints something like `Mat [ 8*8*CV_8UC3, isCont=true, isSubmat=false, nativeObj=0x514dee50 ]`. How can I find it in the form of simple values, so that I can print it on the screen, as well as use the values to somehow preview the color, for verification? – Solace Dec 03 '15 at 04:46
8

Solution for Python:

import cv2
from matplotlib import pyplot as plt

# Read image in BGR
img_path = "test.jpg"
img = cv2.imread(img_path)

# Convert BGR to HSV and parse HSV
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = hsv_img[:, :, 0], hsv_img[:, :, 1], hsv_img[:, :, 2]

# Plot result images
plt.imshow("Original", cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.imshow("HSV", hsv_img)
plt.imshow("H", h)
plt.imshow("S", s)
plt.imshow("V", v)
plt.show()
Leonid Dashko
  • 3,657
  • 1
  • 18
  • 26
5

Here it is for a Mat:

    cv::Mat hsv_image  = ...;

    std::vector<cv::Mat> hsv_channels;
    cv::split(hsv_image, hsv_channels);
    cv::Mat h_image = hsv_channels[0];
    cv::Mat s_image = hsv_channels[1];
    cv::Mat v_image = hsv_channels[2];
Bull
  • 11,771
  • 9
  • 42
  • 53
  • Hi, the datatype of each of the channels is Mat. It prints something like Mat [ 8*8*CV_8UC3, isCont=true, isSubmat=false, nativeObj=0x514dee50 ]. How can I find it in the form of simple values, so that I can print it on the screen, as well as use the values to somehow preview the color, for verification? I'll appreciate your help. – Solace Dec 03 '15 at 04:45
  • 1
    @Solace, please submit your comment as a new question, with more detail about what you are trying achieve. You should also include your (Java?) code. – Bull Dec 04 '15 at 04:18