如何通过函数的for循环将多个图像作为输出结果返回到主

时间:2019-03-27 07:44:14

标签: matlab

我想将函数中的所有分割线图像结果都放到主行中,但它只返回输入图像的最后一行。

这是行分段代码的功能:

function mat5 = charextract(ipimg)
%%%aa=imread('num_ip.tif');
bb=rgb2gray(ipimg);
cc=imbinarize(bb);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%   SEGMENTATION    %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[x,y]=size(cc);
ee=sum(cc,2);
mat2=y-ee;
mat3=mat2~=0;
mat4=diff(mat3);
index1=find(mat4);
[q,w]=size(index1);%size of index2 matrix is q*w
kap=1;
lam=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,:)=cc(j,:); %store the line segmented matrix
         %aadd{j}=mat5(k,:);
         k=k+1;
      end
        lam=lam+2;   
        kap=kap+1;
        %aa=mat5+1;
        %figure, imshow(mat5);
end
end

这是主要代码:

close all;
clear all;
clc;
img=imread('num_ip.tif');
img2=charextract(img);
imshow(img2);

1 个答案:

答案 0 :(得分:0)

问题在于,每次while循环运行时,您都要清除mat5容器。如果将其移出while循环,则应该可以解决问题。

编辑:每次while循环运行时,K也被设置为1。这意味着在for循环的第一次迭代之后。 K将等于1 k=1,并将覆盖您之前的数据。

function mat5 = charextract(ipimg)
%%%aa=imread('num_ip.tif');
bb=rgb2gray(ipimg);
cc=imbinarize(bb);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%   SEGMENTATION    %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[x,y]=size(cc);
ee=sum(cc,2);
mat2=y-ee;
mat3=mat2~=0;
mat4=diff(mat3);
index1=find(mat4);
[q,w]=size(index1);%size of index2 matrix is q*w
kap=1;
lam=1;
mat5=([]); % moved out of while loop
k=1;

while kap<((q/2)+1)%number of loops=number of lines

      for j=(index1(lam)+1):1:index1(lam+1)
         mat5(k,:)=cc(j,:); %store the line segmented matrix
         %aadd{j}=mat5(k,:);
         k=k+1;
      end
        lam=lam+2;   
        kap=kap+1;
        %aa=mat5+1;
        %figure, imshow(mat5);
end
end