将图像写入C中的文本/原始文件


#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图像中绘制直方图的图,然后将其写入文件。我没有得到想要的图,而是一个非常模糊的图像,毫无意义。感谢您事先提供任何帮助。

I[j][i]=0; fwrite(I[i],1,aux[i],fp1);您的意思是在此处编写行?它不起作用。您正在修改中的元素(列号i ROW NUMBER j在内存中,然后将行编号i写入文件。

最新更新