前言
有时候很长的文档图片不适合阅读,本文将长图进行分割,然后可以用分割后的图片生成PDF,方便阅读。
基础版
分割图片
a=imread('a.jpg');
for i=1:70
eval(['Q',num2str(i),'=a((i-1)*1400+1:i*1400,:,:);']);
end
for i=1:70
eval(['imwrite(Q',num2str(i), ',''result',num2str(i),'.jpg'');']);
close all
end
这里使用imwrite函数将matlab绘图白边去掉
找到分割线
close all
len_r=[];
for tt=1:70
xy_r=[];
eval(['I=imread(''result',num2str(tt),'.jpg'');']);
I = rgb2gray(I);
%I = imrotate(I,33,'crop');
% figure
% imshow(rotI);
BW = edge(I,'canny');
% figure
% imshow(BW);
[H,T,R] = hough(BW);
% figure
% imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
% xlabel('\theta'), ylabel('\rho');
% axis on
% axis normal
% hold on
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
% plot(x,y,'s','color','white');
% Find lines and plot them
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
figure, imshow(I),hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if (len>950)&&xy(1,1)~=xy(2,1)
xy_long = xy;
len_r=[len_r;tt,len,xy(1,2)];
xy_r=[xy_r,xy];
end
end
% highlight the longest line segment
for i=1:size(xy_r,2)/2
plot(xy_r(:,2*i-1),xy_r(:,2*i),'LineWidth',2,'Color','blue');
end
end
按照分割线分割
close all
for tt=1:70
eval(['I=imread(''result',num2str(tt),'.jpg'');']);
if tt>1
eval(['I1=imread(''result',num2str(tt-1),'.jpg'');']);
I2=I1(len_r(tt-1,3):end,:,:);
a=[I2;I(1:len_r(tt,3),:,:)];
else
a=I(1:len_r(tt,3),:,:);
end
eval(['imwrite(a,''final',num2str(tt),'.jpg'');']);
end
改进
因为每张图片的长度不一样,有时候寻找的直线被分成了几段,这里进行改进。
原始图片
主函数
clear;
clc;
a=imread('a.png');
for i=1400:2:1700
I=a(1:i,:,:);
len_r=lope(I,0);
if size(len_r,1)>=1
break;
end
end
I=a(1:i,:,:);
lope(I,1)
itt=i;
for i=1:round(size(a,1)/itt)
if i<round(size(a,1)/itt)
eval(['Q',num2str(i),'=a((i-1)*itt+1:i*itt,:,:);']);
else
eval(['Q',num2str(i),'=a((i-1)*itt+1:end,:,:);']);
end
end
for i=1:round(size(a,1)/itt)
eval(['imwrite(Q',num2str(i), ',''result',num2str(i),'.jpg'');']);
end
close all
len_r=[];
for tt=1:round(size(a,1)/itt)
tt
xy_r=[];
eval(['I=imread(''result',num2str(tt),'.jpg'');']);
I = rgb2gray(I);
%I = imrotate(I,33,'crop');
% figure
% imshow(rotI);
BW = edge(I,'canny');
% figure
% imshow(BW);
[H,T,R] = hough(BW);
% figure
% imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
% xlabel('\theta'), ylabel('\rho');
% axis on
% axis normal
% hold on
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
% plot(x,y,'s','color','white');
% Find lines and plot them
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
% figure, imshow(I),hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
% plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% %
% % % Plot beginnings and ends of lines
% plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
% plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if (len>200)&&xy(1,1)~=xy(2,1)
xy_long = xy;
len_r=[len_r;tt,len,xy(1,2)];
xy_r=[xy_r,xy];
end
end
% highlight the longest line segment
% for i=1:size(xy_r,2)/2
% plot(xy_r(:,2*i-1),xy_r(:,2*i),'LineWidth',2,'Color','blue');
% end
end
llll=len_r;
if len_r(1,3)<20
len_r(1,:)=[];
end
m=findxt(len_r(:,3));
maxx=max(m);
qw=zeros(maxx,3);
for i=1:length(len_r)
qw(m(i),2)=qw(m(i),2)+len_r(i,2);
qw(m(i),1)=len_r(i,1);
qw(m(i),3)=len_r(i,3);
end
qwe=zeros(round(size(a,1)/itt),3);
t1=1;
t2=0;
num=1;
t3=0;
for i=1:size(qw,1)
if qw(i,1)==t1
if qw(i,2)>t2
t2=qw(i,2);
t3=qw(i,3);
end
else
qwe(num,2)=t2;
qwe(num,1)=t1;
qwe(num,3)=t3;
t3=0;
t2=0;
t1=t1+1;
num=num+1;
if qw(i,2)>t2
t2=qw(i,2);
t3=qw(i,3);
end
end
end
if i==size(qw,1)
qwe(num,2)=t2;
qwe(num,1)=t1;
qwe(num,3)=t3;
end
close all
for tt=1:round(size(a,1)/itt)
tt
eval(['I=imread(''result',num2str(tt),'.jpg'');']);
if tt>1
eval(['I1=imread(''result',num2str(tt-1),'.jpg'');']);
I2=I1(qwe(tt-1,3):end,:,:);
a=[I2;I(1:qwe(tt,3),:,:)];
else
a=I(1:qwe(tt,3),:,:);
end
eval(['imwrite(a,''final',num2str(tt),'.jpg'');']);
end
计算图片的长度
function len_r=lope(I,k)
len_r=[];
xy_r=[];
I = rgb2gray(I);
%I = imrotate(I,33,'crop');
% figure
% imshow(rotI);
BW = edge(I,'canny');
% figure
% imshow(BW);
[H,T,R] = hough(BW);
% figure
% imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
% xlabel('\theta'), ylabel('\rho');
% axis on
% axis normal
% hold on
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
% plot(x,y,'s','color','white');
% Find lines and plot them
lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
if k==1
figure, imshow(I),hold on
end
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
if k==1
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% %
% % % Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
end
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if (len>700)&&xy(1,1)~=xy(2,1)
xy_long = xy;
len_r=[len_r;len,xy(1,2)];
xy_r=[xy_r,xy];
end
end
% highlight the longest line segment
if k==1
for i=1:size(xy_r,2)/2
plot(xy_r(:,2*i-1),xy_r(:,2*i),'LineWidth',2,'Color','blue');
end
end
if len_r~=0
if len_r(1,2)<120
len_r(1,:)=[];
end
end
找出每张图片最长水平直线的位置
function Output=findxt(X)
Output = ones(size(X));
k = 1;
for ii = 2:length(X)
temp = X(ii);
if (temp == X(ii-1))
Output(ii) = k;
else
k = k+1;
Output(ii) = k;
end
end
end
结果
第一张
第二张
第三张
总结
如图所示,分割效果不错,后面可以根据梯度等信息对程序进行下一步改进。