由于報紙的不同新聞之間都有邊框,所以這個問題實際上就是將一張中間被很多黑線分出區域的圖片按黑線進行分割變成多張圖片,

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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/114113.html
標籤:其他
上一篇:并行程式設計——概論和背景知識
下一篇:C++計算機語言
