如何自动保存分段图像

时间:2014-07-08 10:35:48

标签: matlab image-segmentation

我在matlab上做了一个程序,用于bengali脚本的字符分割,我想在一个特定的文件夹中自动保存一系列分段图像。所以任何人都可以帮助我使用matlab代码自动保存图像

%%LINES SEGMENTATION:

close all;
clear all;
clc;

im=imread('page_1.tif');%read the image
%figure, imshow(im);
%%title('INPUT IMAGE WITH NOISE');
qw=im;

if size(im,3)==3 % RGB image %% Convert to gray scale
    im=rgb2gray(im);
end
threshold = graythresh(im); %% Convert to binary image
im =~im2bw(im,threshold);
im = bwareaopen(im,10); %% Remove all object containing fewer than 60 pixels
%figure(2) %% Show image binary image
%imshow(~im);
%title('INPUT IMAGE WITHOUT NOISE');
%figure, area(im);

%find Area of Interest:
a=(~im);
ijk=~a;
xy=sum(ijk,2);
measurements = regionprops(xy == 0, 'Area', 'PixelIdxList');
%Get indexes of those regions that are >= 20 in length.
fiveLongRegions = find([measurements.Area] >= 3);
theIndexes = vertcat(measurements(fiveLongRegions).PixelIdxList);
c=a;
c(theIndexes,:)=0;
%figure, imshow(c);
%title('AREA OF INTEREST FOR LINE SEGMENT');
ca=~c;
%Segmentation of line:
[x,y]=size(ca);
mat1=sum(ca,2);%sum the elements of bwrowise and save into column matrix mat1
mat2=y-mat1;%subtract each element of the sum matrix(mat1) from the width length(no. of columns)
mat3=mat2~=0;
mat4=diff(mat3);
index1=find(mat4);
[q,w]=size(index1);%size of index2 matrix is q*w
kap=1;
lam=1;
%iii=1;
%ii=1;
while kap<((q/2)+1);%number of loops=number of lines
    k=1;
    mat5=([]);
       for j=(index1(lam)+1):1:index1(lam+1)
         mat5(k,:)=c(j,:); %store the line segmented matrix
           k=k+1;
       end


            lam=lam+2;   
        kap=kap+1;

       figure, imshow(mat5);
       %%imsave();



       %%WORDS SEGMENTATION:

        bwword=im2bw(mat5);%convert the image im into binary(black and white)
        aword=~bwword;


        bword=sum(aword,1);
        %%figure, area(b);

        %%Measure lengths of all "0" regions.
        measurementsword = regionprops(bword == 0, 'Area', 'PixelIdxList');

        % Get indexes of those regions that are >= 20 in length.
        fiveLongRegionsword = find([measurementsword.Area] >= 5);
        theIndexesword = vertcat(measurementsword(fiveLongRegionsword).PixelIdxList);
        cword=aword;
        cword(:,theIndexesword)=1;
        %%figure, imshow(c);

        dword=~cword;
        %%figure, imshow(dword);
        %%title('AREA OF INTERESRT FOR WORD SEGMENTATION IMAGE');


        [xword,yword]=size(cword);
        mat1word=sum(cword,1);%sum the elements of bwrowise and save into column matrix mat1
        mat2word=xword-mat1word;%subtract each element of the sum matrix(mat1) from the width length(no. of columns)
        mat3word=mat2word~=0;
        mat4word=diff(mat3word);
        index1word=find(mat4word);
        [qword,wword]=size(index1word);%size of index2 matrix is q*w
        kapword=1;
        lamword=1;
        %%iii=1;
        %%ii=1;

         while kapword<((wword/2)+1);%number of loops=number of lines
    kword=1;
    mat5word=([]);
       for jword=(index1word(lamword)+1):1:index1word(lamword+1)
         mat5word(:,kword)=bwword(:,jword); %store the word segmented matrix
           kword=kword+1;
       end


            lamword=lamword+1;   
       kapword=kapword+1;

       figure, imshow(mat5word);
       %imsave();


         %%CHARACTER SEGMENTATION:


       bwcha=im2bw(mat5word);%convert the image im into binary(black and white)
        acha=~bwcha;

        mat1cha=sum(a,2);
        %figure, area(mat1);
        xcha=find(mat1cha>70);
        ycha=acha;
        ycha(xcha,:)=0;
        ycha = bwareaopen(ycha,20);
        figure,imshow(ycha);
        %figure, area(y);
        %mat2=sum(y,2);
        %x1=find(mat2<7);
        %y1=y;
        %y1(x1,:)=0;
        %y1 = bwareaopen(y1,15);
        %z=~y1;
        %figure, imshow(z);

        [Lcha, bcha,]=bwlabel(ycha);   %% Label connected components
        propiedcha=regionprops(Lcha,'BoundingBox');  %% Measure properties of image regions
        for ncha=1:size(propiedcha,1)  %% Plot Bounding Box
        rectangle('Position',propiedcha(ncha).BoundingBox,'EdgeColor','g','LineWidth',1);
        end


        for ncha=1:bcha   %% Objects extraction
            [rcha,ccha] = find(Lcha==ncha);
         n1cha=ycha(min(rcha):max(rcha),min(ccha):max(ccha));
            figure, imshow(~n1cha);
            %pause(0.2)
            %imsave();
        end







        end

