Monday, March 8, 2010

March Madness Challenge - MMC08

Image processing at University was not always fun. Today I tried to create a histogram of a gray scale image. I wanted to use an array that has 256 elements. Then I wanted to loop thru the whole image, use the value as index and increase by one in the 256 array. But the histogram function does not like tuples I guess. So this python script is totally inefficient, because it creates an array that has x*y values between 0 and 255 instead of 256 values.

1 #! /usr/bin/python
2
3 from PIL import Image
4 import matplotlib.pyplot as plt
5 import sys
6
7 histo = []
8
9 """for i in range(0,256,1):
10 histo.append([i,0])"""
11
12 img = Image.open(sys.argv[1])
13 pix = img.load()
14
15 width = img.size[0]
16 height = img.size[1]
17
18 for x in range(0,width,1):
19 for y in range(0,height,1):
20 #histo[pix[x,y]][1] += 1
21 histo.append(pix[x,y])
22
23 plt.hist(histo,256)
24 plt.xlabel('Grayvalue')
25 plt.ylabel('Frequency')
26 plt.title('Histogram %s' % sys.argv[1])
27 plt.grid(True)
28 plt.show()
29



No comments:

Post a Comment