使用qdbmp的read.bmp图像 [英] read.bmp image using qdbmp

查看:63
本文介绍了使用qdbmp的read.bmp图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个名为qdbmc的库来读取bmp灰度图像(lena_gray.bmp)

I am using a library called qdbmc to read a bmp gray image (lena_gray.bmp)

链接到库

这是我的代码:

int read_image(char *filename)
{
struct _BMP* bmp;
UINT    width, height;
UCHAR red,green,blue;

bmp = BMP_ReadFile(filename);
BMP_CHECK_ERROR( stderr, -1 );

/* Get image's dimensions */
 width = BMP_GetWidth( bmp );
 height = BMP_GetHeight( bmp );
 printf("%lu   %lu \n",width,height);

 /* Iterate through all the image's pixels */
 for (int x = 0 ; x < width ; ++x )
 {
     for (int y = 0 ; y < height ; ++y )
     {
         /* Get pixel's RGB values */
         BMP_GetPixelRGB( bmp, x, y, &red, &green, &blue );
         printf("%d \t %d \t %d \n",red,green,blue);
     }
 }
 return 0;
}

宽度和高度正确显示(512 x 512),但像素值正确,因为它显示了全零.

the width and the height are displayed correctly (512 x 512) but the pixels values are in correct because it is showing me all zeros.

当我将imread()函数与python一起使用时,我得到了:

when i used the imread() function with python i got this :

60 160 160 159 161 156 161 159 162 159 160 158 154 162 158 154 156 155
160 160 153 156 154 156 154 156 154 152 155 153 153 155 153 157 155 158
.....

有人可以帮忙吗?

这是链接到图像的链接(选择Lena,8岁位灰(512 x 512),bmp )

This is the link to the image (choose Lena, 8 bit gray (512 x 512), bmp )

推荐答案

两个输出均不正确.灰度位图中的灰色具有以下形式:(x,x,x)其中红色,蓝色和绿色相同.因此,零是错误的.并且60 160 160 159 161 156 ...是错误的,因为没有重复模式.

Neither output is correct. Gray colors in grayscale bitmap are of the form: (x,x,x) where the red, blue, and green are the same. Therefore zero is wrong. And and 60 160 160 159 161 156 ... is wrong because there is no repeat pattern.

8位位图使用一个表.前54个字节是文件标题.然后有256种颜色(每个4字节长),然后是width * height字节,其中必须填充width,因此width的大小以字节为单位是4的倍数.

8-bit bitmap uses a table. The first 54 bytes are the file heading. Then there is 256 colors (each 4 bytes long) and then width * height bytes, where width has to be padded so the size of width in bytes is a multiple of 4.

位图像素从下到上开始,首先您必须读取行(从下开始),然后读取每一列.此代码应为每行的前5列打印正确的输出:

The bitmap pixels start from bottom to top, first you have to read the row (starting from bottom) then read each column. This code should print the correct output for the first 5 columns in each row:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

#pragma pack(push, 1)
struct my_BITMAPFILEHEADER {
    short bfType;
    int bfSize;
    short bfReserved1;
    short bfReserved2;
    int bfOffBits;
};

struct my_BITMAPINFOHEADER {
    int biSize;
    int biWidth;
    int biHeight;
    short biPlanes;
    short biBitCount;
    int biCompression;
    int biSizeImage;
    int biXPelsPerMeter;
    int biYPelsPerMeter;
    int biClrUsed;
    int biClrImportant;
};
#pragma pack(pop)

int main(void)
{
    if(sizeof(struct my_BITMAPFILEHEADER) != 14)
    {
        printf("stop!\n");
        return 0;
    }

    FILE *fp = fopen("c:\\test\\barbara_gray.bmp", "rb");

    //Read file header
    struct my_BITMAPFILEHEADER fhdr;
    struct my_BITMAPINFOHEADER ihdr;
    fread(&fhdr, sizeof(fhdr), 1, fp);
    fread(&ihdr, sizeof(ihdr), 1, fp);

    if(fhdr.bfType == 'MB' && ihdr.biBitCount == 8 && ihdr.biPlanes == 1)
    {
        //Read table
        unsigned int table[256] = { 0 };
        fread(table, 4, 256, fp);

        int w = ihdr.biWidth;
        int h = ihdr.biHeight;

        //Find width in bytes. Use a math trick to make sure it's divisble by 4
        int w_in_bytes = ((w * 8 + 31) / 32) * 4;
        int size = w_in_bytes * h;

        //Read pixels
        unsigned char *pixels = malloc(size);
        fread(pixels, 1, size, fp);

        //Read from bottom to top:
        for(int row = h - 1; row >= 0; row--)
        {
            printf("%3d: ", h - 1 - row);
            //Read from left to right:
            for(int col = 0; col < w; col++)
            {
                int pos = row * w_in_bytes + col;
                unsigned char color_index = pixels[pos];
                unsigned int clr = table[color_index];
                printf("%02X%02X%02X ", 
                    clr & 0xFF, (clr >> 8) & 0xFF, (clr >> 16) & 0xFF);
                if(col > 5) break;
            }
            printf("\n");
        }

        free(pixels);
    }

    printf("\n");
    fclose(fp);
    return 0;
}

这篇关于使用qdbmp的read.bmp图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