反转位图图像会使图像变黑。但不一致

时间:2014-09-30 16:16:50

标签: c matlab image-processing bitmapimage

我试图用一些用C编写的代码来否定一个位图图像。

  1. 当我第一次获得黑色图像(LenaImage2)时,我运行Lena图像的代码(让我称之为LenaImage1)。当我在图像上运行相同的代码时,我返回原始图像(LenaImage1)。这意味着代码正常工作正确吗? 当我使用MATLAB查看并保存(作为位图)LenaImage2时,我没有得到黑色图像,这意味着我可以将其视为被称赞的图像(MATLAB返回相同的输出)。

  2. 当我在第二个图像(MountainImage1)上运行相同的C代码时,我得到一个非常糟糕的图像(MounainImage2),有很多伪着色但不是黑色图像作为输出。当我运行代码来补充MATLAB中的MounainImage1时,我得到的是没有伪着色的正确图像。当我在MountainImage2上运行C代码时,我会返回原始图像(即MountainImage1)。

  3. 在第三张图像上运行代码会显示与Lena图像类似的行为。所有提到的图片都可以找到here

    另一个answer提到位图图像不支持透明度。

    这是我在这里看到的透明度问题吗? 如果是,为什么我在所有图片中看不到此透明度问题的一致行为? 另外,当我使用MATLAB查看和保存LenaImage2以及MountainImage2并且它不显示黑色图像或任何伪着色时怎么样? 我该如何更正我的C代码?

    以下是反转图像的代码:

    我的头文件中的文件头声明:

                #pragma pack(push,1)
                typedef struct bmpFileHeader
                {
                    unsigned short bfType;       /*specifies the file type*/
                    unsigned long bfSize;        /*specifies the size in bytes of the bitmap file*/
                    unsigned short bfReserved1;  /*reserved; must be 0*/
                    unsigned short bfReserved2;  /*reserved; must be 0*/
                    unsigned long bfOffBits;     /*species the offset in bytes from the bitmapfileheader to the bitmap bits*/
                }bmpFileHeader;
    
                typedef struct bmpInfoHeader
                {
                    unsigned long biSize;           /*Size of bmpInfoHeader*/
                    unsigned long biWidth;          /*specifies width in pixels*/
                    unsigned long biHeight;         /*species height in pixels*/
                    unsigned short biPlanes;        /*specifies the number of color planes, must be 1*/
                    unsigned short biBitCount;      /*specifies the number of bit per pixel*/
                    unsigned long biCompression;    /*spcifies the type of compression*/
                    unsigned long biSizeImage;      /*size of image in bytes*/
                    unsigned long biXPelsPerMeter;  /*number of pixels per meter in x axis*/
                    unsigned long biYPelsPerMeter;  /*number of pixels per meter in y axis*/
                    unsigned long biClrUsed;        /*number of colors used by the bitmap*/
                    unsigned long biClrImportant;   /*number of colors that are important*/
                }bmpInfoHeader;
                #pragma pack(pop)
    

    完成工作的代码:

                void ImageData(FILE *filePtr)
                {
                    bmpFileHeader FileHeader;
                    bmpInfoHeader InfoHeader;
                    char *bmpImageData;    /*array to hold the image data*/
                    size_t imagedataSize=0,check=0;
                    int y;
    
    
                    /*read the bitmap file header*/
                    check=fread(&FileHeader, sizeof(bmpFileHeader),1,filePtr);
                    printf("checkFileHeader=%d\n",check);
    
                    /*verify that this is a bmp file by check bitmap id*/
                    if (FileHeader.bfType !=0x4D42)
                    {
                        fclose(filePtr);
                        printf("ImageData:Is not a BMP file\n");
                        return;
                    }
    
                    /*read the bitmap info header*/
                    check=fread(&InfoHeader, sizeof(InfoHeader),1,filePtr);
                    printf("checkInfoHeader=%d\n",check);
    
                    /*Allocate space for the image data*/
    
                    bmpImageData=(char*)malloc(InfoHeader.biSizeImage);
                    if(bmpImageData==NULL){
                        printf("Couldn't allocate memory for ImageData\n");
                        return;
                    }
                    /*move the file position to the point where the aligned image data begins*/
                    check=fseek(filePtr,FileHeader.bfOffBits,SEEK_SET);
                    if(check!=0){
                        printf("Error seeking file\n");
                        return;
                    }
                    /*Copy the image data to bmpImageData*/
                    check=fread(bmpImageData,InfoHeader.biSizeImage,1,filePtr);
                    if(check!=1){
                        printf("Error reading image data\n");
                        return
                    }
    
                    /*Moving filePtr to position where image data begins*/
                    check=fseek(filePtr,FileHeader.bfOffBits,SEEK_SET);
                    if(check!=0){
                        printf("Error seeking file\n");
                        return;
                    }
                    /*Manipulating the image data*/
                    for(y=0;y<InfoHeader.biSizeImage;y++){
                        bmpImageData[y]=255-bmpImageData[y];
                    }
                    check=fwrite(bmpImageData,InfoHeader.biSizeImage,1,filePtr);
                    if(check!=1)
                        printf("Error writing image data to file");
    
                    free(bmpImageData);
    
                }
    

    我使用的所有图像都是具有8位像素深度的灰度图像。我在x86机器上使用CodeBlocks IDE(GNU GCC编译器) 运行Windows 8.

    增加: 这就是Lena图像的标题字段:

                File Header Information
                FileHeaderSize=14
                bfType=4d42
                bfSize=263222
                bfReserved1=0
                bfReserved2=0
                bfOffBits=1078
    
                Info Header Information
                InfoHeaderSize=40
                biSize=40
                biHeight=512
                biWidth=512
                biPlanes=1
                biBitCount=8
                biCompression=0
                biSizeImage=262144
                biXPelsPerMeter=0
                biYPelsPerMeter=0
                biClrUsed=256
                biClrImportant=256
    

