Showing posts with label pylab. Show all posts
Showing posts with label pylab. Show all posts

Friday, March 01, 2013

[PCVP] Gaussian blurring and a minor imshow problem

As mentioned in the previous post (To-Do List of 2013), I am trying to learn something by following the examples listed in Programming Computer Vision with Python (PCVP).

I've done some exercises without any systematic or consistent record. So I think it's time to post some results here to record my own understanding as well as to push myself to keep going forward.

The exercise here is a test of Gaussian blurring given in pp.31-32 of the PCVP book. The following is my code and output images.


from PIL import Image
from PIL import Image
import numpy as np
from scipy.ndimage import filters
import matplotlib.pylab as plt 

img = Image.open('../data/empire.jpg')
im0 = np.array(img) #original image
im1 = np.array(img.convert('L')) #convert to grayscale
im2 = filters.gaussian_filter(im0,2)
im3 = filters.gaussian_filter(im0,5)
im4 = filters.gaussian_filter(im0,10)

for i in range(5):
    plt.subplot(1,5,i+1).set_axis_off()
    plt.title( '(' + str(unichr(97+i)) + ')' )
    #plt.imshow(eval('im'+str(i))) # This will output the grayscale image with colors 
    plt.imshow(eval('im'+str(i)), cmap=plt.cm.Greys_r)

plt.show()


From left to right: (a) original image, (b) grayscale image, (c)  Gaussian filter with σ = 2, (d) σ = 5, (e) σ = 10.

Actually, the Gaussian blurring has no problems but the convert('L') and the imshow() functions bothered me for a while. I expected the imshow() function will gave me a grayscale image as the one shown in the PCVP book, but it gave me an image with colors as the second one listed in the following figure.


The solution I found [1] and tested is to add a cmap option to tell the imshow() which colormap [2] I want it to apply on the output image.

---
Ref:
[1] Display image as grayscale using matplotlib
[2] Matplotlib Color Maps

Friday, March 05, 2010

[Py] Save the figure before show it

This might be a simple and trivial tips, but I didn't know it till today. I occasionally got a blank figure saved by matplotlib's savefig function and had no idea about that strange result. Then I decided to test it with simple Python code.

My test code:
--------------------------------------------
1 from pylab import *
2

3
y = []
4
for x in range(0,10):

5
y.append(x*x)

6

7
plot(range(0,10), y)

8
savefig('fig_saved.png')

9
show()

--------------------------------------------

After running the above code, you should get the foll0wing figure saved by using savefig() function.But if you exchange line 8 and line 9, you get nothing but a totally blank figure.

Sunday, February 28, 2010

[Py] Random data generation -- a trial

I am studying the usage of LIBSVM recently. What I want is to test the library with some simple data to enhance my understanding for using the library. Therefore, I thought about generating some random data sets for my own learning purpose, and of course, the first choice came into me was Python.

The following are my code and the resulting data plot.
---------------------------------------------
Code:
from pylab import *
from numpy import *

data =[]
#generate random data sets
data.append( 1.2*random.randn(2,30) )
data.append( 2.0*random.randn(2,30) )
data.append( random.randn(2,30) )

#shift the data sets
x0 = data[0][0]; y0 = data[0][1]
x1 = data[1][0]+3.5; y1 = data[1][1]+2.5
x2 = data[2][0]-1.5; y2 = data[2][1]+4.5

f = open('randData','w')
for i in range(len(x0)):
print >> f, '%f %f %f %f %f %f'\
% (x0[i], y0[i], x1[i], y1[i], x2[i], y2[i])

f.close()

plot(x0, y0, 'ro', x1, y1, 'go', x2, y2, 'bo')
savefig('randData.png')
show()


Tuesday, September 15, 2009

[Py] Python: array and list

When I tested some basic functions in iPython, I noticed the differences between array and list.

At the beginning, I thought that if there are two matrices matA and matB in the same dimension, then I can just add their corresponding elements with ``matA + matB.'' By doing some tests, however, I found it's not what I expected.

>>> matA = [[1, 2], [3, 4]]
>>> matA
[[1, 2], [3, 4]]
>>> matB = [[-1, -2], [-3, -4]]
>>> matB
[[-1, -2], [-3, -4]]
>>> matC = matA + matB
>>> matC
[[1, 2], [3, 4], [-1, -2], [-3, -4]]

The plus operator concentrated matA and matB, and this was not what I needed.

Importing the ``array'' module still cannot let me do things I wanted.

>>> import array
>>> matA = array.array("i", [1, 2, 3, 4])
>>> matA
array('i', [1, 2, 3, 4])
>>> matB = array.array("i", [-1, -2, -3, -4])
>>> matC = matA + matB
>>> matC
array('i', [1, 2, 3, 4, -1, -2, -3, -4])

The solution is ``to use SciPy.'' Yes, if you want to manipulate matrices with Python more intuitively, remember to import scipy.

>>> from scipy import *
>>> from pylab import *
>>> matA = array([[1, 2], [3, 4]])
>>> matA
array([[1, 2],
[3, 4]])
>>> matB = array([[-1, -2], [-3, -4]])
>>> matB
array([[-1, -2],
[-3, -4]])
>>> matC = matA + matB
>>> matC
array([[0, 0],
[0, 0]])