c语言图像清晰度算法源码_c语言数字图像处理

hacker|
110

文章目录:

用C语言编写程序处理图片bmp文件 1.读取图片的宽度,高度,每个像素所需的位数,水平分辨率,垂直

#include windows.h

//读bmp图片需要两个结构

#pragma pack(push, enter_defBM, 1) //指定内存对齐单位为1。

typedef struct tagBmpFileHeader

{

WORD  bfType;      // 文件类型  BM

DWORD bfSize;      // 文件大小

WORD  bfReserved1; // 保留字

WORD  bfReserved2; // 保留字

DWORD bfOffBits;   // 位图的数据信息离文件头的偏移量

} BFH;

typedef struct tagBmpImgHeader

{

DWORD biSize;          // 表示本结构的大小,0X28

LONG  biWidth;         // 位图的宽度

LONG  biHeight;        // 位图的高度

WORD  biPlanes;        // 位面数永远为1

WORD  biBitCount;      // 位图的位数

DWORD biCompression;   // 压缩类型

DWORD biSizeImage;     // 表示位图数据区域的大小

LONG  biXPelsPerMeter; // 表示显示设备的水平分辨率

LONG  biYPelsPerMeter; // 表示显示设备的垂直分辨率

DWORD biClrUsed;       // 实际使用的颜色数目

DWORD biClrImportant;  // 重要的颜色数量

} BIH;

#pragma pack(pop, enter_defBM) //恢复默认内存对齐单位。

#define HDIB HANDLE // 位图句柄

DWORD WINAPI DIBNumColors(BYTE * data)

{

WORD wBitCount;

DWORD dwClrUsed = ((BIH *)data)-biClrUsed;

if (dwClrUsed != 0) return (WORD)dwClrUsed;

wBitCount = ((BIH *)data)-biBitCount;

return 1  wBitCount;

}

WORD WINAPI PaletteSize(BYTE * data)

{

return (WORD)(::DIBNumColors(data) * sizeof(RGBQUAD));

BYTE *  WINAPI FindDIBBits(BYTE * data)

{

return (data + *(DWORD *)data + ::PaletteSize(data));

}

// 获取Bmp的宽度

DWORD FAR DIBWidth(const BYTE * data)   

{   

BIH * pbmi;

pbmi = (BIH *)data; 

if(pbmi-biSize == sizeof(BIH)) return pbmi-biWidth;

else return -1;

}   

// 获取Bmp的高度

DWORD FAR DIBHeight(const BYTE * data)   

{   

BIH * pbmi;

pbmi = (BIH *)data; 

if(pbmi-biSize == sizeof(BIH)) return pbmi-biHeight;

else return -1;

}   

// 从文件读取Bmp图像数据

HDIB WINAPI ReadDIBFile(FILE * fp)

{

BFH    bmf;

HDIB   hDIB;

BYTE * pData;

rewind(fp);

if(fread(bmf, sizeof(BFH), 1, fp) != 1) return NULL; // 文件读取错误

if(bmf.bfType != 19778) return NULL;                  // 文件类型错误

hDIB = (HDIB) ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, bmf.bfSize); // 为DIB分配内存

if (hDIB == 0) return NULL;                           // 内存分配失败。

pData = (BYTE *)::GlobalLock((HGLOBAL) hDIB);         // 锁定

if(fread(pData, 1, bmf.bfSize - sizeof(BFH), fp) != (bmf.bfSize - sizeof(BFH))) // 文件读取错误

{

::GlobalUnlock((HGLOBAL) hDIB); // 解除锁定

::GlobalFree((HGLOBAL) hDIB);   // 释放内存

return NULL;

}

::GlobalUnlock((HGLOBAL) hDIB);     // 解除锁定

return hDIB;                        // 返回DIB句柄

}

BOOL WINAPI PaintDIB(HDC hDC, int posX, int posY, HDIB hDIB)

