Nugroho's blog.

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

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


JPEG Support for Python 2.7's PIL Module on OS X Lion

My PIL module refuse to process jpeg image. It says that there is no decoder jpeg on my system. It's no good  since I want to process that kind of file using PIL in future, and my older iPhoto picture on my 13 inch MacBook Pro is generally in jpeg format too.

Here the snapshot


Nugrohos-MacBook-Pro:olah gambar nugroho$ python olahgambar.py 
Program Image Enchancement
Oleh Nugroho Adi Pramono
275514
===========================
format awal: JPEG 2448x3264 RGB
konversi ke grayscale
Traceback (most recent call last):
File "olahgambar.py", line 14, in
gbw = gb.convert("L")
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL/Image.py", line 679, in convert
self.load()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL/ImageFile.py", line 189, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL/Image.py", line 385, in _getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available
Nugrohos-MacBook-Pro:olah gambar nugroho$

So I googling for it and found libjpeg at http://www.ijg.org/files/, download, exctract and install it using magic UNIX words ./configure, make, make install

Nugrohos-MacBook-Pro:~ nugroho$ cd Downloads/jpeg-8c/
Nugrohos-MacBook-Pro:jpeg-8c nugroho$ ./configure
Nugrohos-MacBook-Pro:jpeg-8c nugroho$ make
Nugrohos-MacBook-Pro:jpeg-8c nugroho$ make test
rm -f testout*
./djpeg -dct int -ppm -outfile testout.ppm ./testorig.jpg
./djpeg -dct int -bmp -colors 256 -outfile testout.bmp ./testorig.jpg
./cjpeg -dct int -outfile testout.jpg ./testimg.ppm
./djpeg -dct int -ppm -outfile testoutp.ppm ./testprog.jpg
./cjpeg -dct int -progressive -opt -outfile testoutp.jpg ./testimg.ppm
./jpegtran -outfile testoutt.jpg ./testprog.jpg
cmp ./testimg.ppm testout.ppm
cmp ./testimg.bmp testout.bmp
cmp ./testimg.jpg testout.jpg
cmp ./testimg.ppm testoutp.ppm
cmp ./testimgp.jpg testoutp.jpg
cmp ./testorig.jpg testoutt.jpg
Nugrohos-MacBook-Pro:jpeg-8c nugroho$ sudo make install

reinstall PIL

PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version 1.1.7
platform darwin 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
--------------------------------------------------------------------
--- TKINTER support available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
*** FREETYPE2 support not available
*** LITTLECMS support not available
--------------------------------------------------------------------
To add a missing option, make sure you have the required
library, and set the corresponding ROOT variable in the
setup.py script.

Friday, December 16, 2011

Ubuntu 11.10 Oneiric Ocelot On OS X Lion's VBox (NAT and Host-Guest Sharing Folder)

After installing Ubuntu 11.10 on Mac OS X Lion using VirtualBox, here first impression of Oneiric Ocelot



There is dock at left, like on Mac.

Network is works flawlessy, I can surf internet using firefox, its NAT configuration come by default while installing it.
From Oneiric Ocelot

Firefox run at fullscreen(I mean ubuntu screen), just like Lion's or iPad's Safari (Lion's version of firefox didn't auto fullscreen yet). We can windowed-mode-ed it by hovering mouse at top edge of screen, it'll show menu-bar too

Hover mouse at top
From Oneiric Ocelot
It's windowed firefox

From Oneiric Ocelot
 If we click dash home, we'll got launchpad like (just like iPad or Lion's launchpad). At second tab, there's apps available to download.

From Oneiric Ocelot

 To access shared folder on Lion named vbox, I opened terminal by clicking Dash home and type terminal on search box
From Oneiric Ocelot

 First create directory
aravir@aravir-VirtualBox:~$mkdir vbox

mount shared vbox on lion using this command
aravir@aravir-VirtualBox:~$sudo mount -t vboxsf vbox vbox 

