// 文件名
char file_name[100],file2_name[100];
// 指向影像文件的指针
FILE* stream = NULL;
// 影像宽度
int width = 255;
// 影像高度
int height = 255;
// 旋转角度
int sigma = 45;
// 圆周率π
const double pi = 3.14;
// 正弦值
double sinsigma = 0;
// 余弦值
double cossigma = 0;
// 画布宽度
int newwidth = 0;
// 画布高度
int newheight = 0;
// 保存原始影像的数组
unsigned char* src_image = NULL;
// 保存图像旋转后的数组
unsigned char* dst_image = NULL;
printf("演示如何实现图像叠加. \n");
printf("输入影像文件名: ");
scanf("%s", &file_name);
/*printf("输入影像宽度: ");
scanf("%d", &width);
printf("输入影像高度: ");
scanf("%d", &height);
printf("输入顺时针旋转度数: ");
scanf("%d",&sigma);*/
// 给原始影像开辟内存空间
src_image = (unsigned char*)malloc(width*height*sizeof(unsigned char));
// 打开文件
if((stream = fopen(file_name, "rb" )) == NULL)
{
printf("不能打开'%s'文件,程序退出!\n\n",file_name);
exit(-1);
}
else
printf("成功打开'%s'文件!\n\n",file_name);
// 读取影像数据到内存上
int numread = fread(src_image,sizeof(unsigned char),width*height,stream);
printf("共读入%d字节的数据\n",numread);
// 计算正弦、余弦值
cossigma = cos(sigma*pi/180);
sinsigma = sin(sigma*pi/180);
// 计算画布宽度和高度
newwidth = abs((0*cossigma+0*sinsigma)-(width*cossigma+height*sinsigma));
newheight = abs((-width*sinsigma+0*cossigma)-(-0*sinsigma+height*cossigma));
// 给图像除法后影像开辟内存空间
dst_image = (unsigned char*)malloc(newwidth*newheight*sizeof(unsigned char));
// 对画布赋初值
for(int y = 0;y<newheight;y++)
for(int x = 0;x<newwidth;x++)
{
dst_image[y*newwidth+x] = 0;
}
// 对旋转后图像进行赋值
for(int y = 0;y<height;y++)
for(int x = 0;x<width;x++)
{
dst_image[(int)((-x*sinsigma+y*cossigma)*newwidth+(x*cossigma+y*sinsigma))] = src_image[y*width+x];
}
// 行插值填充空白点
for(int y = 0;y<newheight;y++)
for(int x = 0;x<newwidth;x++)
{
if((dst_image[y*newwidth+x-1] != 0) && (dst_image[y*newwidth+x] == 0) && (dst_image[y*newwidth+x+1] != 0))
dst_image[y*newwidth+x] = dst_image[y*newwidth+x-1];
}
fclose(stream);
// 创建文件,将滤波后影像保存
char file_name_new[100] = {0};
printf("请输入要建立的文件名:");
scanf("%s",file_name_new);
FILE*stream_new = fopen(file_name_new,"at+");
if(stream_new == NULL)
{
printf("无法打开文件");
return 0;
}
fwrite(dst_image,sizeof(unsigned char),newheight*newwidth,stream_new);
fclose(stream_new);
printf("文件已保存\n");
// 完成工作,释放内存
free(src_image);
free(dst_image);
return 0;
调试了几次,总是会在赋值也是正常的,但总会在fclose处跳出导致无法存储旋转后影像。肿么办肿么办