具有QBarSeries的RGB直方图

时间:2018-11-16 15:53:54

标签: qt qchart

我遵循此Example来绘制带有Qt的RGB直方图。我有三个不同的QBarSet,每个颜色通道一个。

我现在的问题是所有条形图都相邻绘制。可以以某种方式在彼此前面画条吗?


这就是现在的样子: enter image description here

这就是我想要的: Link

由于在我的示例中所有条形图都彼此相邻绘制,因此它们太窄了。

1 个答案:

答案 0 :(得分:0)

我通过QStackedBarSeries和一些手工计算解决了我的问题。

我的代码:

// Histogramms
std::vector<int> nGrayValues(NOGRAYLEVELS, 0);
std::vector<int> nRedValues(NOGRAYLEVELS, 0);
std::vector<int> nGreenValues(NOGRAYLEVELS, 0);
std::vector<int> nBlueValues(NOGRAYLEVELS, 0);

QRgb* imPixels= reinterpret_cast< QRgb* >(image.bits());
for (quint64 i = 0; i < numberPixels; i++) {
    nGrayValues[static_cast<unsigned int>(qGray(imPixels[i]))]++;
    nRedValues[static_cast<unsigned int>(qRed(imPixels[i]))]++;
    nGreenValues[static_cast<unsigned int>(qGreen(imPixels[i]))]++;
    nBlueValues[static_cast<unsigned int>(qBlue(imPixels[i]))]++;
}

QStackedBarSeries* pSeries = new QStackedBarSeries();

QBarSet* grayBarSet = new QBarSet("Gray");
grayBarSet->setColor(Qt::black);
QBarSet* redBarSet = new QBarSet("Red");
redBarSet->setColor(Qt::red);
QBarSet* greenBarSet = new QBarSet("Green");
greenBarSet->setColor(Qt::green);
QBarSet* blueBarSet = new QBarSet("Blue");
blueBarSet->setColor(Qt::blue);

double totalMaxValue = 0.0;
if (numberPixels != 0) {
    double numberPixels_f = static_cast<double>(numberPixels);
    for (unsigned int i = 0; i < NOGRAYLEVELS; i++) {

        // Helper function
        auto calcPart = [=](int value, int& maxValue) -> int 
                        {
                            int temp = value - maxValue;
                            if (temp > 0) {
                                maxValue = value;
                                return temp;
                            } else {
                                return 0;
                            }
                         };

        int grayPart = 0, redPart = 0, greenPart = 0, bluePart = 0, maxPart = 0;
        grayPart = calcPart(nGrayValues[i], maxPart);
        redPart = calcPart(nRedValues[i], maxPart);
        greenPart = calcPart(nGreenValues[i], maxPart);
        bluePart = calcPart(nBlueValues[i], maxPart);

        // normalize histogramm entries as procent
        *grayBarSet << grayPart * 100.0 / numberPixels_f;
        *redBarSet << redPart * 100.0 / numberPixels_f;
        *greenBarSet << greenPart * 100.0 / numberPixels_f;
        *blueBarSet << bluePart * 100.0 / numberPixels_f;

        double m1 = std::max(gray, red);
        double m2 = std::max(green, blue);
        double maxValue = std::max(m1, m2);
        if (maxValue > totalMaxValue) {
            totalMaxValue = maxValue;
        }
    }
}

pSeries->append(grayBarSet);
pSeries->append(redBarSet);
pSeries->append(greenBarSet);
pSeries->append(blueBarSet);

// add series to the chart and update axisY
pChart->removeAllSeries();
axisY->setRange(0, totalMaxValue);
pChart->addSeries(pSeries);

以固定顺序绘制各个颜色(灰色->红色->绿色->蓝色)。


这是结果:Stacked Histogram