comp 204
play

COMP 204 Introduction to image analysis with scikit-image (part - PowerPoint PPT Presentation

COMP 204 Introduction to image analysis with scikit-image (part three) Mathieu Blanchette, based on slides from Christopher J.F. Cameron and Carlos G. Oliver 1 / 17 Edge detection Goal: Identify regions of the image that contain sharp changes


  1. COMP 204 Introduction to image analysis with scikit-image (part three) Mathieu Blanchette, based on slides from Christopher J.F. Cameron and Carlos G. Oliver 1 / 17

  2. Edge detection Goal: Identify regions of the image that contain sharp changes in colors/intensities. Why? Useful for ◮ delineating objects (image segmentation) ◮ recognizing them (object recognition) ◮ etc. 2 / 17

  3. Edge detection 3 / 17

  4. Edge detection 4 / 17

  5. Edge detection What’s an edge in an image? Horizontal edge at row i : ◮ image [ i − 1 , j ] is very different from image [ i + 1 , j ] Vertical edge at column j : ◮ image [ i , j − 1] is very different from image [ i , j + 1] Idea: To determine if an RGB pixel ( i , j ) belongs to an edge: For each color ∈ { R , G , B } : ◮ L x [ color ] = image [ i , j − 1 , color ] − image [ i , j + 1 , color ] ◮ L y [ color ] = image [ i − 1 , j , color ] − image [ i + 1 , j , color ] L x [ color ] 2 + L y [ color ] 2 � ◮ gradient[color] = gradient [ R ] 2 + gradient [ G ] 2 + gradient [ B ] 2 � edginess = if edginess > some threshold, then pixel ( i , j ) belongs to an edge 5 / 17

  6. Edge detection 1 def d e t e c t e d g e s ( im , m i n g r a d i e n t =50) : ””” 2 Args : 3 im : The image on which to d e t e c t edges 4 m i n g r a d i e n t : The minimum g r a d i e n t v a l u e 5 f o r a p i x e l to be c a l l e d an edge 6 Returns : An new image with edge p i x e l s s e t to white , 7 and e v e r y t h i n g e l s e s e t to black 8 ””” 9 n row , n col , c o l o r s = image . shape 10 11 # c r e a t e a empty empty of the same s i z e as the o r i g i n a l 12 edge image = np . z e r o s ( ( n row , n col , 3 ) , dtype=np . u i n t 8 ) 13 14 f o r i i n range (1 , n row − 1) : # avoid the f i r s t / l a s t row 15 f o r j i n range (1 , n col − 1) : # and f i r s t / l a s t c o l 16 grad =[0 ,0 ,0] 17 f o r c i n range (3) : # f o r each c o l o r 18 Lx = f l o a t ( im [ i − 1, j , c ] ) − f l o a t ( im [ i +1, j , c ] ) 19 Ly = f l o a t ( im [ i , j − 1,c ] ) − f l o a t ( im [ i , j +1,c ] ) 20 grad [ c ] = math . s q r t ( Lx ∗∗ 2+Ly ∗∗ 2) 21 norm = math . s q r t ( grad [ 0 ] ∗ ∗ 2 + grad [ 1 ] ∗ ∗ 2 \ 22 + grad [ 2 ] ∗ ∗ 2 ) 23 i f ( norm > m i n g r a d i e n t ) : 24 edge image [ i , j ] = (255 ,255 ,255) 25 6 / 17 r e t u r n edge image 26

  7. Analysis of microscopy images Cells (purple ”circles”) are infected by Plasmodium falciparum (small red dots). 7 / 17

  8. Edge detection (threshold = 60) 8 / 17

  9. Edge detection (threshold = 120) 9 / 17

  10. Edge detection Skimage has many edge detection algorithms: http://scikit-image.org/docs/0.5/auto_examples/plot_ canny.html 10 / 17

  11. Counting/annotating cells What if we want to automatically identify/count cells in the image? Idea: 1. Find edges in the image 2. Identify closed (encircled) shapes within the edge image to Each closed shape is assigned a different color. Number of closed shapes ( = approximation to cell count) is calculated. 11 / 17

  12. Seed filling algorithm How to take an edge image and fill-in each closed shape? Seed filling (aka flood filling) algorithm: ◮ Start from a black pixel. ◮ Color it and expand to its neighboring pixel, unless neighbor is an edge (white). ◮ Keep expanding until no more expansion is possible ◮ Repeat from a new starting point, until no black pixels are left 12 / 17

  13. Seed filling algorithm Oops: I’ve swapped black and white!... Black = edge, white = background Seed = pixel at position (4,4) 2 3 2 3 2 3 8 1 1 4 1 4 1 4 9 5 6 7 5 6 7 5 6 7 Front: ¡[1] Front: ¡[2, ¡3, ¡4, ¡5, ¡6, ¡7] Front: ¡[3, ¡4, ¡5, ¡6, ¡7] Front: ¡[4, ¡5, ¡6, ¡7,8, ¡9] 2 3 8 2 3 8 2 3 8 2 3 8 1 4 9 1 4 9 1 4 9 1 4 9 5 6 7 10 5 6 7 10 5 6 7 10 5 6 7 10 11 12 11 12 13 11 12 13 14 Front: ¡[5, ¡6, ¡7, ¡8, ¡9, ¡10] Front: ¡[6, ¡7, ¡8, ¡9, ¡10, ¡11, ¡12] Front: ¡[7, ¡8, ¡9, ¡10, ¡11, ¡12, ¡13] Front: ¡[8, ¡9, ¡10, ¡11, ¡12, ¡14] 2 3 8 2 3 8 2 3 8 2 3 8 15 15 15 15 1 4 9 1 4 9 1 4 9 1 4 9 16 16 16 16 5 6 7 10 5 6 7 10 5 6 7 10 5 6 7 17 17 10 17 11 12 13 14 11 12 13 14 11 12 13 14 18 11 12 13 14 18 19 20 Front: ¡[ ¡9, ¡10, ¡11, ¡12, ¡14, ¡15, ¡16] Front: ¡[10, ¡11, ¡12, ¡14, ¡15, ¡16, ¡17] Front: ¡[11, ¡12, ¡14, ¡15, ¡16, ¡17, ¡18] Front: ¡[12, ¡14, ¡15, ¡16, ¡17, ¡18, ¡19, ¡20] 13 / 17

  14. Seed filling implementation 1 def s e e d f i l l ( im , seed row , s e e d c o l , f i l l c o l o r , bckg ) : ””” 2 im : The image on which to perform the s e e d f i l l a l g o r i t h m 3 seed row and s e e d c o l : p o s i t i o n of the seed p i x e l 4 f i l l c o l o r : Color f o r the f i l l 5 bckg : Color of the background , to be f i l l e d 6 Returns : Number of p i x e l s f i l l e d 7 Behavior : M o d i f i e s image by performing s e e d f i l l 8 ””” 9 s i z e =0 # keep t r a c k of patch s i z e 10 n row , n col , foo = im . shape 11 f r o n t =[( seed row , s e e d c o l ) ] # i n i t i a l f r o n t 12 w h i l e l e n ( f r o n t ) > 0: 13 r , c = f r o n t . pop (0) # remove 1 s t element of f r o n t 14 # This i s how to t e s t e q u a l i t y of two np . a r r a y s 15 i f np . a r r a y e q u a l ( im [ r , c ] , bckg ) : 16 im [ r , c]= f i l l c o l o r # c o l o r the p i x e l 17 s i z e+=1 18 # look at a l l n e i g h b o r s 19 f o r i i n range (max (0 , r − 1) , min ( n row , r +2)) : 20 f o r j i n range (max (0 , c − 1) , min ( n col , c+2)) : 21 # i f background , add to f r o n t 22 i f np . a r r a y e q u a l ( im [ i , j ] , bckg ) and \ 23 ( i , j ) not i n f r o n t : 24 f r o n t . append (( i , j ) ) 25 14 / 17 r e t u r n s i z e 26

  15. Seeding from all possible starting pixel... 1 f i l e n a m e=” malaria2 ” f i g=p l t . f i g u r e () # i g n o r e t h i s 2 3 image = i o . imread ( f i l e n a m e+” . jpg ” ) 4 5 edge image = d e t e c t e d g e s ( image , 60) 6 i o . imsave ( f i l e n a m e+” edge . jpg ” , edge image ) 7 m i n c e l l s i z e =100 # based on p r i o r knowledge 8 m a x c e l l s i z e =300 # based on p r i o r knowledge 9 n c e l l s =0 10 11 12 f o r i i n range ( edge image . shape [ 0 ] ) : f o r j i n range ( edge image . shape [ 1 ] ) : 13 # i f p i x e l i s black , s e e d f i l l from here 14 i f np . a r r a y e q u a l ( edge image [ i , j , : ] , ( 0 , 0 , 0 ) ) : 15 r a n d c o l o r = ( random . randrange (255) , 16 random . randrange (255) , 17 random . randrange (255) ) 18 s i z e=s e e d f i l l w i t h a n i m a t i o n ( edge image , i , j , 19 r a n d c o l o r , ( 0 , 0 , 0 ) ) 20 i f s i z e > = m i n c e l l s i z e and s i z e < m a x c e l l s i z e : 21 n c e l l s+=1 22 p r i n t ( ”Number of c e l l s : ” , n c e l l s ) # Number of c e l l s : 208 23 15 / 17

  16. Seed filling execution See live execution of program 16 / 17

  17. Issues Several things would need to be improved to get a more accurate cell count: ◮ Some cells are not surrounded by a closed edge because of lack of contrast; those end up not being counted. ◮ In some cells, the nucleus is enclosed by an edge. Those cells often end up not being counted, because both the cytoplasmic and nuclear portions are too small to be called a cell. ◮ Some cells may not be red blood cells, and should not be counted ◮ etc... 17 / 17

Recommend


More recommend