// Creates an image from a 2D Histogram (x axis = Hue, y axis = Saturation) void DrawHSHistogram(CvHistogram hist) { // Get the maximum and minimum values from the histogram float minValue, maxValue; hist.GetMinMaxValue(out minValue, out maxValue); int xBins = hist.Bins.GetDimSize(0); // Number of hue bins (x axis) int yBins = hist.Bins.GetDimSize(1); // Number of saturation bins (y axis) // Create an image to visualize the histogram int scaleHeight = 5, scaleWidth = 5; CvMat hist_img = new CvMat(yBins * scaleHeight, xBins * scaleWidth, TriColorMatrix); hist_img.Zero(); // Set all the pixels to black double binVal; int _intensity; for (int h = 0; h < xBins; h++) { for (int s = 0; s < yBins; s++) { binVal = Cv.QueryHistValue_2D(hist, h, s); _intensity = Cv.Round(binVal / maxValue * 255); // 0 to 255 // Draw a rectangle (h, s) to (h+1, s+1) (scaled by window size) // The pixel value is the color of the histogram value at bin (h, s) hist_img.Rectangle(Cv.Point(h * scaleWidth, s * scaleHeight), Cv.Point((h + 1) * scaleWidth - 1, (s + 1) * scaleHeight - 1), Cv.RGB(_intensity, _intensity, _intensity), Cv.FILLED); } } Cv.ShowImage("HS Histogram", hist_img); }
// Creates an image from a 1D Histogram void Draw1DHistogram(CvMat _image) { float channelMax = 255; CvHistogram hist1 = CalculateOneChannelHistogram(_image, 0, channelMax); CvHistogram hist2 = CalculateOneChannelHistogram(_image, 1, channelMax); CvHistogram hist3 = CalculateOneChannelHistogram(_image, 2, channelMax); // Get the maximum and minimum values from the histogram float minValue, maxValue; hist1.GetMinMaxValue(out minValue, out maxValue); int hBins = hist1.Bins.GetDimSize(0); // Number of bins // Create an image to visualize the histogram int scaleWidth = 3, scaleHeight = 1; int histWidth = hBins * imColorChannels * scaleWidth, histHeight = Mathf.FloorToInt(channelMax * scaleHeight); CvMat hist_img = new CvMat(histHeight, histWidth, TriColorMatrix); hist_img.Zero(); // Set all the pixels to black double binVal; int _intensity; for (int h = 0; h < hBins; h++) { // Draw Channel 1 binVal = Cv.QueryHistValue_1D(hist1, h); _intensity = Cv.Round(binVal / maxValue * channelMax) * scaleHeight; // 0 to channelMax // Draw a rectangle (h, s) to (h+1, s+1) (scaled by window size) // The pixel value is the color of the histogram value at bin (h, s) hist_img.Rectangle(Cv.Point(h * imColorChannels * scaleWidth, histHeight), Cv.Point(h * imColorChannels * scaleWidth + 1, histHeight - _intensity), CvColor.Red, Cv.FILLED); // Draw Channel 2 binVal = Cv.QueryHistValue_1D(hist2, h); _intensity = Cv.Round(binVal / maxValue * channelMax) * scaleHeight; // 0 to channelMax // Draw a rectangle (h, s) to (h+1, s+1) (scaled by window size) // The pixel value is the color of the histogram value at bin (h, s) hist_img.Rectangle(Cv.Point(h * imColorChannels * scaleWidth + 2, histHeight * scaleHeight), Cv.Point(h * imColorChannels * scaleWidth + 3, histHeight * scaleHeight - _intensity), CvColor.Blue, Cv.FILLED); // Draw Channel 3 binVal = Cv.QueryHistValue_1D(hist3, h); _intensity = Cv.Round(binVal / maxValue * channelMax) * scaleHeight; // 0 to channelMax // Draw a rectangle (h, s) to (h+1, s+1) (scaled by window size) // The pixel value is the color of the histogram value at bin (h, s) hist_img.Rectangle(Cv.Point(h * imColorChannels * scaleWidth + 4, histHeight * scaleHeight), Cv.Point(h * imColorChannels * scaleWidth + 5, histHeight * scaleHeight - _intensity), CvColor.Green, Cv.FILLED); } Cv.ShowImage("Histogram", hist_img); }