1 个答案:

答案 0 :(得分:3)

bitcount = 8,这意味着必须使用颜色表(调色板) - 请参阅here。这意味着像素数组中的值(即您的变量bmpImageData)不是颜色,而是实际索引到调色板中。因此,您无法明智地直接修改bmpImageData

因此,如果像素[0,0]包含,例如3,则必须在调色板中查找第3个条目以找出对应的颜色。

调色板应位于BITMAPINFOHEADER和DIB之后,因此它来自字节55-435。

我建议你用十六进制打印出这些字节并查看它们。我猜你会看到它们是RGBA,RGBA,RGBA,但A(alpha或透明度)将为零,即未使用。所以我希望它们看起来像这样,R = G = B(因为它们是灰度):

xx xx xx 00 yy yy yy 00 zz zz zz 00

然后,为了反转您的图片,请在调色板中将每个xx更改为255-xx(以及同样yz),而无需处理所有完全bmpImageData

我的答案中存在一些不一致之处,但这是因为我在尝试调试JPEG文件时只能看到BMP文件。一个不一致的是DIB和像素阵列之间的空间是436-54字节,即382字节不能被3或4整除,因此使用3或4字节不能产生非常合理的调色板大小每个条目...但抓取十六进制数据,我们将看一看,看看我们是否能够更接近正确的答案。

