Nugroho's blog.: Robert Cross Edge Detection using Python on OS X Lion

Saturday, December 17, 2011

Robert Cross Edge Detection using Python on OS X Lion

The code below will convert an image to grayscale, get its pixel value and save it in array. With its pixel value in array, it's easy to apply Robert Cross Edge detect operation.

This operator is simpler than Sobel's.



print 'Program Python Deteksi Tepi'
print 'oleh Nugroho Adi Pramono'
'''Komentar diantara tiga-tanda petik tidak akan dibaca
oleh python'''
#komentar setelah tanda pagar juga tidak di baca oleh python
import Image #memanggil modul untuk olah gambar
import os,sys #memanggil modul untuk mengakses file
import numpy as np #memanggil modul untuk operasi maatematika matrik/array
gb = Image.open('../gambar.jpg') #memasukkan gambar ke variabel gb
print 'format awal: ' ,gb.format, "%dx%d" % gb.size, gb.mode
print 'konversi ke grayscale'
gbw = gb.convert("L")
gbw.save('gambarBW.jpg') #simpan hasil konversi ddg nama gambarBW.jpg
gbw = Image.open('gambarBW.jpg') #masukkan gambar grayscale hasil konversi ke variabel gbw
print 'format: ',gbw.format, "%dx%d" % gbw.size, gbw.mode
ukuran=gbw.size #mengambil nilai resolusi gambar
'''buat array r dan s berukuran sama dengan ukuran gambar'''
r=np.zeros((ukuran[0],ukuran[1]),dtype=np.integer)
s=np.zeros((ukuran[0],ukuran[1]),dtype=np.integer)
print 'Mengambil nilai piksel, masukkan ke array r'
for i in range (ukuran[0]):
for j in range (ukuran[1]):
r[i,j]=gbw.getpixel((i,j))
print 'Deteksi tepi menggunakan operator Robert'
gx=np.zeros((ukuran[0],ukuran[1]),dtype=np.integer)
gy=np.zeros((ukuran[0],ukuran[1]),dtype=np.integer)
g=np.zeros((ukuran[0],ukuran[1]),dtype=np.float)
for i in range (ukuran[0]-1):
for j in range (ukuran[1]-1):
gx[i,j]=r[i,j]-r[i+1,j+1]
gy[i,j]=r[i,j+1]-r[i+1,j]
g=np.sqrt(np.power(gx,2)+np.power(gy,2))
print 'Atur threshold'
for i in range (ukuran[0]-1):
for j in range (ukuran[1]-1):
if (g[i,j]<11):
s[i,j]=0
else:
s[i,j]=255

print 'update gambar'
for i in range (ukuran[0]):
for j in range (ukuran[1]):
gbw.putpixel((i,j),s[i,j]) #letakkan pixel yang telah dimodifikasi ke posisi i,j
print 'Menyimpan gambar'
gbw.save('gambarRobert.jpg') #simpan dengan nama gambarRobert.jpg
print 'Gambar tersimpan'
print 'Program Selesai'

Here, the result

Image source
From python

Gray-scaled image
From python

Edge-detected image
From python

No comments:

323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)