如何使用 OpenCV 在 C++ 中将 RGB 图像转换为 HSV 并将 H、S 和 V 值保存到 3 个单独的图像中?

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?

我有一张 RBG 图片,我想将其转换为 HSV。我想将 H、S 和 V 值保存到 3 个单独的图像中。我该怎么做?

我当前的代码只是将 RGB 图像转换为 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);

您需要使用 COLOR_BGR2HSV 而不是 COLORMAP_HSV,因为您正在从 BGR 转换为 HSV(OpenCV 默认使用 BGR)。之后,您可以将图像拆分为其通道:

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

然后用您选择的名称将它们一一保存:

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