0% found this document useful (0 votes)
35 views

Program 2

The document discusses reading and processing image pixels from a PGM file. It first reads the header information like width, height and maximum value. It then allocates memory to store the pixel values in an array and reads each pixel value from the file into the array. It also shows code to print the intensity values of specific pixels. The document further discusses blurring an image by taking the average of neighboring pixels. It also shows code to generate a histogram of pixel intensity levels by counting pixels for each possible value.

Uploaded by

Dian Anggraini
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Program 2

The document discusses reading and processing image pixels from a PGM file. It first reads the header information like width, height and maximum value. It then allocates memory to store the pixel values in an array and reads each pixel value from the file into the array. It also shows code to print the intensity values of specific pixels. The document further discusses blurring an image by taking the average of neighboring pixels. It also shows code to generate a histogram of pixel intensity levels by counting pixels for each possible value.

Uploaded by

Dian Anggraini
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

READING IMAGE

#include <stdio.h>

#include <stdlib.h>

main()

FILE *fp;

int i,j,k,height,width,maxval,coordinate;

char line [100];

unsigned char *pixel;

fp=fopen("contoh-1.pgm","r");//pointer file fp will open the file of input image with type read//

fgets(line,100,fp); //read the header of the input image//

fgets(line,100,fp);

// now lets get the width and height from the header//

width=atoi(strtok(line, " "));

height=atoi(strtok(NULL," "));

printf("The width is %d and the height is %d\n",width,height);

// getting the maximum value//

fgets(line,100,fp);

maxval=atoi(line);

printf("The maximum value is %d\n",maxval);


//now let's put the pixel value in certain memory, so we can use it later on//

// USE POINTER//

pixel = malloc(width*height);

for(k=0;k<(width*height);k++)

fread(pixel+k,sizeof(unsigned char),1,fp);

printf("Intensity of pixel %dth is %d\n",k,*(pixel+k));

for(j=0;j<height;j++){

for(i=0;i<width;i++)

coordinate=(j*width)+i;

fread(pixel+coordinate,sizeof(unsigned char),1,fp);

printf("Intensity of column %dth and row %dth is %d\n",j,i,*(pixel+coordinate));

}}

fclose(fp);

}
BLURRING

// Latihan Image Processing

// Anto Satriyo Nugroho

#include <stdio.h>

#include <stdlib.h>

main(int argc,char **argv)

FILE *fpi,*fpo;

char line[100];

int i,j,height=455,width=440;

unsigned char input_image[height][width],output_image[height][width];

if(argc!=3) {

fprintf(stderr,"%s 3-2.pgm 3-2a.pgm\n",argv[0]);

exit(1);

fpi=fopen(argv[1],"r");

fpo=fopen(argv[2],"w");

fgets(line,100,fpi); // baca baris pertama dari input file

fprintf(fpo,"%s",line); // tulis baris pertama ke output file

fgets(line,100,fpi); // baca baris kedua dari input file


fprintf(fpo,"%s",line); // tulis baris kedua ke output file

fgets(line,100,fpi); // baca baris ketiga dari input file

fprintf(fpo,"%s",line); // tulis baris ketiga ke output file

for(j=0;j<height;j++) {

for(i=0;i<width;i++) {

fread(&input_image[j][i],sizeof(unsigned char),1,fpi); // baca pixel satu persatu dari input image

// Tugas: ubahlah bagian dibawah ini untuk melakukan operasi blurring terhadap input image

// ----------mulai dari sini------------------------------

for(j=1;j<height-1;j++) {

for(i=0;i<width-1;i++) {

input_image[j][i]=(input_image[j-1][i-1]+input_image[j-1][i]+input_image[j-1]
[i+1]+input_image[j][i-1]+input_image[j][i]+input_image[j][i+1]+input_image[j+1][i-
1]+input_image[j+1][i]+input_image[j+1][i+1])/9; // mengubah nilai pixel

for(j=0;j<height;j++) {

for(i=0;i<width;i++) {

output_image[j][i]=input_image[j][i]; // copy nilai tiap pixel dari input image ke output image
}

// ----------sampai sini----------------------------------

for(j=0;j<height;j++) {

for(i=0;i<width;i++) {

fwrite(&output_image[j][i],sizeof(unsigned char),1,fpo); // tulis pixel satu persatu ke output image

fclose(fpi);

fclose(fpo);

}
HISTOGRAM

#include <stdio.h>

#include <stdlib.h>

main()

FILE *fp;

int i, j, k = 0, height,width,maxval;

char line [100];

unsigned char pixel_value;

int histogram[256]; // Histogram dengan 256 level (0 - 255)

// Masing-masing level kita isi dengan nilai "0"

for (k = 0; k < 256; k++)

histogram[k] = 0;

fp = fopen("mandril.pgm", "r");

fgets(line, 100, fp);

fgets(line, 100, fp);

width=atoi(strtok(line, " "));

height=atoi(strtok(NULL," "));

fgets(line, 100, fp);


maxval=atoi(line);

for (j = 1; j <= height; j++)

for (i = 1; i <= width; i++)

fread(&pixel_value, sizeof(unsigned char), 1, fp);

// Pixel value -> Intensity level

histogram[pixel_value] += 1;

// Masing-masing diprint dengan format:

// <intensity level> \t <pixel count> \n

for (k = 0; k < 256; k++)

printf("%d \t %d \n", k, histogram[k]);

fclose(fp);

You might also like