C语言实现数字图像处理
以前是用VC++实现数字图像处理的,那里面打开一个BMP文件然后对文件数据处理很简单,但用到C时却取不了BMP中的图像数据,我用的是fopen和fread命令,程序如下:功能:打开一副位图,根据给定的阈值进行二值化,origin_image存放原始图片,result_image存放处理后的图片,程序运行后result_image中没有图片,另外这是在VC++环境下运行的,在CCS环境下都运行不了。
#include <stdio.h>
#define IMAGE_WIDTH 4
#define IMAGE_HEIGHT 10
#define ONE 1
#define ZERO 0
unsigned char origin_image[IMAGE_WIDTH][IMAGE_HEIGHT];
unsigned char result_image[IMAGE_WIDTH][IMAGE_HEIGHT];
main()
{
FILE *I_file,*O_file;
char origin_image_file_name[80];
char result_image_file_name[80];
int threshold;
int i,j;
printf("origin_image_file_name-->");
scanf("%s",origin_image_file_name);
printf("\n");
printf("result_image_file_name-->");
scanf("%s",result_image_file_name);
printf("\n");
printf("threshold(0^255)-->");
scanf("%d",&threshold);
printf("\n");
//read image file
if((I_file=fopen(origin_image_file_name,"r"))==NULL)
{
printf("file open error!");
return 0;
}
if((O_file=fopen(result_image_file_name,"w"))==NULL)
{
printf("file open error!");
return 0;
}
for(i=0;i<IMAGE_WIDTH;i++)
{
fread(origin_image[i],sizeof(unsigned char), IMAGE_HEIGHT,I_file);
}
fclose(I_file);
for(i=0;i<IMAGE_WIDTH;i++)
{
for(j=0;j<IMAGE_HEIGHT;j++)
{
result_image[i][j]=(origin_image[i][j]>threshold)?ONE:ZERO;
}
}
//SAVE
for(i=0;i<IMAGE_WIDTH;i++)
{
fwrite(result_image[i],sizeof(unsigned char),IMAGE_HEIGHT,O_file);
}
fclose(O_file);
}
希望各位仁人志士给予帮助