hslogic_A simple realization of newspaper segmentation algorithm based on image binarization processing

         Since there are borders between different news items in the newspaper, the problem is actually to divide a picture in the middle divided by many black lines into multiple pictures by black lines.

 

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


I1=imread('newspaper.jpg');% read the picture format RGB
I2=rgb2gray(I1);% convert to grayscale

figure(1)
subplot(231), imshow(I2)% show gray scale

I3=im2bw(I2,p1);% Binarization processing
subplot(232), imshow(I3)% Display binarization graph

SE1=strel('disk',p2);% Operation of structural elements, the operation mode is disk
I4=imerode(I3,SE1);% Realize image corrosion
subplot(233), imshow(I4)% Display the image after image corrosion

SE2=strel('square',p3); %Structural element operation, the operation mode is square
M1=imopen(I4,SE2); %Realize image corrosion
subplot(234),imshow(M1) %Display the image after image corrosion

SE3=strel('square',p4)% Operation of structural elements, the operation method is square
I5=imclose(M1,SE3);% Realize image corrosion
subplot(235), imshow(I5)% Display the image after image corrosion

I6=imfill(~I5,'holes')% Fill
subplot(236), imshow(I6)     on the margin

[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

Guess you like

Origin blog.csdn.net/ccsss22/article/details/108709004
Recommended