-1

I want to save height and width value from multiple objects in one text file, but it only saved the last processed image data. Here's my code:

contents = get(hObject,'Value')
pilih=guidata(gcbo);
theImage{1}=getImage(handles.axes1);
theImage{2}=getImage(handles.axes2);
theImage{3}=getImage(handles.axes3);
theImage{4}=getImage(handles.axes4);
theImage{5}=getImage(handles.axes5);

for z = 1:5;
    Img = theImage{z};
    abu=rgb2gray(Img);
    cb=imclearborder(abu);
    thresh=graythresh(cb);
    b=im2bw(cb,thresh);
    bw=bwareaopen(b,60);
    bwfill=imfill(bw,'holes');
    s=regionprops(bwfill,'BoundingBox');
    objects=cell(numel(s),1);
    for idx=1:numel(s)

        bb=floor(s(idx).BoundingBox);
        out=bsxfun(@times,Img,uint8(bwfill));
        objects{idx}=out(bb(2):bb(2)+bb(4),bb(1):bb(1)+bb(3),:);
    end
        X = zeros(3, numel(objects));
            for k = 1:numel(objects)
                k1=objects{k};
                c1=rgb2gray(k1);
                t1=graythresh(c1);
                biner1=im2bw(c1,t1);
                [height,width]=size(biner1);
                a1=bwarea(biner1);
                X(:,k)=[height;width;a1];
            end
end

save grading/datauji.txt X -ascii;

What should I do? Thank you very much. Here's my images. There are 5 of them, and I want to save all of the objects height and width data in one txt file.

sebenalern
  • 2,515
  • 3
  • 26
  • 36
M.luth
  • 11
  • 3

1 Answers1

0

Try this code,

code

numel = 100;
file = fopen('ans.txt','w');
testdata = rand(3, numel);
X = zeros(3, numel);
for i=1:numel
    X(:,i) = testdata(:,i);

    fprintf(file,'%ld %ld %ld\n', X(1,i), X(2,i), X(3,i));
end

fclose(file);

I have created a dummy number of data points i.e 100. I have created random data in variable in 'testdata', then I have iterated 'numel' and every time saved saved all 3 data in comma separated type

ans.txt

11.0 2.2 3.1 
4.2 5.2 1.1
32.1 542 12.1
12. 12 23

Hope this works.

Raj Samant
  • 41
  • 6
  • I tried and got this error message: ??? Index exceeds matrix dimensions. Error in ==> guikedelaizulfa>ekstrakfitur_Callback at 1158 fprintf(file,'%ld %ld %ld\n', X(height), X(width), X(a1)); Error in ==> gui_mainfcn at 96 feval(varargin{:}); Error in ==> guikedelaizulfa at 42 gui_mainfcn(gui_State, varargin{:}); Error in ==> @(hObject,eventdata)guikedelaizulfa('ekstrakfitur_Callback',hObject,eventdata,guidata(hObject)) ??? Error while evaluating uicontrol Callback – M.luth Feb 18 '16 at 16:15
  • You cannot use fprintf(file,'%ld %ld %ld\n', X(height), X(width), X(a1));, it will generate error, as X matrix limit is exceeded. try this instead. `fprintf(file,'%ld %ld %ld\n', height, width, a1);` or `fprintf(file,'%ld %ld %ld\n', X(1,k), X(2,k), X(3,k));` as it is. – Raj Samant Feb 18 '16 at 18:35
  • I tried your suggestion, but it only saved 1x25 matriks (consist of height, width and a1) data when there should be 3x60 (height; width; a1). I will post my image as your reference – M.luth Feb 18 '16 at 23:00
  • try to put file opening code outside `z loop` the it should save (z*numel)x3 matrix in text file – Raj Samant Feb 20 '16 at 05:50