pythonopencvhsvbgr

How to create a Mat image in OpenCV if i have the values of h, s and v?


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++.


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