如果您使用ImageMagick(可从here免费获得Unix / Linux和Windows),您可以使用identify命令帮助您调试代码:

  identify -verbose lena.bmp

  Format: BMP (Microsoft Windows bitmap image)
  Class: PseudoClass
  Geometry: 512x512+0+0
  Units: PixelsPerCentimeter
  Type: Grayscale
  Base type: Grayscale
  Endianess: Undefined
  Colorspace: Gray
  Depth: 8-bit
  Channel depth:
    gray: 8-bit
  Channel statistics:
    Pixels: 262144
    Gray:
      min: 28 (0.109804)
      max: 244 (0.956863)
      mean: 124.071 (0.486554)
      standard deviation: 47.9288 (0.187956)
      kurtosis: -0.839484
      skewness: -0.0822738
  Colors: 28
  Histogram:
       474: ( 28, 28, 28) #1C1C1C gray(28)
      4477: ( 36, 36, 36) #242424 gray(36)
     13395: ( 44, 44, 44) #2C2C2C gray(44)
     15131: ( 52, 52, 52) #343434 gray(52)
      9461: ( 60, 60, 60) #3C3C3C gray(60)
      6569: ( 68, 68, 68) #444444 gray(68)
      7027: ( 76, 76, 76) #4C4C4C gray(76)
      7733: ( 84, 84, 84) #545454 gray(84)
     10330: ( 92, 92, 92) #5C5C5C gray(92)
     14865: (100,100,100) #646464 gray(100)
     12095: (108,108,108) #6C6C6C gray(108)
     11631: (116,116,116) #747474 gray(116)
     16153: (124,124,124) #7C7C7C gray(124)
     17429: (132,132,132) #848484 gray(132)
     17623: (140,140,140) #8C8C8C gray(140)
     18636: (148,148,148) #949494 gray(148)
     19688: (156,156,156) #9C9C9C gray(156)
     12554: (164,164,164) #A4A4A4 gray(164)
      9848: (172,172,172) #ACACAC gray(172)
      7380: (180,180,180) #B4B4B4 gray(180)
      5589: (188,188,188) #BCBCBC gray(188)
      7162: (196,196,196) #C4C4C4 gray(196)
      7451: (204,204,204) #CCCCCC gray(204)
      6699: (212,212,212) #D4D4D4 gray(212)
      2293: (220,220,220) #DCDCDC gray(220)
       425: (228,228,228) #E4E4E4 gray(228)
        22: (236,236,236) #ECECEC gray(236)
         4: (244,244,244) #F4F4F4 gray(244)
  Colormap entries: 256
  Colormap:
         0: (  0,  0,  0) #000000 gray(0)
         1: (128,  0,  0) #800000 gray(128)
         2: (  0,128,  0) #008000 gray(0)
         3: (128,128,  0) #808000 gray(128)
         4: (  0,  0,128) #000080 gray(0)
         5: (128,  0,128) #800080 gray(128)
         6: (  0,128,128) #008080 gray(0)
         7: (128,128,128) #808080 gray(128)
         8: ( 28, 28, 28) #1C1C1C gray(28)
         9: (140,140,140) #8C8C8C gray(140)
        10: ( 84, 84, 84) #545454 gray(84)
        11: (196,196,196) #C4C4C4 gray(196)
        12: ( 60, 60, 60) #3C3C3C gray(60)
        13: (172,172,172) #ACACAC gray(172)
        14: (116,116,116) #747474 gray(116)
        15: (228,228,228) #E4E4E4 gray(228)
        16: ( 44, 44, 44) #2C2C2C gray(44)
        17: (156,156,156) #9C9C9C gray(156)
        18: (100,100,100) #646464 gray(100)
        19: (212,212,212) #D4D4D4 gray(212)
        20: ( 76, 76, 76) #4C4C4C gray(76)
        21: (188,188,188) #BCBCBC gray(188)
        22: (132,132,132) #848484 gray(132)
        23: (244,244,244) #F4F4F4 gray(244)
        24: ( 36, 36, 36) #242424 gray(36)
        25: (148,148,148) #949494 gray(148)
        26: ( 92, 92, 92) #5C5C5C gray(92)
        27: (204,204,204) #CCCCCC gray(204)
        28: ( 68, 68, 68) #444444 gray(68)
        29: (180,180,180) #B4B4B4 gray(180)
        30: (124,124,124) #7C7C7C gray(124)
        31: (236,236,236) #ECECEC gray(236)
        32: ( 52, 52, 52) #343434 gray(52)
        33: (164,164,164) #A4A4A4 gray(164)
        34: (108,108,108) #6C6C6C gray(108)
        35: (220,220,220) #DCDCDC gray(220)
        36: (  0,  0,  0) #000000 gray(0)
        37: (  0,  0,  0) #000000 gray(0)
        38: (  0,  0,  0) #000000 gray(0)
        39: (  0,  0,  0) #000000 gray(0)
        40: (  0,  0,  0) #000000 gray(0)
        41: (  0,  0,  0) #000000 gray(0)
        42: (  0,  0,  0) #000000 gray(0)
        43: (  0,  0,  0) #000000 gray(0)
        44: (  0,  0,  0) #000000 gray(0)
        45: (  0,  0,  0) #000000 gray(0)
        46: (  0,  0,  0) #000000 gray(0)
        47: (  0,  0,  0) #000000 gray(0)
        48: (  0,  0,  0) #000000 gray(0)
        49: (  0,  0,  0) #000000 gray(0)
        50: (  0,  0,  0) #000000 gray(0)
        51: (  0,  0,  0) #000000 gray(0)
        52: (  0,  0,  0) #000000 gray(0)
        53: (  0,  0,  0) #000000 gray(0)
        54: (  0,  0,  0) #000000 gray(0)
        55: (  0,  0,  0) #000000 gray(0)
        56: (  0,  0,  0) #000000 gray(0)
        57: (  0,  0,  0) #000000 gray(0)
        58: (  0,  0,  0) #000000 gray(0)
        59: (  0,  0,  0) #000000 gray(0)
        60: (  0,  0,  0) #000000 gray(0)
        61: (  0,  0,  0) #000000 gray(0)
        62: (  0,  0,  0) #000000 gray(0)
        63: (  0,  0,  0) #000000 gray(0)
        64: (  0,  0,  0) #000000 gray(0)
        65: (  0,  0,  0) #000000 gray(0)
        66: (  0,  0,  0) #000000 gray(0)
        67: (  0,  0,  0) #000000 gray(0)
        68: (  0,  0,  0) #000000 gray(0)
        69: (  0,  0,  0) #000000 gray(0)
        70: (  0,  0,  0) #000000 gray(0)
        71: (  0,  0,  0) #000000 gray(0)
        72: (  0,  0,  0) #000000 gray(0)
        73: (  0,  0,  0) #000000 gray(0)
        74: (  0,  0,  0) #000000 gray(0)
        75: (  0,  0,  0) #000000 gray(0)
        76: (  0,  0,  0) #000000 gray(0)
        77: (  0,  0,  0) #000000 gray(0)
        78: (  0,  0,  0) #000000 gray(0)
        79: (  0,  0,  0) #000000 gray(0)
        80: (  0,  0,  0) #000000 gray(0)
        81: (  0,  0,  0) #000000 gray(0)
        82: (  0,  0,  0) #000000 gray(0)
        83: (  0,  0,  0) #000000 gray(0)
        84: (  0,  0,  0) #000000 gray(0)
        85: (  0,  0,  0) #000000 gray(0)
        86: (  0,  0,  0) #000000 gray(0)
        87: (  0,  0,  0) #000000 gray(0)
        88: (  0,  0,  0) #000000 gray(0)
        89: (  0,  0,  0) #000000 gray(0)
        90: (  0,  0,  0) #000000 gray(0)
        91: (  0,  0,  0) #000000 gray(0)
        92: (  0,  0,  0) #000000 gray(0)
        93: (  0,  0,  0) #000000 gray(0)
        94: (  0,  0,  0) #000000 gray(0)
        95: (  0,  0,  0) #000000 gray(0)
        96: (  0,  0,  0) #000000 gray(0)
        97: (  0,  0,  0) #000000 gray(0)
        98: (  0,  0,  0) #000000 gray(0)
        99: (  0,  0,  0) #000000 gray(0)
       100: (  0,  0,  0) #000000 gray(0)
       101: (  0,  0,  0) #000000 gray(0)
       102: (  0,  0,  0) #000000 gray(0)
       103: (  0,  0,  0) #000000 gray(0)
       104: (  0,  0,  0) #000000 gray(0)
       105: (  0,  0,  0) #000000 gray(0)
       106: (  0,  0,  0) #000000 gray(0)
       107: (  0,  0,  0) #000000 gray(0)
       108: (  0,  0,  0) #000000 gray(0)
       109: (  0,  0,  0) #000000 gray(0)
       110: (  0,  0,  0) #000000 gray(0)
       111: (  0,  0,  0) #000000 gray(0)
       112: (  0,  0,  0) #000000 gray(0)
       113: (  0,  0,  0) #000000 gray(0)
       114: (  0,  0,  0) #000000 gray(0)
       115: (  0,  0,  0) #000000 gray(0)
       116: (  0,  0,  0) #000000 gray(0)
       117: (  0,  0,  0) #000000 gray(0)
       118: (  0,  0,  0) #000000 gray(0)
       119: (  0,  0,  0) #000000 gray(0)
       120: (  0,  0,  0) #000000 gray(0)
       121: (  0,  0,  0) #000000 gray(0)
       122: (  0,  0,  0) #000000 gray(0)
       123: (  0,  0,  0) #000000 gray(0)
       124: (  0,  0,  0) #000000 gray(0)
       125: (  0,  0,  0) #000000 gray(0)
       126: (  0,  0,  0) #000000 gray(0)
       127: (  0,  0,  0) #000000 gray(0)
       128: (  0,  0,  0) #000000 gray(0)
       129: (  0,  0,  0) #000000 gray(0)
       130: (  0,  0,  0) #000000 gray(0)
       131: (  0,  0,  0) #000000 gray(0)
       132: (  0,  0,  0) #000000 gray(0)
       133: (  0,  0,  0) #000000 gray(0)
       134: (  0,  0,  0) #000000 gray(0)
       135: (  0,  0,  0) #000000 gray(0)
       136: (  0,  0,  0) #000000 gray(0)
       137: (  0,  0,  0) #000000 gray(0)
       138: (  0,  0,  0) #000000 gray(0)
       139: (  0,  0,  0) #000000 gray(0)
       140: (  0,  0,  0) #000000 gray(0)
       141: (  0,  0,  0) #000000 gray(0)
       142: (  0,  0,  0) #000000 gray(0)
       143: (  0,  0,  0) #000000 gray(0)
       144: (  0,  0,  0) #000000 gray(0)
       145: (  0,  0,  0) #000000 gray(0)
       146: (  0,  0,  0) #000000 gray(0)
       147: (  0,  0,  0) #000000 gray(0)
       148: (  0,  0,  0) #000000 gray(0)
       149: (  0,  0,  0) #000000 gray(0)
       150: (  0,  0,  0) #000000 gray(0)
       151: (  0,  0,  0) #000000 gray(0)
       152: (  0,  0,  0) #000000 gray(0)
       153: (  0,  0,  0) #000000 gray(0)
       154: (  0,  0,  0) #000000 gray(0)
       155: (  0,  0,  0) #000000 gray(0)
       156: (  0,  0,  0) #000000 gray(0)
       157: (  0,  0,  0) #000000 gray(0)
       158: (  0,  0,  0) #000000 gray(0)
       159: (  0,  0,  0) #000000 gray(0)
       160: (  0,  0,  0) #000000 gray(0)
       161: (  0,  0,  0) #000000 gray(0)
       162: (  0,  0,  0) #000000 gray(0)
       163: (  0,  0,  0) #000000 gray(0)
       164: (  0,  0,  0) #000000 gray(0)
       165: (  0,  0,  0) #000000 gray(0)
       166: (  0,  0,  0) #000000 gray(0)
       167: (  0,  0,  0) #000000 gray(0)
       168: (  0,  0,  0) #000000 gray(0)
       169: (  0,  0,  0) #000000 gray(0)
       170: (  0,  0,  0) #000000 gray(0)
       171: (  0,  0,  0) #000000 gray(0)
       172: (  0,  0,  0) #000000 gray(0)
       173: (  0,  0,  0) #000000 gray(0)
       174: (  0,  0,  0) #000000 gray(0)
       175: (  0,  0,  0) #000000 gray(0)
       176: (  0,  0,  0) #000000 gray(0)
       177: (  0,  0,  0) #000000 gray(0)
       178: (  0,  0,  0) #000000 gray(0)
       179: (  0,  0,  0) #000000 gray(0)
       180: (  0,  0,  0) #000000 gray(0)
       181: (  0,  0,  0) #000000 gray(0)
       182: (  0,  0,  0) #000000 gray(0)
       183: (  0,  0,  0) #000000 gray(0)
       184: (  0,  0,  0) #000000 gray(0)
       185: (  0,  0,  0) #000000 gray(0)
       186: (  0,  0,  0) #000000 gray(0)
       187: (  0,  0,  0) #000000 gray(0)
       188: (  0,  0,  0) #000000 gray(0)
       189: (  0,  0,  0) #000000 gray(0)
       190: (  0,  0,  0) #000000 gray(0)
       191: (  0,  0,  0) #000000 gray(0)
       192: (  0,  0,  0) #000000 gray(0)
       193: (  0,  0,  0) #000000 gray(0)
       194: (  0,  0,  0) #000000 gray(0)
       195: (  0,  0,  0) #000000 gray(0)
       196: (  0,  0,  0) #000000 gray(0)
       197: (  0,  0,  0) #000000 gray(0)
       198: (  0,  0,  0) #000000 gray(0)
       199: (  0,  0,  0) #000000 gray(0)
       200: (  0,  0,  0) #000000 gray(0)
       201: (  0,  0,  0) #000000 gray(0)
       202: (  0,  0,  0) #000000 gray(0)
       203: (  0,  0,  0) #000000 gray(0)
       204: (  0,  0,  0) #000000 gray(0)
       205: (  0,  0,  0) #000000 gray(0)
       206: (  0,  0,  0) #000000 gray(0)
       207: (  0,  0,  0) #000000 gray(0)
       208: (  0,  0,  0) #000000 gray(0)
       209: (  0,  0,  0) #000000 gray(0)
       210: (  0,  0,  0) #000000 gray(0)
       211: (  0,  0,  0) #000000 gray(0)
       212: (  0,  0,  0) #000000 gray(0)
       213: (  0,  0,  0) #000000 gray(0)
       214: (  0,  0,  0) #000000 gray(0)
       215: (  0,  0,  0) #000000 gray(0)
       216: (  0,  0,  0) #000000 gray(0)
       217: (  0,  0,  0) #000000 gray(0)
       218: (  0,  0,  0) #000000 gray(0)
       219: (  0,  0,  0) #000000 gray(0)
       220: (  0,  0,  0) #000000 gray(0)
       221: (  0,  0,  0) #000000 gray(0)
       222: (  0,  0,  0) #000000 gray(0)
       223: (  0,  0,  0) #000000 gray(0)
       224: (  0,  0,  0) #000000 gray(0)
       225: (  0,  0,  0) #000000 gray(0)
       226: (  0,  0,  0) #000000 gray(0)
       227: (  0,  0,  0) #000000 gray(0)
       228: (  0,  0,  0) #000000 gray(0)
       229: (  0,  0,  0) #000000 gray(0)
       230: (  0,  0,  0) #000000 gray(0)
       231: (  0,  0,  0) #000000 gray(0)
       232: (  0,  0,  0) #000000 gray(0)
       233: (  0,  0,  0) #000000 gray(0)
       234: (  0,  0,  0) #000000 gray(0)
       235: (  0,  0,  0) #000000 gray(0)
       236: (  0,  0,  0) #000000 gray(0)
       237: (  0,  0,  0) #000000 gray(0)
       238: (  0,  0,  0) #000000 gray(0)
       239: (  0,  0,  0) #000000 gray(0)
       240: (  0,  0,  0) #000000 gray(0)
       241: (  0,  0,  0) #000000 gray(0)
       242: (  0,  0,  0) #000000 gray(0)
       243: (  0,  0,  0) #000000 gray(0)
       244: (  0,  0,  0) #000000 gray(0)
       245: (  0,  0,  0) #000000 gray(0)
       246: (  0,  0,  0) #000000 gray(0)
       247: (  0,  0,  0) #000000 gray(0)
       248: (192,192,192) #C0C0C0 gray(192)
       249: (255,  0,  0) #FF0000 gray(255)
       250: (  0,255,  0) #00FF00 gray(0)
       251: (255,255,  0) #FFFF00 gray(255)
       252: (  0,  0,255) #0000FF gray(0)
       253: (255,  0,255) #FF00FF gray(255)
       254: (  0,255,255) #00FFFF gray(0)
       255: (255,255,255) #FFFFFF gray(255)
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: gray(255)
  Border color: gray(223)
  Matte color: gray(189)
  Transparent color: gray(0)
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 512x512+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Undefined
  Orientation: Undefined
  Properties:
    date:create: 2014-10-01T09:18:15+01:00
    date:modify: 2002-11-25T01:33:53+00:00
    signature: dcd0e6587dcdeaaba5999a9bb384473f1af19f7a4c09ffee95356ea51bec22ce
  Artifacts:
    filename: /Users/mark/Desktop/lena.bmp
    verbose: true
  Tainted: True
  Filesize: 263KB
  Number pixels: 262K
  Pixels per second: 26.21MB
  User time: 0.000u
  Elapsed time: 0:01.009
  Version: ImageMagick 6.8.9-7 Q16 x86_64 2014-09-10 http://www.imagemagick.org

正如您所看到的,图像中只有28种独特的颜色,并且调色板仅部分填充,如您所知。

另一种可以帮助您调试代码的技术是让ImageMagick将整个图像转储为文本,就像这样 - 我只显示前8个像素,但您明白了这一点:

convert lena.bmp -colorspace rgb txt:- | more

# ImageMagick pixel enumeration: 512,512,255,rgb
0,0: (85,85,85)  #555555  rgb(85,85,85)
1,0: (95,95,95)  #5F5F5F  rgb(95,95,95)
2,0: (85,85,85)  #555555  rgb(85,85,85)
3,0: (95,95,95)  #5F5F5F  rgb(95,95,95)
4,0: (95,95,95)  #5F5F5F  rgb(95,95,95)
5,0: (85,85,85)  #555555  rgb(85,85,85)
6,0: (95,95,95)  #5F5F5F  rgb(95,95,95)
7,0: (85,85,85)  #555555  rgb(85,85,85)