{

BYTE *   pDIBHd;             // BITMAPINFOHEADER指针

BYTE *   pDIBBits;           // DIB象素指针

BOOL     bSuccess = FALSE;   // 成功标志

HPALETTE hPal     = NULL;    // DIB调色板

//HPALETTE hOldPal  = NULL;    // 以前的调色板

if (hDIB == NULL) return FALSE;                    // 判断DIB对象是否为空

pDIBHd    = (BYTE *) ::GlobalLock((HGLOBAL) hDIB); // 锁定DIB

pDIBBits  = ::FindDIBBits(pDIBHd);                 // 找到DIB图像象素起始位置

::SetStretchBltMode(hDC, COLORONCOLOR);            // 设置显示模式

    // 调用SetDIBitsToDevice()来绘制DIB对象

bSuccess = ::SetDIBitsToDevice(hDC, // hDC

           posX, posY, 

   ((BIH *)pDIBHd)-biWidth,    // nDestWidth

   ((BIH *)pDIBHd)-biHeight,   // nDestHeight

           0,                           // SrcX

           0,                           // SrcY

           0,                           // nStartScan

           (WORD)DIBHeight(pDIBHd),     // nNumScans

           pDIBBits,                    // lpBits

           (LPBITMAPINFO)pDIBHd,        // lpBitsInfo

           DIB_RGB_COLORS);             // wUsage

::GlobalUnlock((HGLOBAL) hDIB);     // 解除锁定

return bSuccess;

}

// 打印位图信息

VOID WINAPI PrintDIBInfo(HDIB hDIB)

{

BYTE *   pDIBHd = (BYTE *)::GlobalLock((HGLOBAL) hDIB);

BIH *    pbmi   = (BIH *)pDIBHd;

const char *lp[] = 

{

"位图信息长度: %d\n",

"位图图像大小: %dx%d\n",

"位面数:%d\n",

"位图颜色深度: %d\n",

"位图数据压缩类型: %d\n",

"位图数据区域大小: %d\n",

"位图分辨率: 水平 %d dpi, 垂直 %d dpi\n",

};

printf("Windows V3 cBitmapInfoHeader 信息\n\n");

printf(lp[0], pbmi-biSize);

printf(lp[1], pbmi-biWidth, pbmi-biHeight);

printf(lp[2], pbmi-biPlanes);

printf(lp[3], pbmi-biBitCount);

printf(lp[4], pbmi-biCompression);

printf(lp[5], pbmi-biSizeImage);

printf(lp[6], (LONG)(pbmi-biXPelsPerMeter * 0.0254f + 0.5f), (LONG)(pbmi-biYPelsPerMeter * 0.0254f + 0.5f));

::GlobalUnlock((HGLOBAL) hDIB);     // 解除锁定

}

int main(int argc, char* argv[])

{

HDIB x;

FILE * fp = fopen("1.bmp", "rb");

if(fp==NULL) return -1;

x = ReadDIBFile(fp);

printf("DIB handle %u", x);

PaintDIB(GetDC(NULL), 0, 0, x);

PrintDIBInfo(x);

return 0;

}

如何利用c语言实现像素图形的输出

1、可以变成灰度图也可以不变。这里假设你的图像都是IPL_DEPTH_8U类型。

2、如果变成灰度图,就是单通道图像,获取的就是每一个像素点的灰度值。

IplImage* img = cvLoadImage("test.bmp", 0);

for (int i = 0; i img-height; i++)

{

for (int j = 0; j img-width; j++)

{

//方法一:使用cvGet2D()函数间接访问

CvScalar s = cvGet2D(img, i, j); //其中i代表y轴(第i行),即height;j代表x轴(第j列),即width。

printf("gray value=%f\n",s.val[0]);

//方法二:使用直接访问

uchar val = ((uchar *)(img-imageData + i*img-widthStep))[j]; //i和j的意义同上

printf("gray value=%d\n",val);

}

}

3、如果不变成灰度图,就是3通道图像,获取的就是每一个像素点的BGR值,然后分别获取B值,G值和R值。

IplImage* img = cvLoadImage("test.bmp", 1);

for (int i = 0; i img-height; i++)

{

for (int j = 0; j img-width; j++)

{

//方法一:使用cvGet2D()函数间接访问

CvScalar s=cvGet2D(img,i,j); //其中i代表y轴(第i行),即height;j代表x轴(第j列),即width。

printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]); //注意是BGR顺序

//方法二:使用直接访问

int bVal = ((uchar *)(img-imageData + i*img-widthStep))[j*img-nChannels + 0]; // B

int gVal = ((uchar *)(img-imageData + i*img-widthStep))[j*img-nChannels + 1]; // G

