【发布时间】:2015-07-12 20:17:28
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned char **T;
int H[256];
int I[256][256];
int i,j,a,aux[256];
FILE *fp=fopen("Collines400300.ima","rb");
FILE *fp1=fopen("image4.raw","wb");
int height = 300;
int width = 400;
T=(unsigned char**)malloc(height*sizeof(unsigned char*));
for(i=0;i<height;i++)
{
if (feof(fp))
{
//handle error... could just malloc and memset to zero's
break;
}
T[i]=(unsigned char*)malloc(width*sizeof(unsigned char*));
fread(T[i],1,400,fp);
}
for(i=0;i<256;i++)
H[i]=0;
//filling up the histogram
for(i=0;i<300;i++)
{
for(j=0;j<400;j++)
H[T[i][j]]++;
}
//printing out the values of each gray scale value in the histogram
for(i=0;i<256;i++)
printf("%d ",H[i]);
//converting the values from a scale of 3000 to a scaale of 256 to fit in our new image
for(i=0;i<256;i++)
{
a=(H[i]+6)/12;
aux[i]=a;
}
//initialising the 2D image to white
for(i=0;i<256;i++)
{
for(j=0;j<256;j++)
I[i][j]=255;
}
//the loop to make the graph for my histogram. This is where the problem is.
for(i=0;i<256;i++)
{
for(j=254;j>254-aux[i];j--)
{
I[j][i]=0;
fwrite(I[i],1,aux[i],fp1);
}
}
fclose(fp);
fclose(fp1);
return 0;
}
给定一张图像,我应该制作图像的直方图,我这样做了。然后在 2D 图像中绘制直方图并将其写入文件。我没有得到我想要的图表,而是一个非常模糊的图像,没有任何意义。提前感谢您的帮助。
【问题讨论】:
-
如果您的输入图像是 400x300,为什么您认为直方图需要从 3,000 缩放到 256?当然,您需要通过循环找到直方图中的最大值,然后将直方图中的所有值乘以 255/max?
标签: c image-processing