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')

No comments:

Post a Comment