CrazyEngineers
  • code to record data in csv file from gui matlab

    violet

    violet

    @violet-7grJvM
    Updated: Oct 25, 2024
    Views: 1.3K
    Hi, i'm new to Matlab. i have this code that extract image feature using gui. i have problem creating a code that can record data extracted into csv and txt file. i want the data extracted from image are recorded into a single csv file each time an image is load into the gui. the data will be use to train a neural network. can some one help me? tq

    [filename, pathname] = uigetfile( ...
        {'*.jpg', 'All JPG-Files (*.jpg)'; ...
            '*.*','All Files (*.*)'}, ...
        'Open');
    % If "Cancel" is selected then return
    if isequal([filename,pathname],[0,0])
        return
        % Otherwise construct the fullfilename and Check and load the file.
    else
        File = fullfile(pathname,filename);
        % if the MAT-file is not valid, do not save the name
        fopen(File);
        
        a=[pathname,filename]; %file directory
    
        set(handles.edit38,'String',filename);
        
    i=imread(File);%load image"
    axes(handles.axes2);
    imshow(i);
    
    r=rgb2gray(i);%grayscale image
    axes(handles.axes3);
    imshow(r);
    
    
    L = medfilt2(r,[5 5]);%filtered image
    axes(handles.axes4);
    imshow(L);
    
    level=graythresh(L);%define treshvalue to convert image to binary
    c=im2bw(L,level);
    axes(handles.axes5);
    imshow(c);
    
    
    [xsize,ysize]=size(c);% read the size of the image
    xsize
    ysize
    
    
    
    m00=0;
    m01=0;
    m10=0;
    m11=0;
    m12=0;
    m21=0;
    m02=0;
    m20=0;
    m03=0;
    m30=0;
    
    
    
    for x=1:xsize
        for y=1:ysize
            if (~c(x,y)==0),
                        m00=m00+1;
                        m01=m01+(y);
                        m10=m10+(x);
                        m11=m11+(x*y);
                        m02=m02+(y^2);
                        m20=m20+(x^2);
                        m12=m12+(x*y^2);
                        m21=m21+(x^2*y);
                        m03=m03+(y^3);
                        m30=m30+(x^3);
            end
        end
    end
    
    
    %centroid
    z=x,y
    xbar=m10/m00;
    ybar=m01/m00;
    
    xbar
    ybar
    
    set(handles.edit36,'String',num2str(m10/m00));
    set(handles.edit43,'String',num2str(m01/m00));
    
    
    %calculate the area of binary image
    BW =~c;
    area= bwarea(BW);
    area
    imshow(BW);
    set(handles.edit37,'String',num2str(area));
    
    bw2=~c;
    
    perimeter=bwperim(bw2);
    perimeter=bwarea(perimeter);
    set(handles.edit44,'String',num2str(perimeter))%display to GUI
    
    u00=m00;
    u11=m11-(m10.*(m01/m00))-((m10/m00).*m01)+((m10/m00).*(m10/m00)).*m00;
    u20=m20-(m10/m00).^2;
    u02=m02-(m01/m00).^2;
    u12=m12-2*m11.*(m01/m00)+m10.*((m01/m00).^2)-m02.*(m10/m00)+2*m01.*(m01/m00).*(m10/m00)-((m10/m00).^2).*(m10/m00).*m00;
    u21=m21-2*m11.*(m10/m00)+m01.*((m10/m00).^2)-m20.*(m01/m00)+2*m10.*(m10/m00).*(m01/m00)-((m10/m00).^2).*(m01/m00).*m00;
    u03=m03-2*m02.*(m01/m00)+m01.*((m01/m00).^2)+m02.*(m01/m00)+2*m01.*((m01/m00).^2)-((m01/m00).^3).*m00;
    u30=m30-2*m20.*(m10/m00)+m10.*((m10/m00).^2)+m20.*(m10/m00)+2*m10.*((m10/m00).^2)-((m10/m00).^3).*m00;
    
    tmp1=u00.^2;
    tmp2=u00.^(2.5);
    n20=u20/tmp1;
    n11=u11/tmp1;
    n02=u02/tmp1;
    n30=u30/tmp2;
    n12=u12/tmp2;
    n21=u21/tmp2;
    n03=u03/tmp2;
    
    n11
    n12
    n21
    n02
    n20
    n03
    n30
    
    M1=n20+n02;
    M2=(n20-n02).^2+(4*(n11.^2));
    M3=((n30-3*n12).^2)+(3*n21-n03).^2;
    M4=((n30+n12).^2)+(n21+n03).^2;
    M5=(n30-3*n12).*(n30+n12)+(((n30+n12).^2)-3*((n21-n03).^2))+(3*n21-n03)*(n21+n03)*(3*((n30+n12).^2)-(n21-n03).^2);
    M6=(n20+n02).*(((n30+n12).^2)-((n21+n03).^2))+4*n11.*(n30+n12).*(n21+n03);
    M7=(3*n21-n03).*(n30+n12).*(((n30-n12).^2)-3*((n21+n03).^2))+(3*n12-n30).*(n21+n03).*(3*((n12+n30).^2)-((n21+n03).^2));
    
    M1
    M2
    M3
    M4
    M5
    M6
    M7
    
    set(handles.M1,'String',num2str(M1));
    set(handles.M2,'String',num2str(M2));
    set(handles.M3,'String',num2str(M3));
    set(handles.M4,'String',num2str(M4));
    set(handles.M5,'String',num2str(M5));
    set(handles.M6,'String',num2str(M6));
    set(handles.M7,'String',num2str(M7));
    
    
    
    %~~~~~~~~~~~~~~~~~~~~~~~~use when train data~~~~~~~~~~~~~~~~~~~~~
    % stt=0; %use it when train data\\set grade value set 1 if pet, set 0 if nonpet
    % humoment=[num2str(M1) ',' num2str(M2) ',' num2str(M3) ',' num2str(M4) ',' num2str(M5) ',' num2str(M6) ',' num2str(M7) ',' num2str(perimeter) ',' num2str(area) ',' num2str(xbar) ',' num2str(ybar) ',' num2str(stt)];
    % Record (humoment,'C:\MATLABDATA\datatrain.csv') %use it when train data
    % %~~~~~~~~~~~~~~~~~~~~~~~~use when test data~~~~~~~~~~~~~~~~~~~~~~
    humoment=[num2str(M1) ',' num2str(M2) ',' num2str(M3) ',' num2str(M4) ',' num2str(M5) ',' num2str(M6) ',' num2str(M7) ',' num2str(perimeter) ',' num2str(area) ',' num2str(xbar) ',' num2str(ybar) ];
    
    data= [filename ',' num2str(M1) ',' num2str(M2) ',' num2str(M3) ',' num2str(M4) ',' num2str(M5) ',' num2str(perimeter) ',' num2str(M6) ',' num2str(M7) ',' num2str(area) ',' num2str(xbar) ',' num2str(ybar)];
    Record(humoment,'C:\MATLABDATA\datatest.csv')
    Record(data,'C:\MATLABDATA\datatest.txt')  
    set(handles.edit45,'String','Load completed')
    
    end
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Home Channels Search Login Register