<s> import matplotlib . delaunay as md <EOL> from scipy import ndimage <EOL> from pylab import * <EOL> from numpy import * <EOL> from PCV . geometry import homography <EOL> def image_in_image ( im1 , im2 , tp ) : <EOL> """<STR_LIT>""" <EOL> m , n = im1 . shape [ : <NUM_LIT:2> ] <EOL> fp = array ( [ [ <NUM_LIT:0> , m , m , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , n , n ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> H = homography . Haffine_from_points ( tp , fp ) <EOL> im1_t = ndimage . affine_transform ( im1 , H [ : <NUM_LIT:2> , : <NUM_LIT:2> ] , <EOL> ( H [ <NUM_LIT:0> , <NUM_LIT:2> ] , H [ <NUM_LIT:1> , <NUM_LIT:2> ] ) , im2 . shape [ : <NUM_LIT:2> ] ) <EOL> alpha = ( im1_t > <NUM_LIT:0> ) <EOL> return ( <NUM_LIT:1> - alpha ) * im2 + alpha * im1_t <EOL> def combine_images ( im1 , im2 , alpha ) : <EOL> """<STR_LIT>""" <EOL> return ( <NUM_LIT:1> - alpha ) * im1 + alpha * im2 <EOL> def alpha_for_triangle ( points , m , n ) : <EOL> """<STR_LIT>""" <EOL> alpha = zeros ( ( m , n ) ) <EOL> for i in range ( min ( points [ <NUM_LIT:0> ] ) , max ( points [ <NUM_LIT:0> ] ) ) : <EOL> for j in range ( min ( points [ <NUM_LIT:1> ] ) , max ( points [ <NUM_LIT:1> ] ) ) : <EOL> x = linalg . solve ( points , [ i , j , <NUM_LIT:1> ] ) <EOL> if min ( x ) > <NUM_LIT:0> : <EOL> alpha [ i , j ] = <NUM_LIT:1> <EOL> return alpha <EOL> def triangulate_points ( x , y ) : <EOL> """<STR_LIT>""" <EOL> centers , edges , tri , neighbors = md . delaunay ( x , y ) <EOL> return tri <EOL> def plot_mesh ( x , y , tri ) : <EOL> """<STR_LIT>""" <EOL> for t in tri : <EOL> t_ext = [ t [ <NUM_LIT:0> ] , t [ <NUM_LIT:1> ] , t [ <NUM_LIT:2> ] , t [ <NUM_LIT:0> ] ] <EOL> plot ( x [ t_ext ] , y [ t_ext ] , '<STR_LIT:r>' ) <EOL> def pw_affine ( fromim , toim , fp , tp , tri ) : <EOL> """<STR_LIT>""" <EOL> im = toim . copy ( ) <EOL> is_color = len ( fromim . shape ) == <NUM_LIT:3> <EOL> im_t = zeros ( im . shape , '<STR_LIT>' ) <EOL> for t in tri : <EOL> H = homography . Haffine_from_points ( tp [ : , t ] , fp [ : , t ] ) <EOL> if is_color : <EOL> for col in range ( fromim . shape [ <NUM_LIT:2> ] ) : <EOL> im_t [ : , : , col ] = ndimage . affine_transform ( <EOL> fromim [ : , : , col ] , H [ : <NUM_LIT:2> , : <NUM_LIT:2> ] , ( H [ <NUM_LIT:0> , <NUM_LIT:2> ] , H [ <NUM_LIT:1> , <NUM_LIT:2> ] ) , im . shape [ : <NUM_LIT:2> ] ) <EOL> else : <EOL> im_t = ndimage . affine_transform ( <EOL> fromim , H [ : <NUM_LIT:2> , : <NUM_LIT:2> ] , ( H [ <NUM_LIT:0> , <NUM_LIT:2> ] , H [ <NUM_LIT:1> , <NUM_LIT:2> ] ) , im . shape [ : <NUM_LIT:2> ] ) <EOL> alpha = alpha_for_triangle ( tp [ : , t ] , im . shape [ <NUM_LIT:0> ] , im . shape [ <NUM_LIT:1> ] ) <EOL> im [ alpha > <NUM_LIT:0> ] = im_t [ alpha > <NUM_LIT:0> ] <EOL> return im <EOL> def panorama ( H , fromim , toim , padding = <NUM_LIT> , delta = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> is_color = len ( fromim . shape ) == <NUM_LIT:3> <EOL> def transf ( p ) : <EOL> p2 = dot ( H , [ p [ <NUM_LIT:0> ] , p [ <NUM_LIT:1> ] , <NUM_LIT:1> ] ) <EOL> return ( p2 [ <NUM_LIT:0> ] / p2 [ <NUM_LIT:2> ] , p2 [ <NUM_LIT:1> ] / p2 [ <NUM_LIT:2> ] ) <EOL> if H [ <NUM_LIT:1> , <NUM_LIT:2> ] < <NUM_LIT:0> : <EOL> print '<STR_LIT>' <EOL> if is_color : <EOL> toim_t = hstack ( ( toim , zeros ( ( toim . shape [ <NUM_LIT:0> ] , padding , <NUM_LIT:3> ) ) ) ) <EOL> fromim_t = zeros ( ( toim . shape [ <NUM_LIT:0> ] , toim . shape [ <NUM_LIT:1> ] + padding , toim . shape [ <NUM_LIT:2> ] ) ) <EOL> for col in range ( <NUM_LIT:3> ) : <EOL> fromim_t [ : , : , col ] = ndimage . geometric_transform ( fromim [ : , : , col ] , <EOL> transf , ( toim . shape [ <NUM_LIT:0> ] , toim . shape [ <NUM_LIT:1> ] + padding ) ) <EOL> else : <EOL> toim_t = hstack ( ( toim , zeros ( ( toim . shape [ <NUM_LIT:0> ] , padding ) ) ) ) <EOL> fromim_t = ndimage . geometric_transform ( fromim , transf , <EOL> ( toim . shape [ <NUM_LIT:0> ] , toim . shape [ <NUM_LIT:1> ] + padding ) ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> H_delta = array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:1> , - delta ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ] ) <EOL> H = dot ( H , H_delta ) <EOL> if is_color : <EOL> toim_t = hstack ( ( zeros ( ( toim . shape [ <NUM_LIT:0> ] , padding , <NUM_LIT:3> ) ) , toim ) ) <EOL> fromim_t = zeros ( ( toim . shape [ <NUM_LIT:0> ] , toim . shape [ <NUM_LIT:1> ] + padding , toim . shape [ <NUM_LIT:2> ] ) ) <EOL> for col in range ( <NUM_LIT:3> ) : <EOL> fromim_t [ : , : , col ] = ndimage . geometric_transform ( fromim [ : , : , col ] , <EOL> transf , ( toim . shape [ <NUM_LIT:0> ] , toim . shape [ <NUM_LIT:1> ] + padding ) ) <EOL> else : <EOL> toim_t = hstack ( ( zeros ( ( toim . shape [ <NUM_LIT:0> ] , padding ) ) , toim ) ) <EOL> fromim_t = ndimage . geometric_transform ( fromim , <EOL> transf , ( toim . shape [ <NUM_LIT:0> ] , toim . shape [ <NUM_LIT:1> ] + padding ) ) <EOL> if is_color : <EOL> alpha = ( ( fromim_t [ : , : , <NUM_LIT:0> ] * fromim_t [ : , : , <NUM_LIT:1> ] * fromim_t [ : , : , <NUM_LIT:2> ] ) > <NUM_LIT:0> ) <EOL> for col in range ( <NUM_LIT:3> ) : <EOL> toim_t [ : , : , col ] = fromim_t [ : , : , col ] * alpha + toim_t [ : , : , col ] * ( <NUM_LIT:1> - alpha ) <EOL> else : <EOL> alpha = ( fromim_t > <NUM_LIT:0> ) <EOL> toim_t = fromim_t * alpha + toim_t * ( <NUM_LIT:1> - alpha ) <EOL> return toim_t </s>