int rVal = ((uchar *)(img-imageData + i*img-widthStep))[j*img-nChannels + 2]; // R

printf("B=%d, G=%d, R=%d\n",bVal,gVal,rVal); //注意是BGR顺序

}

}

C语言打开图像文件后读取像素

C语言打开图像文件后运用以下代码就可以读取像素,具体如下:

#ifndef IMAGE_H

#define IMAGE_H

void image_info(FILE* file);

void image_save(FILE *file);

void image_gray();

void image_binarization();

void image_opposite();

void image_channel(); //抽取RGB通道

void image_bright();//改变图像亮度

typedef struct BMP

{

//14字节

unsigned short bfType; //文件标识 2字节 必须为BM

unsigned int bfSize; //文件大小 4字节

unsigned short bfReserved1; //保留,每字节以"00"填写 2字节

unsigned short bfReserved2; //同上 2字节

unsigned int bfOffBits; //记录图像数据区的起始位置(图象数据相对于文件头字节的偏移量)。 4字节

//40字节

unsigned int biSize; //表示本结构的大小 4字节

int biWidth; //位图的宽度 4字节

int biHeight; //位图的高度 4字节

unsigned short biPlanes; //永远为1 , 2字节

unsigned short biBitCount; //位图的位数 分为1 4 8 16 24 32 2字节

unsigned int biCompression; //压缩说明 4字节

unsigned int biSizeImage; //表示位图数据区域的大小以字节为单位 4字节

int biXPelsPerMeter; //用象素/米表示的水平分辨率 4字节

int biYPelsPerMeter; //用象素/米表示的垂直分辨率 4字节

unsigned int biClrUsed; //位图使用的颜色索引数 4字节

unsigned int biClrImportant; //对图象显示有重要影响的颜色索引的数目 4字节

} BMP;

int line_byte;

unsigned char *imagedata;

extern BMP bmp;

extern int line_byte;

extern unsigned char *imagedata;

#endif

//image_rw.c文件

#includestdio.h

#includestdlib.h

#include"image.h"

void image_info(FILE *file)

{

int times=3; //输入文件名次数。

char bmp_name[10]; //文件名

printf("\nplease enter a file name for reading:");

do

{

if (times3)

{

printf("\nplease enter a file name for reading again:");

}

fflush(stdin);

gets(bmp_name);

//printf("\n%s",bmp_name);

file=fopen(bmp_name,"rb+"); //打开一个文件进行读写操作。

--times;

if (file==NULL)

{

printf("\nerror opening %s for reading! ",bmp_name);

}

else

{

break;

}

}

while(times!=0);

if (times==0)

{

printf("\nsorry, shutdown!");

exit(1);

}

//读取图像信息

fseek(file,0L,0); //读取图像文件类型

fread(bmp,sizeof(BMP),1,file);

printf("\n bmp tpye: %u",bmp.bfType);

printf("\n bmp size: %u",bmp.bfSize);

printf("\n bmp reserved1: %u",bmp.bfReserved1);

printf("\n bmp reserved2: %u",bmp.bfReserved2);

printf("\n bmp offBits: %u",bmp.bfOffBits);

printf("\n bmp bisize: %u",bmp.biSize);

printf("\n bmp biWidth: %d",bmp.biWidth);

printf("\n bmp biHeight: %d",bmp.biHeight);

printf("\n bmp biplans: %u",bmp.biPlanes);

printf("\n bmp biBitCount: %u",bmp.biBitCount);

printf("\n bmp biCompression: %u",bmp.biCompression);

printf("\n bmp biSizeImage: %u",bmp.biSizeImage);

printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);

printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);

printf("\n bmp biClrUsed: %u",bmp.biClrUsed);

printf("\n bmp biClrImportant: %u\n",bmp.biClrImportant);

line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //获得图像数据每行的数据个数

//printf("dfsa%u",bmp.line_byte);

//bmp.imagedata=NULL;

imagedata=(unsigned char*)malloc(bmp.biSizeImage);

fseek(file,(long)bmp.bfOffBits,0);

fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);

fclose(file);

}

//保存图像

void image_save(FILE *file)