at screenshot you know it generate error because ubuntu did'nt recognize vboxsf filesystem. To introduce it, guest addition must be installed.
From Oneiric Ocelot
From Oneiric Ocelot
From Oneiric Ocelot
From Oneiric Ocelot
Failed. Guest addition need root privilege.
aravir@aravir-VirtualBox:~$cd /Media/VBOXADDITIONS_4.1.6_74713
$sudo ./VBoxLinuxAdditions.run

After guest addition installed and the system restarted (it's support full screen on my Macbook monitor now), retype the command above, and done

 Vbox directory before mounted
From Oneiric Ocelot
After mounted
From Oneiric Ocelot

Thursday, December 15, 2011

Installing Python-2.7's Imaging Library Module on OS X Lion

PIL, as its name suggest, is an imaging library for python. I used PIL to get every pixel of an image as 2x2 array information. With that I could modify it with many possibilities; edge detection, black and white transformation, log transformation, creating watermark, etc.

{update: I install this PIL module on my OS X El Capitan too, :), and easier



Installing PIL is an easy task, if we have its prerequities installed. Just download it and extract it and then install using this command

Nugrohos-MacBook-Pro:Imaging-1.1.7 nugroho$ python setup.py install
running install
running build
running build_py
creating build
creating build/lib.macosx-10.6-intel-2.7
...
unable to execute gcc-4.2: No such file or directory
error: command 'gcc-4.2' failed with exit status 1
Nugrohos-MacBook-Pro:Imaging-1.1.7 nugroho$
Oops, it is failed to install, it tell me that there is no gcc-4.2 on my machine, and that's true. I've modified my /usr/bin/gcc to point /usr/local/bin/gcc.

I have gcc 4.2 from Xcode4.2, which actually llvm-gcc. My existing gcc is gcc-4.6.2 so I cheated… I created symlink named gcc-4.2 on /usr/bin pointing /usr/local/bin/gcc

Nugrohos-MacBook-Pro:Imaging-1.1.7 nugroho$ ln -s /usr/local/bin/gcc /usr/bin/gcc-4.2
ln: /usr/bin/gcc-4.2: Permission denied
Nugrohos-MacBook-Pro:Imaging-1.1.7 nugroho$ sudo ln -s /usr/local/bin/gcc /usr/bin/gcc-4.2
Nugrohos-MacBook-Pro:Imaging-1.1.7 nugroho$

Still error, :(

gcc-4.2: error: x86_64: No such file or directory
gcc-4.2: error: unrecognized option ‘-arch’
gcc-4.2: error: unrecognized option ‘-arch’
error: command 'gcc-4.2' failed with exit status 1

Hm, maybe if I'm linking /usr/bin/gcc-4.2 to /usr/bin/llvm-gcc-4-2; it's gcc-4.2 after all, if we ignore llvm (don't know what is it stand for). It doesn't hurt if I'm trying, so..
Nugrohos-MacBook-Pro:Imaging-1.1.7 nugroho$ sudo rm /usr/bin/gcc-4.2 
Password:
Nugrohos-MacBook-Pro:Imaging-1.1.7 nugroho$ sudo ln -s /usr/bin/llvm-gcc-4.2 /usr/bin/gcc-4.2
Nugrohos-MacBook-Pro:Imaging-1.1.7 nugroho$ python setup.py install
running install
running build
running build_py
running build_ext
-
--------------------------------------------------------------------
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version 1.1.7
platform darwin 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
--------------------------------------------------------------------
--- TKINTER support available
*** JPEG support not available
--- ZLIB (PNG/ZIP) support available
*** FREETYPE2 support not available
*** LITTLECMS support not available
--------------------------------------------------------------------
creating /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL.pth
Nugrohos-MacBook-Pro:Imaging-1.1.7 nugroho$

Wow, success 

Well, no need gcc4.6.2 afterall, just create symbolic link of llvm-gcc-4.2 named gcc-4.2,:)

test

Nugrohos-MacBook-Pro:Imaging-1.1.7 nugroho$ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import image
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named image
>>> import Image
>>>

OK

Install GCC 4.6.2 on OS X Lion (success)

Still failed, for not able to access gcc-4.6.2 automatically (have to access ~/gcc462/bin), I install binary GCC from HPC. So I'm extracted gcc-lion.tar.gz. For my surprise, it's extracted with some hierarchy like usr/local/bin, usr/local/include, … and I supposed to copy it to root folder. In that case, my Xcode4.2's gcc would still intact because it's in /usr/bin .

 I wonder if all gcc compilation result'll stored to /usr/local if we didn't pass any parameter, so I checked my /usr/local/bin directory



Nugrohos-MacBook-Pro:/ nugroho$ cd usr/local/bin/
Nugrohos-MacBook-Pro:bin nugroho$ ls
2to3gfortran python-config smtpd.py
2to3-2.7 idle python2.7 smtpd2.7.py
c++ idle2.7 python2.7-32 x86_64-apple-darwin11.2.0-c++
cpp jcf-dump python2.7-config x86_64-apple-darwin11.2.0-g++
g++ pydoc pythonw x86_64-apple-darwin11.2.0-gcc
gcc pydoc2.7 pythonw-32 x86_64-apple-darwin11.2.0-gcc-4.6.2
gcj python pythonw2.7 x86_64-apple-darwin11.2.0-gcj
gcov python-32 pythonw2.7-32 x86_64-apple-darwin11.2.0-gfortran

There is gcc in it. Is in gcc-4.2?
Nugrohos-MacBook-Pro:bin nugroho$ ./gcc --version
gcc (GCC) 4.6.2
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Yes, it is gcc-4.6.2? So, I replace gcc symlink previously  pointed to /usr/bin/llvm-gcc to /usr/local/bin/gcc

Nugrohos-MacBook-Pro:bin nugroho$ which gcc
/usr/bin/gcc
Nugrohos-MacBook-Pro:bin nugroho$ which llvm-gcc
/usr/bin/llvm-gcc
Nugrohos-MacBook-Pro:bin nugroho$ sudo ln -s gc
gcc gcj gcov
Nugrohos-MacBook-Pro:bin nugroho$ sudo ln -s gcc /usr/bin/gcc
Password:
ln: /usr/bin/gcc: File exists
Nugrohos-MacBook-Pro:bin nugroho$ sudo mv /usr/bin/gcc /usr/bin/gccOLD
Nugrohos-MacBook-Pro:bin nugroho$ sudo ln -s gcc /usr/bin/gcc
Nugrohos-MacBook-Pro:bin nugroho$ gcc
gcc: fatal error: no input files
compilation terminated.
Nugrohos-MacBook-Pro:bin nugroho$ gcc --version
gcc (GCC) 4.6.2
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Nugrohos-MacBook-Pro:bin nugroho$
Hooray, HPC binary is left untouched, :)

Ubuntu 11.10 Oneiric Ocelot on OS X Lion via Virtual Box

It's long time since my MacBook Pro had ubuntu installed. Curious about new release of ubuntu, I decided to download .iso file via torrent network and installed it on my Mac via VirtualBox as I don't want to dual booting my Mac again (the way I did several years ago).



 I downloaded 64-bit version of Oneiric Ocelot and it's installed without problem.

 From screenshot below, we know ubuntu team has improved their installation method. While the system is busy copying file, we're prompted to fill our detail of instalation; keyboard, time zone, username. Such an efficient method.
From Blogsy Photos
From Blogsy Photos
From Blogsy Photos
From Blogsy Photos
From Blogsy Photos
From Blogsy Photos
From Blogsy Photos
From Blogsy Photos
From Blogsy Photos
From Blogsy Photos
From Blogsy Photos
From Blogsy Photos
From Blogsy Photos

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)