由于RGB存储为BGR,那么HSV的情况也是如此吗?
我正在制作一个项目,从网络摄像头中输入并将其转换为HSV颜色,以跟踪特定颜色的对象。
由于RGB存储为BGR,那么HSV的情况也是如此吗?
我正在制作一个项目,从网络摄像头中输入并将其转换为HSV颜色,以跟踪特定颜色的对象。
int main()
{
// Load your Red colored image
cv::Mat frame = imread("test.png");
// Split each channel
cv::Mat rgbChannels[3];
cv::split(frame, rgbChannels);
cv::imshow("RGB", frame);
// Check value of your Red, Blue and Green Channel
double minVal, maxVal;
// Note: Blue is first channel
cv::minMaxLoc(rgbChannels[0], &minVal, &maxVal);
std::cout << "Blue: Min = " << minVal << ", Max = " << maxVal << std::endl;
cv::minMaxLoc(rgbChannels[1], &minVal, &maxVal);
std::cout << "Green: Min = " << minVal << ", Max = " << maxVal << std::endl;
cv::minMaxLoc(rgbChannels[2], &minVal, &maxVal);
std::cout << "Red: Min = " << minVal << ", Max = " << maxVal << std::endl;
std::cout << "*******************************" << std::endl;
cv::Mat hsv;
cv::Mat hsvChannels[3];
// Convert BGR image to HSV. Dont use CV_RGB2HSV.
cv::cvtColor(frame, hsv, CV_BGR2HSV);
// Split each channel
cv::split(hsv, hsvChannels);
// **Display HSV image: Note: When displaying opencv does not display image as Red image**
// This is because imshow will just take first channel which is hue and treat it as Blue, second channel as
// Green, and last channel as Red.
cv::imshow("HSV", hsv);
cv::minMaxLoc(hsvChannels[0], &minVal, &maxVal);
std::cout << "Hue: Min = " << minVal << ", Max = " << maxVal << std::endl;
cv::minMaxLoc(hsvChannels[1], &minVal, &maxVal);
std::cout << "Saturation: Min = " << minVal << ", Max = " << maxVal << std::endl;
cv::minMaxLoc(hsvChannels[2], &minVal, &maxVal);
std::cout << "Value: Min = " << minVal << ", Max = " << maxVal << std::endl;
waitKey(0);
return 0;
}
输出:-
Blue: Min = 36, Max = 36
Green: Min = 28, Max = 28
Red: Min = 237, Max = 237
*******************************
Hue: Min = 179, Max = 179
Saturation: Min = 225, Max = 225
Value: Min = 237, Max = 237
输出解释
使用这个工具,RGB值(237, 28, 36)映射到HSV(358,88.2,92.9)。由于色调范围从0到359,该值超过了1字节的边界,只允许256个值。色调除以2后,范围为[0,179],以在opencv中使用更少的内存。色调值358除以2映射到179,是第一个通道。此外,饱和度和值仅被归一化为0-255的比例。因此,如您所见,饱和度映射到第二个通道,值映射到第三个通道。没有,但是有一种适用于RGB值的函数,它以HSV格式存储在numpy数组中。 请前往OpenCV文档Opencv Documentation Color Conversion查看。