Nugroho's blog.: Vertical Sobel Operator Manual Edge Detection using Python and PIL

Saturday, December 17, 2011

Vertical Sobel Operator Manual Edge Detection using Python and PIL

This python code is used to detect the edge, of course, using Sobel Operator. The 'manual' word in this post title means I apply it manually as scipy python module has it capability. This code convert a jpeg image to grayscale and then detect its edge.



Here the code, I used vertical detection only as my horizontal code result is scrambling. I will update it as soon as all code success

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 Sobel'
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 (1,ukuran[0]-1):
for j in range (1,ukuran[1]-1):
gy[i,j]=r[i+1,j-1]+2*r[i+1,j]+r[i+1,j+1]-r[i-1,j-1]-2*r[i-1,j]-r[i-1,j+1]
print 'Atur threshold'
print 'update gambar Vertikal'
for i in range (ukuran[0]):
for j in range (ukuran[1]):
gbw.putpixel((i,j),np.abs(gy[i,j])) #letakkan pixel yang telah dimodifikasi ke posisi i,j
print 'Menyimpan gambar Vertikal'
gbw.save('gambarSobelVertikal.jpg') #simpan dengan nama gambarSobelVertikal.jpg
print 'Gambar tersimpan'
print 'Program Selesai'
And 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)