关于c语言读图像并统计灰度直方图的问题!!!!求救~~~【急】

zhangchenn123 2012-04-06 11:43:55
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Width 576
#define Height 549
int value[256];
int Buffer[Width][Height];

void ReadDat()
{
FILE *fp;
memset(value,0,sizeof(value));

int i,j;
if((fp=fopen("E:\\1.raw","rt"))==NULL)
{
printf("Cannot open the file\n");
exit(0);
}
else
{
for(i=0;i<Width;i++)
for(j=0;j<Height;j++)
{
value[Buffer[i][j]]++;
}
}
// fclose(fp);

}


void WriteDat()
{
FILE *fp;
int i;
fp=fopen("result.txt","w");
printf("直方图统计结果:\n");
printf("灰度级\t频率\n");
for(i=0;i<256;i++)
{
printf("%d/t%f\n",i,value[i]);
}
// fclose(fp);
}

void main()
{

ReadDat();
WriteDat();
}


程序编译通过,但是执行的时候会有debug error 出现~~~~~求解释啊~~~~
...全文
993 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
屁兜兜 2012-05-11
  • 打赏
  • 举报
回复
求楼主,分享一下吧!或者发我邮箱我的QQ号是1398961676
屁兜兜 2012-05-11
  • 打赏
  • 举报
回复
怎么解决的?
zhangchenn123 2012-04-09
  • 打赏
  • 举报
回复
已经解决了。。。。==
zhangchenn123 2012-04-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

没看懂你的目的,打开文件,但是又不是使用?
还是你的程序只贴出来一部分?

不过单纯这句 value[Buffer[i][j]]++; 就有问题吧
Buffer[i][j] 的值 是啥?不会越界?
理论上他应该是图片的灰度值吧。
就是要小于256
[/Quote]

我又重写了一个。。。大神再看看这个。。。。

#include<stdio.h>
#include<stdlib.h>
//#include<string.h>
int *value=new int[256*256]; //存像素的数组空间
void OpenDat()//打开文件(图像)
{
FILE *fp;
if((fp=fopen("E:\\Lena.raw","rw"))==NULL)
exit(0);
// return true;

}
bool ReadDat()//读图像
{
FILE *fp;
int i,j;
for(i=0;i<256;i++)
for(j=0;j<256;j++)
{
if(fread(&value[i][j],8,256,fp)!=1) //这样写对不对?
printf("error\n");
}
fclose(fp);
}
bool WriteDat()
{
FILE *fp;
int i,j,k;
if((fp=fopen("E:\\result.txt","rw"))==NULL)
exit(0);


for(i=0;i<256;i++)
for(j=0;j<256;j++)
{
fwrite(&value[i][j],8,256,fp);
}
for(k=0;k<256;k++)
{
printf("%d\t%f",k,value);
}
}
void main()
{
// int * value[256][256]=new int[256*256];
OpenDat();
ReadDat();
WriteDat();
delete[] value;
}

ERROR

-----Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
E:\zz\zz\test\test.cpp(20) : error C2109: subscript requires array or pointer type
E:\zz\zz\test\test.cpp(20) : error C2102: '&' requires l-value
E:\zz\zz\test\test.cpp(36) : error C2109: subscript requires array or pointer type
E:\zz\zz\test\test.cpp(36) : error C2102: '&' requires l-value
Error executing cl.exe.

test.obj - 4 error(s), 0 warning(s)
relaxisland 2012-04-07
  • 打赏
  • 举报
回复
没看懂你的目的,打开文件,但是又不是使用?
还是你的程序只贴出来一部分?

不过单纯这句 value[Buffer[i][j]]++; 就有问题吧
Buffer[i][j] 的值 是啥?不会越界?
理论上他应该是图片的灰度值吧。
就是要小于256
zhangchenn123 2012-04-07
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Width 576
#define Height 549
int value[256];
int Buffer[Width][Height];

void ReadDat()
{
FILE *fp;
memset(value,0,sizeof(val……
[/Quote]

初始化了。。。但还是不行。。。。T^T
xmu_才盛 2012-04-07
  • 打赏
  • 举报
回复

for(i=0;i<Width;i++)
for(j=0;j<Height;j++)
{
value[Buffer[i][j]]++;
}

你这里面的 Buffer 没初始化就敢用?不崩溃才怪

69,377

社区成员

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

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