end





`enter co%%LINES SEGMENTATION:

close all;
clear all;
clc;

im=imread('page_1.tif');%read the image
%figure, imshow(im);
%%title('INPUT IMAGE WITH NOISE');
qw=im;

if size(im,3)==3 % RGB image %% Convert to gray scale
    im=rgb2gray(im);`enter code here`
end
threshold = graythresh(im); %% Convert to binary image
im =~im2bw(im,threshold);
im = bwareaopen(im,10); %% Remove all object containing fewer than 60 pixels
%figure(2) %% Show image binary image
%imshow(~im);
%title('INPUT IMAGE WITHOUT NOISE');
%figure, area(im);

%find Area of Interest:
a=(~im);
ijk=~a;
xy=sum(ijk,2);
measurements = regionprops(xy == 0, 'Area', 'PixelIdxList');
%Get indexes of those regions that are >= 20 in length.
fiveLongRegions = find([measurements.Area] >= 3);
theIndexes = vertcat(measurements(fiveLongRegions).PixelIdxList);
c=a;
c(theIndexes,:)=0;
%figure, imshow(c);
%title('AREA OF INTEREST FOR LINE SEGMENT');
ca=~c;
%Segmentation of line:
[x,y]=size(ca);
mat1=sum(ca,2);%sum the elements of bwrowise and save into column matrix mat1
mat2=y-mat1;%subtract each element of the sum matrix(mat1) from the width length(no. of columns)
mat3=mat2~=0;
mat4=diff(mat3);
index1=find(mat4);
[q,w]=size(index1);%size of index2 matrix is q*w
kap=1;
lam=1;
%iii=1;
%ii=1;
while kap<((q/2)+1);%number of loops=number of lines
    k=1;
    mat5=([]);
       for j=(index1(lam)+1):1:index1(lam+1)
         mat5(k,:)=c(j,:); %store the line segmented matrix
           k=k+1;
       end


            lam=lam+2;   
        kap=kap+1;

       figure, imshow(mat5);
       %%imsave();



       %%WORDS SEGMENTATION:

        bwword=im2bw(mat5);%convert the image im into binary(black and white)
        aword=~bwword;


        bword=sum(aword,1);
        %%figure, area(b);

        %%Measure lengths of all "0" regions.
        measurementsword = regionprops(bword == 0, 'Area', 'PixelIdxList');

        % Get indexes of those regions that are >= 20 in length.
        fiveLongRegionsword = find([measurementsword.Area] >= 5);
        theIndexesword = vertcat(measurementsword(fiveLongRegionsword).PixelIdxList);
        cword=aword;
        cword(:,theIndexesword)=1;
        %%figure, imshow(c);

        dword=~cword;
        %%figure, imshow(dword);
        %%title('AREA OF INTERESRT FOR WORD SEGMENTATION IMAGE');


        [xword,yword]=size(cword);
        mat1word=sum(cword,1);%sum the elements of bwrowise and save into column matrix mat1
        mat2word=xword-mat1word;%subtract each element of the sum matrix(mat1) from the width length(no. of columns)
        mat3word=mat2word~=0;
        mat4word=diff(mat3word);
        index1word=find(mat4word);
        [qword,wword]=size(index1word);%size of index2 matrix is q*w
        kapword=1;
        lamword=1;
        %%iii=1;
        %%ii=1;

         while kapword<((wword/2)+1);%number of loops=number of lines
    kword=1;
    mat5word=([]);
       for jword=(index1word(lamword)+1):1:index1word(lamword+1)
         mat5word(:,kword)=bwword(:,jword); %store the word segmented matrix
           kword=kword+1;
       end


            lamword=lamword+1;   
       kapword=kapword+1;

       figure, imshow(mat5word);
       %imsave();


         %%CHARACTER SEGMENTATION:


       bwcha=im2bw(mat5word);%convert the image im into binary(black and white)
        acha=~bwcha;

        mat1cha=sum(a,2);
        %figure, area(mat1);
        xcha=find(mat1cha>70);
        ycha=acha;
        ycha(xcha,:)=0;
        ycha = bwareaopen(ycha,20);
        figure,imshow(ycha);
        %figure, area(y);
        %mat2=sum(y,2);
        %x1=find(mat2<7);
        %y1=y;
        %y1(x1,:)=0;
        %y1 = bwareaopen(y1,15);
        %z=~y1;
        %figure, imshow(z);

        [Lcha, bcha,]=bwlabel(ycha);   %% Label connected components
        propiedcha=regionprops(Lcha,'BoundingBox');  %% Measure properties of image regions
        for ncha=1:size(propiedcha,1)  %% Plot Bounding Box
        rectangle('Position',propiedcha(ncha).BoundingBox,'EdgeColor','g','LineWidth',1);
        end


        for ncha=1:bcha   %% Objects extraction
            [rcha,ccha] = find(Lcha==ncha);
         n1cha=ycha(min(rcha):max(rcha),min(ccha):max(ccha));
            figure, imshow(~n1cha);
            %pause(0.2)
            %imsave();
        end







        end

end





de here`ally i am providing my code