{

int times=3; //输入文件名次数。

char bmp_name[10]; //文件名

//int i; //记录数据区个数

printf("\nplease enter a file name for writeing:");

do

{

if (times3)

{

printf("\nplease enter a file name for writeing again:");

}

fflush(stdin);

gets(bmp_name);

printf("\n%s",bmp_name);

file=fopen(bmp_name,"wb+"); //打开一个文件进行读写操作。

--times;

if (file==NULL)

{

printf("\nerror opening %s for writing",bmp_name);

}

else

{

break;

}

}

while(times!=0);

if (times==0)

{

printf("\nsorry, shutdown!");

exit(1);

}

//写文件头

printf("\n%s",bmp_name);

fseek(file,0L,0); //图像文件类型

fwrite((bmp.bfType),sizeof(short),1,file);

printf("\n bmp tpye: %d",bmp.bfType);

fseek(file,2L,0); //图像文件大小

fwrite((bmp.bfSize),sizeof(int),1,file);

printf("\n bmp size: %d",bmp.bfSize);

fseek(file,6L,0); //图像文件保留字1

fwrite((bmp.bfReserved1),sizeof(short),1,file);

printf("\n bmp reserved1: %d",bmp.bfReserved1);

fseek(file,8L,0); //图像文件保留字2

fwrite((bmp.bfReserved2),sizeof(short),1,file);

printf("\n bmp reserved2: %d",bmp.bfReserved2);

fseek(file,10L,0);//数据区的偏移量

fwrite((bmp.bfOffBits),sizeof(short),1,file);

printf("\n bmp offBits: %d",bmp.bfOffBits);

fseek(file,14L,0);//文件头结构大小

fwrite((bmp.biSize),sizeof(int),1,file);

printf("\n bmp bisize: %d",bmp.biSize);

fseek(file,18L,0);//图像的宽度

fwrite((bmp.biWidth),sizeof(int),1,file);

printf("\n bmp biWidth: %d",bmp.biWidth);

fseek(file,22L,0);//图像的高度

fwrite((bmp.biHeight),sizeof(int),1,file);

printf("\n bmp biHeight: %d",bmp.biHeight);

fseek(file,24L,0);//图像的面数

fwrite((bmp.biPlanes),sizeof(short),1,file);

printf("\n bmp biplans: %d",bmp.biPlanes);

fseek(file,28L,0);//图像一个像素的字节数

fwrite((bmp.biBitCount),sizeof(short),1,file);

printf("\n bmp biBitCount: %d",bmp.biBitCount);

fseek(file,30L,0);//图像压缩信息

fwrite((bmp.biCompression),sizeof(short),1,file);

printf("\n bmp biCompression: %d",bmp.biCompression);

fseek(file,34L,0);//图像数据区的大小

fwrite((bmp.biSizeImage),sizeof(int),1,file);

printf("\n bmp biSizeImage: %d",bmp.biSizeImage);

fseek(file,38L,0);//水平分辨率

fwrite((bmp.biXPelsPerMeter),sizeof(int),1,file);

printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);

fseek(file,42L,0);//垂直分辨率

fwrite((bmp.biYPelsPerMeter),sizeof(int),1,file);

printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);

fseek(file,46L,0);//颜色索引数

fwrite((bmp.biClrUsed),sizeof(int),1,file);

printf("\n bmp biClrUsed: %d",bmp.biClrUsed);

fseek(file,50L,0);//重要颜色索引数

fwrite((bmp.biClrImportant),sizeof(int),1,file);

printf("\n bmp biClrImportant: %d\n",bmp.biClrImportant);

fseek(file,(long)(bmp.bfOffBits),0);

fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);

fclose(file);

}

//pixProcess.c文件

#includestdio.h

#includestdlib.h

#includemath.h

#include"image.h"

//灰度化

void image_gray()

{

int i,j;

unsigned char tmp;

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));

imagedata[i*line_byte+j*3+0]=tmp;

imagedata[i*line_byte+j*3+1]=tmp;

imagedata[i*line_byte+j*3+2]=tmp;

//printf("\nnidsfh%d %d",i,j);

}

}

}

//二值化

void image_binarization()

{

int i,j;

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte;j++)

{

if ((*(imagedata+i*line_byte+j))128)

{

imagedata[i*line_byte+j]=0;

}

else

{

imagedata[i*line_byte+j]=255;

}

}

}

}

void image_opposite() //反相

