clear all ;
[inColorImg,pathname]=uigetfile('*.jpg;*.bmp','Open A Color Image Dialog Box');
img_read=imread([pathname,inColorImg],'bmp');
I = rgb2gray(img_read);
%I = img_read ;
%BW0=im2bw(I ,0.667);
BW0 = ~im2bw( I , graythresh(I)) ; % OTSU 求阈值
BW = edge( I , 'canny');
figure;imshow(I);
theta_step=1;
theta = 0:theta_step:180;
[start_points , end_points ,angles ] = getlines( BW , theta , 8);
[ H , W ] = size(BW);
% 这里简单的挑选了有可能的直线(暂定小于5度),但是可能找不到,而默认使用的是图像上下边界的值
% 为了处理这种恶劣情况,在直线挑选失败后应采用 [ 2 ,1 ] 的腐蚀算子对图像做二值腐蚀操作,然后
% 确定上下边框的大致范围,并用直线拟合来确定近似上下边框直线。
[up_idx , bottom_idx] = find_up_bottom( start_points , angles , floor(H/2));
up_idx = 0 ;
bottom_idx = 0 ;
temp = 0 ; %floor( H/10 );
if( up_idx == 0 || bottom_idx == 0 )
% 如果没有检测到直线用下面这种方法来确定上下边框的大致位置。
temp_add = floor( H/10 ) ;
[ up_line_start , up_line_end , bottom_line_start , bottom_line_end ] = getupbottomline0( BW );
up_line_start( 1 , 2 ) = up_line_start( 1 , 2 ) - temp_add ;
up_line_end( 1 , 2 ) = up_line_end( 1 ,2 ) - temp_add;
bottom_line_start( 1 , 2 ) = bottom_line_start( 1 , 2 ) + temp_add ;
bottom_line_end( 1 , 2 ) = bottom_line_end( 1 , 2) + temp_add ;
else
if( up_idx ~= 0 )
up_line_start = start_points( up_idx , : );
up_line_end = end_points( up_idx , : );
else
up_line_start = [ 1 , 1 ] ;
up_line_end = [ W , 1 ] ;
end
if( bottom_idx ~= 0 )
bottom_line_start = start_points(bottom_idx , : );
bottom_line_end = end_points( bottom_idx , : );
else
bottom_line_start = [ 1 , H ] ;
bottom_line_end = [ W , H ] ;
end
end
[ up_outline , bottom_outline , vproject ] = get_up_bottom_outline( BW0 , up_line_start , up_line_end , bottom_line_start , bottom_line_end );
[ mid_up_project , mid_bottom_project ] = mid_project( BW0 , up_line_start , up_line_end , bottom_line_start , bottom_line_end );
figure , imshow(BW0);
hold on ;
line_up = line( [ up_line_start(1) , up_line_end(1) ] , [ up_line_start(2)+temp , up_line_end(2)+temp ] );
set( line_up , 'color' , 'm' );
line_bottom = line( [ bottom_line_start(1) , bottom_line_end(1)] , [ bottom_line_start(2)-temp , bottom_line_end(2)-temp] );
set( line_bottom , 'color' , 'm' );
% 第一次粗略分割文字,分割出的文字有可能粘连,有可能分割错误,或者有些结果不是文字。
%[ start_idx , end_idx , k ] = splitwords0( mid_bottom_project );
[ start_idx , end_idx , k ] = splitwords0( mid_up_project );
for i= 1 : k
s_line = line( [ start_idx(i) , start_idx(i) ] , [ 1 , H ] );
set( s_line , 'color' , 'r' );
e_line = line( [ end_idx(i) , end_idx(i) ] , [ 1 , H ] );
set( e_line , 'color' , 'r' );
end
hold off ;
% figure ;
% subplot( 2 , 2 , 1 ) , hold on ,plot(up_outline) , plot(bottom_outline) , hold off;
% subplot( 2 , 2 , 2 ) ,plot( vproject );
% subplot( 2 , 2 , 3 ) , plot( mid_bottom_project );
% subplot( 2 , 2 , 4 ) , plot( mid_up_project );
[ R , n ] = splitwords1( BW0 , start_idx , end_idx , k , up_line_start , up_line_end , bottom_line_start , bottom_line_end );
figure ;
for i = 1 : n
subplot( 1 , k , i ) , subimage( BW0( R(i,1) :R(i,2) , R(i,3) : R(i,4) ));
end
- 1
- 2
- 3
前往页