Question

I used mathematical calculations to convert the r,g,b values of a pixel into the h,s and v values. How do i use these h, s, and v values to create an image and be able to show them using imshow("HSV", hsv_image).

It would be better if the answer is using python, however it is ok even if it is C++.

Was it helpful?

Solution

if you have a RGB image then just do

cvtColor(img_rgb,img_hsv,CV_RGB2HSV);

is this what you want?

EDIT

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];
    }
}

OTHER TIPS

What you want is merge(). In 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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top