大神帮帮忙-数字图像处理-图像旋转-fclose处无故跳出

weixin_37602279 2018-02-26 09:03:06
调试了几次,总是会在赋值也是正常的,但总会在fclose处跳出导致无法存储旋转后影像。肿么办肿么办
出问题的地方我标粗啦!麻烦各位大神帮忙看一下了,thx!
// 文件名
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;
...全文
362 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-02-27
  • 打赏
  • 举报
回复
仅供参考:
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
wchar_t formats[5][11]={
    L"image/bmp",
    L"image/jpeg",
    L"image/gif",
    L"image/tiff",
    L"image/png",
};
wchar_t exts[5][5]={
    L".bmp",
    L".jpg",
    L".gif",
    L".tif",
    L".png",
};
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) {
   UINT  num = 0;          // number of image encoders
   UINT  size = 0;         // size of the image encoder array in bytes
   ImageCodecInfo* pImageCodecInfo = NULL;
   GetImageEncodersSize(&num, &size);
   if(size == 0) return -1;  // Failure
   pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
   if(pImageCodecInfo == NULL) return -1;  // Failure
   GetImageEncoders(num, size, pImageCodecInfo);
   for (UINT j = 0; j < num; ++j) {
      if ( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) {
         *pClsid = pImageCodecInfo[j].Clsid;
         free(pImageCodecInfo);
         return j;  // Success
      }
   }
   free(pImageCodecInfo);
   return -1;  // Failure
}
int wmain(int argc,wchar_t *argv[]) {
    int r=1;
    if (argc<4) {
    USAGE:
        wprintf(L"%s srcimg.{bmp|jpg|gif|tif|png|wmf|emf|ico}  desimg.{bmp|jpg|gif|tif|png}  angle\n",argv[0]);
        return r;
    }
    int i;
    for (i=0;i<5;i++) {
        if (0==_wcsicmp(argv[1]+wcslen(argv[1])-4,exts[i])) break;
    }
    if (i>=5) goto USAGE;
    for (i=0;i<5;i++) {
        if (0==_wcsicmp(argv[2]+wcslen(argv[2])-4,exts[i])) break;
    }
    if (i>=5) goto USAGE;
    GdiplusStartupInput gdiplusstartupinput;
    ULONG_PTR gdiplustoken;
    GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);
    {
        Image img(argv[1]);
        if (Ok==img.GetLastStatus()) {
            UINT height = img.GetHeight();
            UINT width  = img.GetWidth();
            REAL angle;
            if (1==swscanf_s(argv[3],L"%f",&angle)) {
                REAL size;
                size=(REAL)sqrt(1.0*width*width+1.0*height*height);
                Matrix mat;
                mat.Translate(size / -2.0f, size / -2.0f);
                mat.Rotate(-angle, MatrixOrderAppend);
                mat.Translate(size / 2.0f, size / 2.0f, MatrixOrderAppend);
                PointF pfTL((size-width)/2.0f      ,(size-height)/2.0f       );
                PointF pfTR((size-width)/2.0f+width,(size-height)/2.0f       );
                PointF pfBL((size-width)/2.0f      ,(size-height)/2.0f+height);
                PointF pfBR((size-width)/2.0f+width,(size-height)/2.0f+height);
                Graphics tgp(&img);
                Bitmap bmp((UINT)size,(UINT)size,&tgp);//Let bmp Resolution equal to img Resolution
                Graphics gp(&bmp);
                gp.SetTransform(&mat);
                gp.DrawImage(&img,pfTL);
                REAL xmin,ymin,xmax,ymax,x,y,rw,rh;
                mat.TransformPoints(&pfTL);
                xmin=xmax=pfTL.X;
                ymin=ymax=pfTL.Y;
                mat.TransformPoints(&pfTR);
                if (xmin>pfTR.X) xmin=pfTR.X;
                if (xmax<pfTR.X) xmax=pfTR.X;
                if (ymin>pfTR.Y) ymin=pfTR.Y;
                if (ymax<pfTR.Y) ymax=pfTR.Y;
                mat.TransformPoints(&pfBL);
                if (xmin>pfBL.X) xmin=pfBL.X;
                if (xmax<pfBL.X) xmax=pfBL.X;
                if (ymin>pfBL.Y) ymin=pfBL.Y;
                if (ymax<pfBL.Y) ymax=pfBL.Y;
                mat.TransformPoints(&pfBR);
                if (xmin>pfBR.X) xmin=pfBR.X;
                if (xmax<pfBR.X) xmax=pfBR.X;
                if (ymin>pfBR.Y) ymin=pfBR.Y;
                if (ymax<pfBR.Y) ymax=pfBR.Y;
                x=xmin;
                y=ymin;
                rw=xmax-x;
                rh=ymax-y;
                Bitmap* clone;
                clone = bmp.Clone(x,y,rw,rh,PixelFormat24bppRGB);//bmp.GetPixelFormat()
                CLSID encoderClsid;
                if (0<=GetEncoderClsid(formats[i],&encoderClsid)) {
                    if (Ok==clone->Save(argv[2],&encoderClsid)) {
                        wprintf(L"OK to %s  %s  %s  %s\n",argv[0],argv[1],argv[2],argv[3]);
                        r=0;
                    } else {
                        wprintf(L"Error to save %s\n",argv[2]);
                        r=4;
                    }
                } else {
                    wprintf(L"Error to GetEncoderClsid(%s,...)\n",formats[i]);
                    r=3;
                }
                delete clone;
            } else {
                wprintf(L"Error to get angle %s\n",argv[3]);
                r=2;
            }
        } else {
            wprintf(L"Error to load %s\n",argv[1]);
            r=5;
        }
    }
    GdiplusShutdown(gdiplustoken);
    return r;
}
自信男孩 2018-02-27
  • 打赏
  • 举报
回复
printf("输入影像文件名: ");
scanf("%s", &file_name);
这个地方有问题,file_name是数组名,本身就是地址。 因此应改成这样:去掉&
printf("输入影像文件名: ");
scanf("%s", file_name);

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