如何使用Qimage计算图像中的像素

时间:2016-04-13 15:36:14

标签: c++

这是我的代码到目前为止,我在这方面相当无用,似乎无法在网上找到太多帮助。 我想从图像中计算值0xff7a2080的像素,然后在将其保存为特定文件名之前显示主数量。任何帮助将非常感激。

unsigned TotalPixels (int width, int height)
{

 int pixelcount;
 int QImage:: width (QCoreApplication) const;
 int QImage:: height (QCoreApplication) const;

 {
  pixelcount = width * height;
  }

  return pixelcount;
}




int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
const char LogoFile[] = "RGUMoodleLogo.png";

unsigned PixelGrid[WIDTH][HEIGHT];     // Image loaded from file

// If the file cannot be loaded ...
if (!loadImage(PixelGrid, LogoFile))
{
    // Display an error message
    cout << "Error loading file \"" << LogoFile << "\"" << endl;
}
else
{
    cout << "File \"" << LogoFile << "\" opened successfully" << endl;

    if (saveImage(PixelGrid, "RGUMoodleLogoCopy.png"))
    {
        cout << "File \"RGUMoodleLogoCopy.png\" saved successfully" << endl;
    }
    else
    {
        cout << "Could not save \"RGUMoodleLogoCopy.png\"" << endl;
    }

}



return a.exec();

}

1 个答案:

答案 0 :(得分:0)

我想到的最简单的解决方案(适应你的风格)

unsigned countPixels(unsigned pixels[][HEIGHT], long value) {
    unsigned count = 0;

    for(long x = 0; x < WIDTH; x++) {
        for(long y = 0; y < HEIGHT; y++) {
            if(pixels[x][y] == value)
                count++;
        }
    }
    return count;
}

用法:std::cout << countPixels(PixelGrid, 0xff7a2080) << std::endl