【问题标题】:Writing an image to a text/raw file in C将图像写入 C 中的文本/原始文件
【发布时间】: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


【解决方案1】:

I[j][i]=0; fwrite(I[i],1,aux[i],fp1); 你的意思是把 column 行写到这里吗?它不是那样工作的。您正在修改内存中(列号i行号j中的一个元素,然后将行号i写入文件。

【讨论】:

  • 我想在我的文件中写入图像行。这就是我这样做的原因。
  • 也许你的意思是写一行fwrite(I[j], ...。我怀疑你打算写一列for(j1=0;j1&lt;aux1;j1++){fwrite(I[j1][i], 1, 1, ...,我严重怀疑你打算写错行fwrite(I[i], ...
  • 是的,但这在这里也不起作用。我仍然无法得到我想要的图表。
  • 哦,这不是在黑色图形的右边画一条白线吗?这不是你想要的吗?我想你现在可能已经决定画一个常量函数了,为了调试你的程序。
  • 不,我想立即绘制直方图的函数。但由于某种原因它不起作用。
猜你喜欢
  • 2012-02-28
  • 2014-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多