[英]How to count the pixels in an image using Qimage
到目前为止,这是我的代码,对此我毫无用处,并且似乎无法在网络上找到太多帮助。 我想从图像中计数值为0xff7a2080的像素,然后在将其保存为特定文件名之前显示main中的数量。 任何帮助将非常感激。
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();
}
我想到的最简单的解决方案(适应您的风格)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.