2 个答案:

答案 0 :(得分:0)

假设您正在尝试将您在代码末尾显示的所有n1cha张图片写入文件夹。

您需要维护一个计数器,该计数器计算您已保存的字符数,然后使用imwrite保存字符:

% init variables before loop
imgFolder = 'path/to/saved/images';
counter = 1;
while kap<((q/2)+1);%number of loops=number of lines
      % your code here... nested loops...
      % ...
      % finally you get to this loop:
      for ncha=1:bcha
          % extract the character...
          imwrite( n1cha, fullfile( imgFolder, sprintf( 'char_%04d.png', counter ) ) );
          counter = counter + 1;
      end
end 

答案 1 :(得分:0)

如果您想将其保存到名称为“文件夹”的文件夹中,请点击此处(我正在使用并复制粘贴您的代码):

folder = "Folder"; // **the path and name of your folder**<br/>
for ncha=1:bcha   %% Objects extraction<br/> &nbsp;&nbsp;&nbsp;&nbsp;
    [rcha,ccha] = find(Lcha==ncha);<br/> &nbsp;&nbsp;&nbsp;&nbsp;
    n1cha=ycha(min(rcha):max(rcha),min(ccha):max(ccha));<br/> &nbsp;&nbsp;&nbsp;&nbsp;
    figure, imshow(~n1cha);<br/> &nbsp;&nbsp;&nbsp;&nbsp;
    imwrite(n1cha, folder); **//code to save it to folder 'Folder' automatically**<br/>
end