[英]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.