HSV值是否存储为numpy数组中的VSH?

时间:2016-08-27 20:06:20

标签: python opencv numpy colors

由于RGB存储为BGR,在HSV的情况下会发生同样的情况吗?

我正在制作一个项目,我从摄像头获取输入并将其转换为HSV颜色,以跟踪特定颜色的对象。

2 个答案:

答案 0 :(得分:1)

没有。这是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;
}

Red Image

<强>输出: -

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

输出说明  使用此tool,RGB值(237,28,36)映射到HSV(358,88.2,92.9)。由于HUE的范围为0到359,因此该值跨越仅允许256个值的1字节边界。在opencv中,HUE除以2到[0,179]的范围,以使用更少的内存。色调值358除以2映射到179,这是第一个通道。此外,饱和度和值仅标准化为0-255。因此,您可以看到饱和度映射到第二个,而值映射到第三个通道。

答案 1 :(得分:0)

不。但是有些功能适用于RGB值。并以HSV格式存储在numpy数组中。 要查看此内容,请转到Opencv文档Opencv Documentation Color Conversion