hslogic_一个简单的基于图像二值化处理的报纸分割算法的实现

         由于报纸的不同新闻之间都有边框,所以这个问题实际上就是将一张中间被很多黑线分出区域的图片按黑线进行分割变成多张图片。

 

p1=0.73;
p2=6;
p3=3;
p4=5;


I1=imread('newspaper.jpg');%读入图片格式RGB
I2=rgb2gray(I1);           %转化为灰度图

figure(1)
subplot(231),imshow(I2)    %显示灰度图

I3=im2bw(I2,p1);          %二值化处理
subplot(232),imshow(I3)    %显示二值化图

SE1=strel('disk',p2);       %结构元素操作,操作方式为disk
I4=imerode(I3,SE1);        %实现图像腐蚀
subplot(233),imshow(I4)    %显示图像腐蚀后的图

SE2=strel('square',p3);     %结构元素操作,操作方式为square
M1=imopen(I4,SE2);         %实现图像腐蚀
subplot(234),imshow(M1)    %显示图像腐蚀后的图

SE3=strel('square',p4)      %结构元素操作,操作方式为square
I5=imclose(M1,SE3);        %实现图像腐蚀
subplot(235),imshow(I5)    %显示图像腐蚀后的图

I6=imfill(~I5,'holes')     %对边际填充
subplot(236),imshow(I6)    

[L,n]=bwlabel(I6);         
figure(2)
imshow(I1);


for i=1:n
   
    [r,c]=find(L==i);
    a1(i)=max(r);a2(i)=min(r);
    b1(i)=max(c);b2(i)=min(c);
    w=b1(i)-b2(i);h=a1(i)-a2(i); 
    rectangle('Position',[b2(i),a2(i),w,h],'LineWidth',3,'EdgeColor','b');
    
    b=strcat(int2str(i));
    c='.jpg';
    str=strcat(b,c);
    imwrite(I1(a2(i):a1(i),b2(i):b1(i),1:3),str ,'jpg');
end

猜你喜欢

转载自blog.csdn.net/ccsss22/article/details/108709004