You are on page 1of 8

14 Januari 2011

Pengantar Image Processing


- Pendahuluan -

Dr. Anto Satriyo Nugroho


Center for Information & Communication Technology, Agency for the Assessment & Application of Technology (PTIK-BPPT) Email: asnugroho@gmail.com URL: http://asnugroho.net

Persiapan Praktikum
OS: Linux Ubuntu Editor: vi, emacs, gedit C Compiler: gcc imagemagick display convert xgraph

Portable Gray Map


Isi sebuah PGM file adalah sbb. A "magic number" for identifying the file type. A pgm image's magic number is the two characters "P5". Whitespace (blanks, TABs, CRs, LFs). A width, formatted as ASCII characters in decimal. Whitespace. A height, again in ASCII decimal. Whitespace. The maximum gray value (Maxval), again in ASCII decimal. Must be less than 65536, and more than zero. A single whitespace character (usually a newline). A raster of Height rows, in order from top to bottom. Each row consists of Width gray values, in order from left to right. Each gray value is a number from 0 through Maxval, with 0 being black and Maxval being white. Each gray value is represented in pure binary by either 1 or 2 bytes. If the Maxval is less than 256, it is 1 byte. Otherwise, it is 2 bytes. The most significant byte is first.

http://netpbm.sourceforge.net/doc/pgm.html

Contoh: PGM file


Sebuah citra memiliki ukuran 4 kolom x 3 baris P5 4 3 255 0 0 255 0 255 255 0 128 0 0 255 0

Header (area yang berwarna hijau): terdiri dari 3 baris yang tertulis dalam plain text ASCII. Masing-masing adalah magick number (P5), lebar tinggi, dan nilai maksimum intensitas Raster (area yang berwarna kuning): berisi data nilai intensitas masingmasing pixel dalam format BINARY. Dimulai dari pixel paling kiri atas menuju ke kanan bawah. 0 adalah hitam, sedangkan 255 adalah putih.
4

Contoh membuat sebuah citra


#include #include <stdio.h> <stdlib.h>

main() { FILE *fp; int height=3, width=4; unsigned char putih=255, hitam=0, abu=128; fp=fopen(contoh-1.pgm,w); fprintf(fp,P5\n%d %d\n255\n,width,height); fwrite(&hitam,sizeof(unsigned char),1,fp); fwrite(&hitam,sizeof(unsigned char),1,fp); fwrite(&putih,sizeof(unsigned char),1,fp); fwrite(&hitam,sizeof(unsigned char),1,fp); fwrite(&putih,sizeof(unsigned char),1,fp); fwrite(&putih,sizeof(unsigned char),1,fp); fwrite(&hitam,sizeof(unsigned char),1,fp); fwrite(&abu,sizeof(unsigned char),1,fp); fwrite(&hitam,sizeof(unsigned char),1,fp); fwrite(&hitam,sizeof(unsigned char),1,fp); fwrite(&putih,sizeof(unsigned char),1,fp); fwrite(&hitam,sizeof(unsigned char),1,fp); fclose(fp); }

Bagaimana membaca sebuah citra ?


#include #include <stdio.h> <stdlib.h>

main() { FILE *fp; int i,j, height=3, width=4; char line[100]; unsigned char pixel_value;

fp=fopen(contoh-1.pgm,r); fgets(line,100,fp); fgets(line,100,fp); fgets(line,100,fp);


for(j=0;j<height;j++) for(i=0;i<width;i++) { fread(&pixel_value,sizeof(unsigned char),1,fp); printf(baris ke %d kolom ke %d nilai pixel=%d\n,j,i,pixel_value); } fclose(fp); }
6

Latihan-1
Buatlah program untuk menampilkan citra:

radius 25

tinggi 75

Lebar 100
7

Latihan-2
Buatlah program untuk menampilkan nilai tiap pixel pada citra berikut (ukuran citra: 512x512)

You might also like