简体   繁体   中英

How do I convert a RBG image to HSV and save the H, S, and V values into 3 separate images in C++ using OpenCV?

I have an RBG picture that I want to convert into HSV. I want to save the H, S, and V values into 3 separate images. How would I do it?

My current code consists of just converting the RGB image to HSV:

#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <unistd.h>
using namespace cv;
using namespace std;

Mat img_hsv, img_rgb;
img_rgb = imread("pic.png", 1);
cvtColor(img_rgb, img_hsv, COLORMAP_HSV);

You need to use COLOR_BGR2HSV instead of COLORMAP_HSV , as you're converting from BGR to HSV (OpenCV uses BGR by default). After that, you can split the image into its channels:

std::vector<Mat> channels;
split(img_hsv, channels);

And then save them one by one with a name of your choice:

imwrite("H.png", channels[0]);
imwrite("S.png", channels[1]);
imwrite("V.png", channels[2]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM