2010_3_Python_Imaging_Library_3.pdf

(934 KB) Pobierz
ImageProcessingandComputer
Graphics
PythonImagingLibrary3
Author:MarekKoci«ski
March2010
1525309315.015.png 1525309315.016.png 1525309315.017.png 1525309315.018.png 1525309315.001.png 1525309315.002.png
1Purpose
To get acquainted with Image filtering using direct access to the pixels. The basic
morphological operations will be applied to binary images.3Dimage will be load into
memory and selected slice will be displayed as2Dimage.
Time
345minutes
2Tasks
1. Open Python interpreter window (Start! Programy! EPD32-6.0.2 ! IDLE)
2. Open new Editor Window (File ! New Window) and write your code into it.
3. Import needed modules, e.g. Image
4. Very often it is needed to get direct access to image data. Function load() returns
a pixel access object that can be used to read and modify pixels. The access object
behaves like a2-dimensional array, so you can do:
pix = im . load ()
print pix [ x , y ]
pix [ x , y ] = value
Create inversion of the brighteness level of the goldhill.bmp image 1.
im = Image . open (" g o l d h i l l .bmp")
im = im . convert ( ‘ ‘L‘ ‘ )
sx = im . s i z e [ 0 ]
sy = im . s i z e [ 1 ]
pix = im . load ()
fory inrange ( sy ) :
forx inrange ( sx ) :
pix [ x , y ] = 255 pix [ x , y ]
im . save ( ’ neg .bmp ’ )
5. Create an function that performs image convolution with33masks. (Hint:
1
1525309315.003.png 1525309315.004.png
(a) Gray-level image
(b) Inverted gray-level image
Figure 1: Pixel operations
def convolution2d (img , k , s i z e =3):
"""This function make image convolutin with cernel .
Kernel as a l i s t from 0 to 8. """
fn=" convolution2d "
print "Function %s" %fn
pix = img . load ()
if s i z e == 3:
print "Kernel s i z e = 3"
sx = img . s i z e [ 0 ]
sy = img . s i z e [ 1 ]
nimg = Image . new (img . mode , img . s i z e )
npix = nimg . load ()
val =[]
fory inrange (1 , sy1):
forx inrange (1 , sx1):
npix [ x , y]= (k [ 0 ] pix [ x1,y1] + k [ 1 ] pix [ x , y1]
+ k [ 2 ] pix [ x+1,y1] + k [ 3 ] pix [ x1,y ]
+ k [ 4 ] pix [ x , y ] + k [ 5 ] pix [ x+1,y ]
+ k [ 6 ] pix [ x1,y+1] + k [ 7 ] pix [ x , y+1]
+ k [ 8 ] pix [ x+1,y+1])
returnnimg
Load image blood1.bmp and perform convolution with Prewitt masks:
2
1525309315.005.png 1525309315.006.png
Figure 2: Edge detection procedure (from ImageProcessinglectures by P.Strumiłło and
M.Strzelecki)
2
3
2
3
1 1 1
0 0 0
1 1 1
101
101
101
6 4
7 5
6 4
7 5
(a) Prewitt (horiz.) — k1
(b) Prewitt (vertic.) — k3
Pass convolution cernels as the function argument in the following convetnion:
k1=[1;1;1;0;0;0;1;1;1]
k3=[1;0;1;1;0;1;1;0;1]
Find edges of the image using procedure showed in the Fig. 2. Find appropriate
threshold value (Fig. 3).
Add booth resulting images using pixel operations.
a1 = Image . new(im . mode , im . s i z e )
pix_a1_v = a1_v . load ()
pix_a1_h = a1_h . load ()
pix_a1 = a1 . load ()
fory inrange ( sy ) :
forx inrange ( sx ) :
pix_a1 [ x , y ] = (pix_a1_h [ x , y ] + pix_a1_v [ x , y ] )/2
6. Load images bin1.bmp and bin2.bmp. For both images apply function dilation
dilaet2D() and erosion erode2D(). Use dierent structuring element (Fig. 4). Define
each element as tupe or list, for example first element, which is cross shapled is
defined as followes:
se1=(0;1;0;1;1;1;0;1;0;)
Use at least6dierent structuring elements (Fig. 5).
3
1525309315.007.png 1525309315.008.png 1525309315.009.png 1525309315.010.png
 
(a) Prewitt (vert.)
(b) Prewitt (horiz.)
(c) Sum of gradients
(d) After thresholding
Figure 3: Edge detection using Prewitt mask
def dilate2D (im , se , sh =1):
""" dilate2D function i s to do dilation of img image (2D) .
se tuple describes any FULL SIZE se .
se (n) ON piksels
Dilation for WHITE objects with BLACK background ! ! ! """
fn=" dilate2D (im , se )"
if( sh ) :print "Function %s" %fn
sX , sY = im . s i z e
imD = im . load ()
nim = Image . new (im . mode , im . s i z e )
nimD= nim . load ()
seX = 3 #size of se
seY = 3
hx = seX/2
hy = seY/2
fory inrange (hy , sYhy ) :
forx inrange (hx , sXhx ) :
if( imD[ x , y ] == 255 ) :
if( se [ 0 ] ) : nimD[ x1,y1] =255
if( se [ 1 ] ) : nimD[ x , y1] =255
if( se [ 2 ] ) : nimD[ x+1,y1] =255
if( se [ 3 ] ) : nimD[ x1,y ] =255
if( se [ 4 ] ) : nimD[ x , y ] =255
if( se [ 5 ] ) : nimD[ x+1,y ] =255
if( se [ 6 ] ) : nimD[ x1,y+1] =255
if( se [ 7 ] ) : nimD[ x , y+1] =255
if( se [ 8 ] ) : nimD[ x+1,y+1] =255
4
1525309315.011.png 1525309315.012.png 1525309315.013.png 1525309315.014.png
Zgłoś jeśli naruszono regulamin