0

I have an array 230x230 contains values: zero (black pixel), and values from 25 till 1200. I want to save this array as PGM image.

I made the image header: P5, 230x230, 255 (as maximum value), and I wrote the array values without changing. This gave me the image but not in very right look (a lot of white areas - 2nd image)

So, I thought I should do some scaling to the values. Again, I rescaled all values to 0-255, but this gave me totally black image.

Any advice how I can properly solve this issue? I have attached the image required to have (1), and the image I already got (2).

Find below the code I have used:

void write_pgm(double **u, long nx, long ny, char *file_name) {
    int i, j; 
    unsigned char byte;
    FILE *outimage = fopen(file_name, "wb");

    fprintf(outimage, "P5\n"); /* format */
    fprintf(outimage, "%ld %ld\n", nx, ny); /* image size */
    fprintf(outimage, "255\n"); /* maximal value */

    for (j = 0; j < ny; j++) {
        for (i = 0; i < nx; i++) {
            byte = (unsigned char) (u[i][j]);
            fwrite(&byte, sizeof(unsigned char), 1, outimage);
        }
    }
    fclose(outimage);
    return;
}

Thank you.

  • hard to say without your code – Thomas Ayoub May 16 '18 at 14:17
  • Need to see code. I'm a bit confused why you have 230 230 in the header but the posted images are different sized png's. I'm guessing they've been resized? I'm also not sure what you mean by "25 until 1200." – Austin Stephens May 16 '18 at 14:53
  • 1
    If we ever see any code, I bet the bug will look like `newval = oldval / 1200 * 255` –  May 16 '18 at 14:57
  • I have posted the code. I meant with values (25,1200), that the pixel values have different scale than (0,255). – matlab user May 17 '18 at 11:49
  • what wumpus is implying is that if you're getting an all-zero array, it is likely you performed _integer_ division rather than float division at some point during your scaling, resulting in decimals disappearing. E.g. 600 / 1200 * 255 = 0 because 600 / 1200 = 0 (as opposed to 600 / 1200.0 = 0.5 ) – Tasos Papastylianou May 17 '18 at 11:53
  • So I should store the matrix as float and not double? – matlab user May 17 '18 at 13:24

0 Answers0