将RGB图像转换为3个HSI输出

时间:2017-10-24 10:38:02

标签: c++ visual-studio opencv rgb

我试图将RGB图像转换为HSI,它有三种不同的输出;色调,饱和度和强度。

这是我到目前为止所做的:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;

int main() {
    //This line of code interts the picture of a cat 

    float r, g, b, h, s, in;

    Mat image;
    image = imread("C:/Users/pjusk/Desktop/kitti.jpg");

    if (image.data && !image.empty()) {
        imshow("Hello world!", image);

        Mat h1(image.rows, image.cols, image.type());
        Mat s1(image.rows, image.cols, image.type());
        Mat i1(image.rows, image.cols, image.type());

        float r, g, b, h, s, in;

        for (int i = 0; i < image.rows; i++)
        {
            for (int j = 0; j < image.cols; j++)
            {
                b = image.at<Vec3b>(i, j)[0];
                g = image.at<Vec3b>(i, j)[1];
                r = image.at<Vec3b>(i, j)[2];

                in = (b + g + r) / 3;

                float min_val = 0;
                min_val = std::min(r, std::min(b, g));

                s = 1 - 3 * (min_val / (b + g + r));
                if (s < 0.00001)
                {
                    s = 0;
                }
                else if (s > 0.99999) {
                    s = 1;
                }

                if (s != 0)
                {
                    h = 0.5 * ((r - g) + (r - b)) / sqrt(((r - g)*(r - g)) + ((r - b)*(g - b)));
                    h = acos(h);

                    if (b <= g)
                    {
                        h = h;
                    }
                    else {
                        h = ((360 * 3.14159265) / 180.0) - h;
                    }
                }


                h1.at<Vec3b>(i, j)[2] = (h * 180) / 3.14159265;
                h1.at<Vec3b>(i, j)[1] = s * 100;
                h1.at<Vec3b>(i, j)[0] = in;

                s1.at<Vec3b>(i, j)[2] = (h * 180) / 3.14159265;
                s1.at<Vec3b>(i, j)[1] = s * 100;
                s1.at<Vec3b>(i, j)[0] = in;

                i1.at<Vec3b>(i, j)[2] = (h * 180) / 3.14159265;
                i1.at<Vec3b>(i, j)[1] = s * 100;
                i1.at<Vec3b>(i, j)[0] = in;
            }
        }


        imshow("h1 image", h1);
        imshow("s1 image", s1);
        imshow("i1 image", i1);


        waitKey(0);
        return 0;
    }
}

我希望你们能帮助我! 目前输出的是4个图像,RGB一个和三个HSI图像,显然没有分割成H,S和I值。

2 个答案:

答案 0 :(得分:1)

通常的术语是频道。 RGB图像有3个通道,红色,绿色和蓝色(实际上是正确记录的B,G,R)。 HSI图像有3个通道,但通道不同。但是你不是用3个频道创建一个HSI图像,而是用3个频道创建3个图像。他们显然是完全相同的。

imshow(i1)如何知道这3个频道应该形成HSI图像?数字只是数字,RGB或HSI是对数字的解释。 imshow会将第一个频道(H)显示为蓝色等

答案 1 :(得分:0)

您需要修改以下内容:

//The h, s and i images should be of type CV_8UC1
Mat h1(image.rows, image.cols, CV_8UC1);
Mat s1(image.rows, image.cols, CV_8UC1);
Mat i1(image.rows, image.cols, CV_8UC1);

//Since h1 is a one channel image, s and l should not be added
h1.at<uchar>(i, j) = (h * 180) / 3.14159265;
//h1.at<Vec3b>(i, j)[1] = s * 100;
//h1.at<Vec3b>(i, j)[0] = in;

//Since s1 is a one channel image, h and l should not be added
//s1.at<Vec3b>(i, j)[2] = (h * 180) / 3.14159265;
s1.at<uchar>(i, j) = s * 100;
//s1.at<Vec3b>(i, j)[0] = in;

//Since i1 is a one channel image, h and s should not be added
//i1.at<Vec3b>(i, j)[2] = (h * 180) / 3.14159265;
//i1.at<Vec3b>(i, j)[1] = s * 100;
i1.at<uchar>(i, j) = in;