boblsturm@0: function [Y] = rot_kernel(kernel, deg) boblsturm@0: boblsturm@0: N = size( kernel, 1 ); boblsturm@0: max_rots = 4*(N-1); boblsturm@0: deg_per_rot = 360 / max_rots; boblsturm@0: boblsturm@0: % deg = deg - mod( deg, deg_per_rot ); boblsturm@0: deg = deg_per_rot * ( round( deg / deg_per_rot ) ); boblsturm@0: num_of_rots = ceil( deg / deg_per_rot ); boblsturm@0: boblsturm@0: if num_of_rots < 1 boblsturm@0: Y = kernel; boblsturm@0: return boblsturm@0: else boblsturm@0: for r = 1:num_of_rots boblsturm@0: if mod( r, 2 ) == 0 boblsturm@0: kernel = rot_outer_layer( kernel ); boblsturm@0: else boblsturm@0: kernel = rot_inner_layer( kernel ); boblsturm@0: end boblsturm@0: end boblsturm@0: end boblsturm@0: boblsturm@0: Y = kernel; boblsturm@0: boblsturm@0: end boblsturm@0: boblsturm@0: function [Y] = rot_outer_layer( A ) boblsturm@0: dim = size( A, 1 ); boblsturm@0: top = A( 1, : ); boblsturm@0: right = A( 2:dim, dim )'; boblsturm@0: bottom = fliplr( A( dim, 1:dim - 1 ) ); boblsturm@0: left = fliplr( A( 2:dim - 1, 1 )' ); boblsturm@0: shifted_layers = wshift('1D',[top,right,bottom,left],-1); boblsturm@0: boblsturm@0: top = shifted_layers( 1:length(top)); boblsturm@0: last_ind = length( top ); boblsturm@0: boblsturm@0: right = shifted_layers( last_ind + 1 : last_ind + length(right)); boblsturm@0: last_ind = last_ind + length( right ); boblsturm@0: boblsturm@0: bottom = shifted_layers( last_ind + 1 : last_ind + length(bottom)); boblsturm@0: last_ind = last_ind + length( bottom ); boblsturm@0: boblsturm@0: left = shifted_layers( last_ind + 1 : end ); boblsturm@0: boblsturm@0: A( 1, : ) = top; boblsturm@0: A( 2:dim, dim ) = right'; boblsturm@0: A( dim, 1:dim - 1 ) = fliplr( bottom ); boblsturm@0: A( 2:dim - 1, 1 ) = fliplr( left' ); boblsturm@0: boblsturm@0: Y = A; boblsturm@0: end boblsturm@0: boblsturm@0: function [Y] = rot_inner_layer( kernel ) boblsturm@0: % dim = size( kernel, 1 ); boblsturm@0: % if dim < 2 boblsturm@0: % Y = rot_outer_layer( kernel ) boblsturm@0: % return; boblsturm@0: % else boblsturm@0: % Y = rot_inner_layer( kernel( 2:dim - 1, 2:dim - 1 ) ) boblsturm@0: % end boblsturm@0: dim = size( kernel, 1 ); boblsturm@0: boblsturm@0: if mod( dim / 2, 2 ) == 0 boblsturm@0: max_rots = dim / 2 - 1; boblsturm@0: else boblsturm@0: max_rots = dim / 2 - 0.5; boblsturm@0: end boblsturm@0: boblsturm@0: low = 2; boblsturm@0: high = dim - 1; boblsturm@0: for i = 1:max_rots boblsturm@0: kernel( low:high, low:high ) = rot_outer_layer( kernel( low:high, low:high ) ); boblsturm@0: low = low + 1; boblsturm@0: high = high - 1; boblsturm@0: end boblsturm@0: boblsturm@0: Y = kernel; boblsturm@0: end