weixin_37602279 2018-02-26 13:06 采纳率: 0%
浏览 890
已结题

C语言-图像旋转-fclose处无故跳出

// 文件名
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处跳出导致无法存储旋转后影像。肿么办肿么办
  • 写回答

4条回答 默认 最新

  • threenewbee 2018-02-26 14:56
    关注

    前面加上fflush()

    评论

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能