纯C语言实现图像处理?

哪位大哥知道如何用纯C语言(不能用Turbo C ,windows API 和 MFC)对BMP图像实行二值化处理,最好有源代码,非常感谢!!
好的话我肯定追加分数!!!

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define ONE 255
#define ZERO 0
/*
typedef struct tagBITMAPFILEHEADER { // bmfh
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{ // bmih
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
*/
void main (int argc,char *argv[])
{
FILE *fi,*fo;//I/O file
char fin[80],fon[80];//I/O file name
unsigned char **ri,**ro;
unsigned char buff;
long w,h;
int t;
int i,j;
if(argc<3)
{
printf("orginfile name:");
scanf("%s",fin);
printf("resultfile name:");
scanf("%s",fon);
}else{
sscanf(argv[1],"%s",fin);
sscanf(argv[2],"%s",fon);
}
if(argc==4)
sscanf(argv[4],"%d",&t);
else{
printf("theshold [0,255]:");
scanf("%d",&t);
}

if (((fi=fopen(fin,"rb"))==NULL)||((fo=fopen(fon,"wb"))==NULL))
{
puts("\nfile open failed");
return;
}

fseek(fi,18L,SEEK_SET);
fread(&w,sizeof(long),1,fi);
fread(&h,sizeof(long),1,fi);

fseek(fi,0L,SEEK_SET);

ri=(unsigned char **)malloc(sizeof(unsigned *)*h);
for (i=0;i<h;i++)
*(ri+i)=(unsigned char *)malloc(sizeof(unsigned)*w);

ro=(unsigned char **)malloc(sizeof(unsigned *)*h);
for (i=0;i<h;i++)
*(ro+i)=(unsigned char *)malloc(sizeof(unsigned)*w);
//分配失败后果自负!

for (i=0;i<32;i++){
fread(&buff,sizeof(buff),1,fi);
fwrite(&buff,sizeof(buff),1,fo);}
for (i=0;i<h;i++)
for (j=0;j<w;j++)
fread(*(ri+i)+j,sizeof(unsigned char),1,fi);

for (i=0;i<h;i++)
for (j=0;j<w;j++)
*(*(ro+i)+j)=((*(*(ri+i)+j)<=t)?ZERO:ONE);
for (i=0;i<h;i++)
for (j=0;j<w;j++)
fwrite(*(ro+i)+j,sizeof(unsigned char),1,fo);
fclose(fo);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-02
这个不难吧?虽然我不用c,可是说下怎么做,首先打开一个BMP文件,然后按照bmp格式解析到一个数组中,接着进行二值化(这个有很多方法),最后将数组中的数据写入一个新的BMP文件就可以了。
第2个回答  2020-05-31
#include
<stdio.h>
#include
<stdlib.h>
#include
<conio.h>
#define
ONE
255
#define
ZERO
0
/*
typedef
struct
tagBITMAPFILEHEADER
{
//
bmfh
WORD
bfType;
DWORD
bfSize;
WORD
bfReserved1;
WORD
bfReserved2;
DWORD
bfOffBits;
}
BITMAPFILEHEADER;
typedef
struct
tagBITMAPINFOHEADER{
//
bmih
DWORD
biSize;
LONG
biWidth;
LONG
biHeight;
WORD
biPlanes;
WORD
biBitCount
DWORD
biCompression;
DWORD
biSizeImage;
LONG
biXPelsPerMeter;
LONG
biYPelsPerMeter;
DWORD
biClrUsed;
DWORD
biClrImportant;
}
BITMAPINFOHEADER;
*/
void
main
(int
argc,char
*argv[])
{
FILE
*fi,*fo;//I/O
file
char
fin[80],fon[80];//I/O
file
name
unsigned
char
**ri,**ro;
unsigned
char
buff;
long
w,h;
int
t;
int
i,j;
if(argc<3)
{
printf("orginfile
name:");
scanf("%s",fin);
printf("resultfile
name:");
scanf("%s",fon);
}else{
sscanf(argv[1],"%s",fin);
sscanf(argv[2],"%s",fon);
}
if(argc==4)
sscanf(argv[4],"%d",&t);
else{
printf("theshold
[0,255]:");
scanf("%d",&t);
}
if
(((fi=fopen(fin,"rb"))==NULL)||((fo=fopen(fon,"wb"))==NULL))
{
puts("\nfile
open
failed");
return;
}
fseek(fi,18L,SEEK_SET);
fread(&w,sizeof(long),1,fi);
fread(&h,sizeof(long),1,fi);
fseek(fi,0L,SEEK_SET);
ri=(unsigned
char
**)malloc(sizeof(unsigned
*)*h);
for
(i=0;i<h;i++)
*(ri+i)=(unsigned
char
*)malloc(sizeof(unsigned)*w);
ro=(unsigned
char
**)malloc(sizeof(unsigned
*)*h);
for
(i=0;i<h;i++)
*(ro+i)=(unsigned
char
*)malloc(sizeof(unsigned)*w);
//分配失败后果自负!
for
(i=0;i<32;i++){
fread(&buff,sizeof(buff),1,fi);
fwrite(&buff,sizeof(buff),1,fo);}
for
(i=0;i<h;i++)
for
(j=0;j<w;j++)
fread(*(ri+i)+j,sizeof(unsigned
char),1,fi);
for
(i=0;i<h;i++)
for
(j=0;j<w;j++)
*(*(ro+i)+j)=((*(*(ri+i)+j)<=t)?ZERO:ONE);
for
(i=0;i<h;i++)
for
(j=0;j<w;j++)
fwrite(*(ro+i)+j,sizeof(unsigned
char),1,fo);
fclose(fo);
}
第3个回答  2019-08-09
我建议你看一下《c语言编程宝典》这本电子书。
相似回答