Tuesday, March 16, 2010

March Madness Challenge - MMC16

Inspired by the Arduino meetings I got an older project out of the basement. The Hive has three Propeller chips from Parrallax. I tried to remember Spin, one of the languages you can use to program a Propeller. So I wrote a little program that lets a LED blink.
{{ blink.spin }}

PUB Toggle
dira[24]~~
repeat
!outa[24]
waitcnt(3_000_000 + cnt)

Monday, March 15, 2010

March Madness Challenge - MMC15

Picking up yesterdays topic and checked out inheritance.
#! /usr/bin/python

import math

class Class1:
def __init__(self, name='class1'):
self.name=name
def getName(self):
print self.name

class Class2(Class1):
def hello(self):
print 'class2 hello'

c1 = Class1('class 1')
c2 = Class2('class 2')

c1.getName()
c2.getName()
c2.hello()

Output:
class 1
class 2
class2 hello

Sunday, March 14, 2010

March Madness Challenge - MMC14

Today I wanted to check out the class functionality of python.
#! /usr/bin/python

import math

class classCircle:
def init(self,rad):
self.rad=rad
def radius(self):
print 'Radius: %f' % self.rad
def diameter(self):
diam = self.rad*2
print 'Diameter: %f' % diam
def circumference(self):
circum = self.rad*2*math.pi
print 'Circumference: %f' % circum
def area(self):
area = math.pow(self.rad,2)*math.pi
print 'Area: %f' % area
def info(self):
self.radius()
self.diameter()
self.circumference()
self.area()

circle = classCircle()
circle.init(5)
circle.info()

Output:
Radius: 5.000000
Diameter: 10.000000
Circumference: 31.415927
Area: 78.539816

March Madness Challenge - MMC13

Normally I work on stuff like that later at night. But we lost power from Saturday 4 pm to Sunday morning. So no program for Saturday.

Friday, March 12, 2010

March Madness Challenge - MMC12

#! /usr/bin/python

print "I'm so tired I'm going to bed now!"


BTW: It's 8:37 PM right now, the posting times are some king of wrong!

Thursday, March 11, 2010

March Madness Challenge - MMC11

The problem from yesterdays post was that it is using multiplication. I replaced the Laplace filter with a Rank filter. The result is much better and the program runs 40 seconds instead of 5 Minutes. See yourself...



The result looks like this. The result of displaying the edges is much better.


#! /usr/bin/python

from PIL import Image
from numpy import matrix

import matplotlib.pyplot as plt
import sys
import os
import shutil

outfile = os.path.splitext(sys.argv[1])[0] + 'Rank.jpg'
shutil.copy(sys.argv[1], outfile)

img = Image.open(sys.argv[1])
pix = img.load()

width = img.size[0]
height = img.size[1]

width -= 1
height -= 1

out = Image.open(outfile)
outpix = out.load()

# loop thru image
for x in range(1,width,1):
for y in range(1,height,1):
# do calculation
pixellist = []
for mx in range(-1,2,1):
for my in range(-1,2,1):
if mx!=0 and my!=0:
pixellist.append(pix[x+mx,y+my])
pixellist.sort()
val = pixellist[len(pixellist)-1]-pixellist[0]
if val > 255:
val = 255
if val < 0:
val = 0

outpix[x,y] = val

out.show()
out.save(outfile, 'JPEG')

Wednesday, March 10, 2010

March Madness Challange - MMC10

Today I created and applied a Laplace filter. The Laplace filter detects edges in an image. The program is slow, it runs about five minutes for my test image, so there is room for improvements.


You have to open it if you want to see the details!



I applied a Threshold of 30, so you be able to see a little bit more.

 
1 #! /usr/bin/python
2
3 from PIL import Image
4 from numpy import matrix
5
6 import matplotlib.pyplot as plt
7 import sys
8 import os
9 import shutil
10
11 outfile = os.path.splitext(sys.argv[1])[0] + 'Laplace.jpg'
12 shutil.copy(sys.argv[1], outfile)
13
14 # Laplace Filter
15 lp = matrix([[0,-1,0],[-1,4,-1],[0,-1,0]])
16
17 # Hot Spot
18 hs = matrix([[0,0,0],[0,0,0],[0,0,0]])
19
20 img = Image.open(sys.argv[1])
21 pix = img.load()
22
23 width = img.size[0]
24 height = img.size[1]
25
26 width -= 1
27 height -= 1
28
29 out = Image.open(outfile)
30 outpix = out.load()
31
32 # loop thru image
33 foor x in range(1,width,1):
34 for y in range(1,height,1):
35 # do calculation
36 sum = 0
37 for mx in range(-1,2,1):
38 for my in range(-1,2,1):
39 sum += pix[x+mx,y+my]*lp[mx+1,my+1]
40 if sum > 255:
41 sum = 255
42 if sum < 0:
43 sum = 0
44 # if sum > 30:
45 # sum = 255
46
47 outpix[x,y] = sum
48
49 #out.show()
50 out.save(outfile, 'JPEG')