使用matlab将长图按分割线分成若干张图片

本文介绍了一种方法,通过Matlab脚本将长文档图片分割成多张,并利用Hough变换找出分割线,进一步生成PDF便于高效阅读。文章详细展示了基础版分割图片、改进原始图片的步骤和关键算法,包括计算图片长度、最长水平直线定位等。
摘要由CSDN通过智能技术生成

前言

有时候很长的文档图片不适合阅读,本文将长图进行分割,然后可以用分割后的图片生成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

结果

第一张

在这里插入图片描述

第二张

在这里插入图片描述

第三张

在这里插入图片描述

总结

如图所示,分割效果不错,后面可以根据梯度等信息对程序进行下一步改进。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leetteel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值