{

int i,j;

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte;j++)

{

imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);

}

}

}

void image_channel() //抽取RGB通道

{

int i,j;

char rgb;

printf("\nplease enter a char(r/g/b): ");

fflush(stdin);

scanf("%c",rgb);

if (rgb=='b')

{

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

imagedata[i*line_byte+3*j+1]=0;

imagedata[i*line_byte+3*j+2]=0;

}

}

}

else if(rgb=='g')

{

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

imagedata[i*line_byte+3*j]=0;

imagedata[i*line_byte+3*j+2]=0;

}

}

}

else

{

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte/3;j++)

{

imagedata[i*line_byte+3*j]=0;

imagedata[i*line_byte+3*j+1]=0;

}

}

}

}

void image_bright()//改变图像亮度

{

int level;

int i,j;

printf("\n please enter the level of brightness[-255 to 255] :");

fflush(stdin);

scanf("%d",level);

for (i=0;ibmp.biHeight;i++)

{

for (j=0;jline_byte;j++)

{

if (level=0)

{

if ((imagedata[i*line_byte+j]+level)255)

imagedata[i*line_byte+j]=255;

else

imagedata[i*line_byte+j]+=level;

}

else

{

if ((imagedata[i*line_byte+j]-abs(level))0)

imagedata[i*line_byte+j]=0;

else

imagedata[i*line_byte+j]+=level;

}

}

}

}

//void image_create() //创建一幅24位BMP图像文件。

//{

//main.c文件

#includestdio.h

#includestdlib.h

#includestring.h

#includeconio.h

#include"image.h"

BMP bmp;

int main()

{

FILE *file=NULL;

int choose;

char gono;

do

{

image_info(file); //imagedata已经分配了动态内存,但是没有释放

printf("\n 1.image_opposite");

printf("\n 2.image_gray");

printf("\n 3.image_binarization");

printf("\n 4.image_channel");

printf("\n 5.image_brightness");

//printf("6.image_opposite");

//printf("7.image_opposite");

printf("\nchoose your options:");

fflush(stdin);

scanf("%d",choose);

switch(choose)

{

case 1:

image_opposite();

image_save(file);

free(imagedata);

break;

case 2:

image_gray();

image_save(file);

free(imagedata);

break;

case 3:

image_binarization();

image_save(file);

free(imagedata);

break;

case 4:

image_channel();

image_save(file);

free(imagedata);

break;

case 5:

image_bright();

image_save(file);

free(imagedata);

break;

default:

printf("\n wrong choose!");

}

printf("\nlet's go on?(y/n):");

fflush(stdin);

scanf("%c",gono);

if (gono=='n')

{

printf("\nbye bye!");

break;

}

}

while(1);

return 0;

}

关于C语言处理图像的问题

#include Bitmap.h//引用处理图片的头文件

FILE *hFile = NULL;//文件指针

int LoadBitmapFile ( char *fileName, BITMAP_IMAGE_PTR bitmap )

{

int hImageFile = 0; //文件读取成功的标识

unsigned char *workingBuffer = 0; //保存读取的图片

hImageFile = _open ( fileName, _O_RDONLY ); //以只读方式打开图片文件,成功返回1,失败返回-1

if ( hImageFile==-1 )//如果打开失败,则输出警告

{

WriteLogFile ( "LoadBitmapFile function error! \nCan not open the bitmap.\n" );

return 0;

}

这只是最基本的打开图片文件,跟打开普通文件没什么区别,后面要对图片进行处理的话没有图形学的知识会很困难,如果想进一步研究,建议学习一下计算机图形学

3条大神的评论

  • avatar
    访客 2022-11-29 下午 11:42:42

    sizeof(int),1,file); printf("\n bmp size: %d",bmp.bfSize); fseek(file,6L,0); //图像文件保留字1 fwrite((bmp.bfReserved1),sizeof(short),1

  • avatar
    访客 2022-11-30 上午 04:14:31

    ns: %u",bmp.biPlanes); printf("\n bmp biBitCount: %u",bmp.biBitCount); printf("\n bmp biCompression: %u

  • avatar
    访客 2022-11-29 下午 09:12:20

    f("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]); //注意是BGR顺序//方法二:使用直接访问int bVal = ((uchar *)(img-im

发表评论