求大神看看这个MatLab 代码是什么作用
北佳霖 2019-05-20 03:54:06
clear;
load pages_im_yzq.mat;
page_nums = size(pages_im, 1);
% for page_num = 1: page_numsyzq1
page_num = 3;
[rows, cols, colors] = size(pages_im{page_num,1});
a = pages_im{page_num,1};
imshow(a,[]);
% agray = im2gray(a);
a_sum_color = sum(a,3);
a2 = a_sum_color;
a2(a2<200) = 0;
a2(a2>=200) = 1;
row10 = round(rows/10);
col5 = round(cols/5);
halfcol = round(col5/2)-1;
halfrow = round(row10/2)-1;
% page= cell(10,5);
columny = (0:4)*col5 + halfcol + 1;
rowx = (0:9)*row10 + halfrow + 1;
hold on;
% plot([rowx(1),left],[rowx(1), right],'r');
for row = 1 : 10
plot([1, cols],[rowx(row),rowx(row)],'r');
end;
for col = 1 : 5
plot([columny(col),columny(col)],[1, rows],'r');
end;
blockx = floor(cols/5);
blocky = floor(rows/10);
width = floor(0.8*col5);
height = floor(0.9 * row10);
for row = 1 : 10
for col = 2 : 5
centery = rowx(row);
centerx = columny(col);
rangey = centery;
rangex = centerx;
y = 0;
% calc the upper line.
while y <= height
sumrow = sum(a2 (rangey, centerx - width : centerx + width))
rangey = rangey - 1;
if (sumrow <= 1 ) break;
end;
end;
hold on, plot([1, cols],[rangey, rangey],'g');
% lower, left, right;
rangey=centery;
while y<= height
sumrow= sum(a2 (rangey, centerx-width:centerx+width));
rangey= rangey+1;
if (sumrow<=1) break;
end;
end;
plot([1,cols],[rangey,rangey],'g');
end;
end;
hold on;
b = a_sum_color(1:500,1:500);
imshow(b,[]);
%b = a_sum_color;
% [rrow, rcol] = find(b>300 & b<600);
[rrow, rcol] = find(b>300);
% rrow = 201 - rrow;
% 要把rrow,rcol拼成坐标,应当用吧b([rrow rcol]);即列平行排列;
plot(rcol,rrow, 'r.');
a_bw = a_sum_color;
a_bw(a_bw<200) = 0;
a_bw(a_bw>=200) = 1;
% end;
row10 = round(rows/10);
col5 = round(cols/5);
halfcol = round(col5/2)-1;
halfrow = round(row10/2)-1;
page= cell(10,5);
columny = (0:4)*col5 + halfcol + left;
rowx = (0:9)*row10 + halfrow + up;
hold on;
% plot([rowx(1),left],[rowx(1), right],'r');
for row = 1 : 10
plot([left, right],[rowx(row),rowx(row)],'r');
end;
for col = 1 : 5
plot([columny(col),columny(col)],[up, down],'r');
end;
% columny(1) = columny(1) +1;
% rowx(1) = rowx(1) + 1;
figure(2)
subplot(10,5,50);
% for row = 1 : 10
% for col = 1 : 5
% page{row,col} = a2(rowx(row) - halfrow + up: rowx(row) + halfrow +up ,...
% columny(col)-halfcol+left : columny(col) + halfcol+left );
%
% subplot(10,5, (row-1)*5 + col),imshow(page{row,col});
% end;
% end;
for row = 1 : 10
for col = 1 : 1
page{row,col} = a2(rowx(row) - halfrow + up: rowx(row) + halfrow +up ,...
columny(col)-halfcol+left : columny(col) + halfcol+left );
figure,imshow(page{row,col});
end;
end;
% xx1 = 183; xx2 = 885;
% [x3 x4]= find(a2(:,500) == 255)
% xx3 = 129; xx4 = 1381;
%
% plot([]);
% for line = 1 : 10
% for column = 1 : 5
% plot();