如果我有h,s和v的值,如何在OpenCV中创建Mat图像?

时间:2013-01-18 06:59:12

标签: python opencv hsv bgr

我使用数学计算将像素的r,g,b值转换为h,s和v值。如何使用这些h,s和v值创建图像并能够使用imshow(“HSV”,hsv_image)显示它们。

如果答案是使用python会更好,但即使它是C ++也没关系。

2 个答案:

答案 0 :(得分:0)

如果您有RGB图像,那么只需

cvtColor(img_rgb,img_hsv,CV_RGB2HSV);

这就是你想要的吗?

修改

for(int row=0;row<height;row++)
{
    for(int col=0;col<width;col++)
    {
        Vec3b data = image_rgb.at<Vec3b>(row,col);

        Vec3b data_hsv;
        data_hsv[0] = // process red channel of pixel with data[0]
        data_hsv[1] = // process green channel of pixel with data[1]
        data_hsv[2] = // process blue channel of pixel with data[2]


        image_hsv.at<Vec3b>(row,col)[0] = data_hsv[0];
        image_hsv.at<Vec3b>(row,col)[1] = data_hsv[1];
        image_hsv.at<Vec3b>(row,col)[2] = data_hsv[2];
    }
}

答案 1 :(得分:0)

你想要的是merge()。在C ++中:

// Assuming you have the H, S, and V images
std::vector<cv::Mat> channels;
channels.push_back(h);
channels.push_back(s);
channels.push_back(v);

cv::Mat hsv;
cv::merge(channels, hsv);

// Now you can display it
cv::imshow("HSV", hsv);