使用OpenCVsharp为RGB图像的红色,绿色和蓝色通道生成单独的直方图

时间:2020-04-11 10:54:50

标签: python visual-studio image-processing histogram opencvsharp

生成红色,绿色和蓝色通道的三个直方图,分别从我们选择的输入图像中获取。

源代码应仅使用OpenCVSharp库。

下面列出了用于将原稿转换为灰度并绘制灰度图像的直方图的示例代码。同样,我需要将原始图像更改为红色,绿色和蓝色直方图。

    public void convertGray()
    {
        grayImage = Cv.CreateImage(src.Size, BitDepth.U8, 1);
        Cv.CvtColor(src, grayImage, ColorConversion.RgbToGray);
        Cv.SaveImage("grayImage.jpg", grayImage);

    }


    //Draw Histogram
    public void drawHistogram()
    {
        float[] range = { 0, 255 };
        float[][] ranges = { range };

        int histSize = 255;
        float minValue = 0;
        float maxValue = 0;

        convertGray();
        IplImage histImage = Cv.CreateImage(src.Size, BitDepth.U8, 1);
        CvHistogram hist = Cv.CreateHist(new int[] { histSize }, HistogramFormat.Array, ranges, true);

        Cv.CalcHist(grayImage, hist);

        Cv.GetMinMaxHistValue(hist, out minValue, out maxValue);

        histImage.Set(CvColor.White);

        Cv.Scale(hist.Bins, hist.Bins, ((double)histImage.Height) / maxValue, 0);

        int binW = Cv.Round((double)histImage.Width / histSize);

        int i;
        for (i = 0; i < histSize; i++)
        {
            histImage.Rectangle(
                new CvPoint(i * binW, grayImage.Height),
                new CvPoint((i + 1) * binW, grayImage.Height - Cv.Round(hist.Bins[i])),
                CvColor.Black, 1, LineType.Link8, 0);


        }
        Cv.SaveImage("HistogramImage.jpg", histImage);

    }

}

这是输出: enter image description here

0 个答案:

没有答案