【问题标题】:Trying to build a function to count unique pixels in image试图构建一个函数来计算图像中的唯一像素
【发布时间】:2014-10-26 11:52:49
【问题描述】:

..这是有用的。但是..我在 i-net 上看不到任何相关内容..这似乎有点问题。这就是为什么..我尝试自己构建功能:

typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned long dword;

typedef struct
{
    byte R;
    byte G;
    byte B;
} RGB;

dword
getpixels
(char *FILE_NAME)
{
    dword WIDTH = 500; // example dimension
    dword HEIGHT = 500; // example dimension
    FILE* fp = fopen(FILE_NAME, "rb");
    #define HEADERS_SIZE 54

    byte color[3];
    byte colorTable[50000][3]; // maximum 50000 pixels
    int val = (-1), valr;

    dword l;
    dword count = 0;

    fseek(fp, HEADERS_SIZE, SEEK_SET); // move iterator to where the pixels start fromS

    // alternate : fread(&valr, 1, 1, fp) == 1
    while( (valr = fgetc(fp)) != EOF ) // runs the code while this is true
    {
        val++; // increment index
        if(val > 2) val = 0;
        color[val] = valr;

        for(l=0; l<50000; l++) {
        if(val == 2 && color[0] != colorTable[l][0] && color[1] != colorTable[l][1] && color[2] != colorTable[l][2])
        {
            colorTable[l][0] = color[0];
            colorTable[l][1] = color[1];
            colorTable[l][2] = color[2];
            count++;
        }
        }

        fseek(fp, WIDTH%4, SEEK_CUR); // skip padding
    }
    fclose(fp);
    return count;
}

您可能已经感觉到.. 该功能无法正常工作,因为.. 我不知道。这就是我真正要问的。我做错了什么?

【问题讨论】:

    标签: c colors pixels bmp


    【解决方案1】:

    尝试以下方法(未经测试,后果自负)。我重命名了一些变量,以使代码更加不言自明。

    编辑: 更新后实际上包含了我在第一个版本中错过的颜色计数器(假设这是您从问题中想要的)。请记住,这很慢,因为您只是在颜色表中进行缓慢的线性查找。对于具有 50000 种颜色的 500x500 图像,您可能会进行 100 亿次循环。

    int numColors = 0;
    int colorIndex = 0;
    int inputColor;
    int colorCounts[50000];  //Be aware of possible overflow
    
    memset(colorCounts, 0, sizeof(int)*50000);  // Initialize counters
    
    while( (inputColor = fgetc(fp)) != EOF ) 
    {
        color[colorIndex] = inputColor;
        colorIndex++;
    
        fseek(fp, WIDTH%4, SEEK_CUR);
    
        if (colorIndex < 3) continue;  // or use an if block
        colorIndex = 0;
    
        for (l = 0; l < numColors; ++l) 
        {
            if (color[0] == colorTable[l][0] && 
                color[1] == colorTable[l][1] && 
                color[2] == colorTable[l][2])
            { 
                ++colorCounts[l]; // Update counter
                break;
            }
        }
    
        if (l >= numColors)
        {
           if (numColors >= 50000) exit(1);  //Or do something else appropriate
           colorTable[numColors][0] = color[0];
           colorTable[numColors][1] = color[1];
           colorTable[numColors][2] = color[2];
    
           colorCounts[l] = 1;   //Initialize counter
    
            ++numColors;
        }
    
    }
    

    编辑 2: 打印颜色表的代码以帮助您调试(添加到函数末尾以显示找到的颜色):

    for (l = 0; l < numColors; ++l)
    {
        printf("%d) %02X %02X %02X\n", l, colorTable[l][0], colorTable[l][1], colorTable[l][2]);
    }
    

    【讨论】:

    • 我把我的代码留在了处理中。我回来后喜欢.. 1 小时,我意识到它返回 399000。所以嗯.. 我的代码工作但非常慢?
    • 嗯,它在全白图片上返回 4。不幸的是,它不起作用:(
    • 已更新以包含我在第一个版本中遗漏的实际计数代码...假设这就是您要查找的内容。
    • 我的额外计数代码似乎没有必要(如果不需要,请删除countCounts 数组),但我测试了计数功能,它似乎工作正常。也许问题是您的文件输入代码或文件内容?
    • 您的 BMP 加载代码一定有问题。它对我来说很好,有几个测试内存缓冲区,运行时间少于 15 毫秒(在调试版本中不少于)。
    猜你喜欢
    • 2011-12-05
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 2021-02-23
    • 1970-01-01
    相关资源
    最近更新 更多