code to record data in csv file from gui matlab

Discussion in 'Programmer's Corner' started by violette, Jan 4, 2011.

  1. violette

    Thread Starter New Member

    Jan 4, 2011
    1
    0
    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 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

    Code ( (Unknown Language)):
    1. [filename, pathname] = uigetfile( ...
    2.     {'*.jpg', 'All JPG-Files (*.jpg)'; ...
    3.         '*.*','All Files (*.*)'}, ...
    4.     'Open');
    5. % If "Cancel" is selected then return
    6. if isequal([filename,pathname],[0,0])
    7.     return
    8.     % Otherwise construct the fullfilename and Check and load the file.
    9. else
    10.     File = fullfile(pathname,filename);
    11.     % if the MAT-file is not valid, do not save the name
    12.     fopen(File);
    13.    
    14.     a=[pathname,filename]; %file directory
    15.  
    16.     set(handles.edit38,'String',filename);
    17.    
    18. i=imread(File);%load image"
    19. axes(handles.axes2);
    20. imshow(i);
    21.  
    22. r=rgb2gray(i);%grayscale image
    23. axes(handles.axes3);
    24. imshow(r);
    25.  
    26.  
    27. L = medfilt2(r,[5 5]);%filtered image
    28. axes(handles.axes4);
    29. imshow(L);
    30.  
    31. level=graythresh(L);%define treshvalue to convert image to binary
    32. c=im2bw(L,level);
    33. axes(handles.axes5);
    34. imshow(c);
    35.  
    36.  
    37. [xsize,ysize]=size(c);% read the size of the image
    38. xsize
    39. ysize
    40.  
    41.  
    42.  
    43. m00=0;
    44. m01=0;
    45. m10=0;
    46. m11=0;
    47. m12=0;
    48. m21=0;
    49. m02=0;
    50. m20=0;
    51. m03=0;
    52. m30=0;
    53.  
    54.  
    55.  
    56. for x=1:xsize
    57.     for y=1:ysize
    58.         if (~c(x,y)==0),
    59.                     m00=m00+1;
    60.                     m01=m01+(y);
    61.                     m10=m10+(x);
    62.                     m11=m11+(x*y);
    63.                     m02=m02+(y^2);
    64.                     m20=m20+(x^2);
    65.                     m12=m12+(x*y^2);
    66.                     m21=m21+(x^2*y);
    67.                     m03=m03+(y^3);
    68.                     m30=m30+(x^3);
    69.         end
    70.     end
    71. end
    72.  
    73.  
    74. %centroid
    75. z=x,y
    76. xbar=m10/m00;
    77. ybar=m01/m00;
    78.  
    79. xbar
    80. ybar
    81.  
    82. set(handles.edit36,'String',num2str(m10/m00));
    83. set(handles.edit43,'String',num2str(m01/m00));
    84.  
    85.  
    86. %calculate the area of binary image
    87. BW =~c;
    88. area= bwarea(BW);
    89. area
    90. imshow(BW);
    91. set(handles.edit37,'String',num2str(area));
    92.  
    93. bw2=~c;
    94.  
    95. perimeter=bwperim(bw2);
    96. perimeter=bwarea(perimeter);
    97. set(handles.edit44,'String',num2str(perimeter))%display to GUI
    98.  
    99. u00=m00;
    100. u11=m11-(m10.*(m01/m00))-((m10/m00).*m01)+((m10/m00).*(m10/m00)).*m00;
    101. u20=m20-(m10/m00).^2;
    102. u02=m02-(m01/m00).^2;
    103. 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;
    104. 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;
    105. u03=m03-2*m02.*(m01/m00)+m01.*((m01/m00).^2)+m02.*(m01/m00)+2*m01.*((m01/m00).^2)-((m01/m00).^3).*m00;
    106. u30=m30-2*m20.*(m10/m00)+m10.*((m10/m00).^2)+m20.*(m10/m00)+2*m10.*((m10/m00).^2)-((m10/m00).^3).*m00;
    107.  
    108. tmp1=u00.^2;
    109. tmp2=u00.^(2.5);
    110. n20=u20/tmp1;
    111. n11=u11/tmp1;
    112. n02=u02/tmp1;
    113. n30=u30/tmp2;
    114. n12=u12/tmp2;
    115. n21=u21/tmp2;
    116. n03=u03/tmp2;
    117.  
    118. n11
    119. n12
    120. n21
    121. n02
    122. n20
    123. n03
    124. n30
    125.  
    126. M1=n20+n02;
    127. M2=(n20-n02).^2+(4*(n11.^2));
    128. M3=((n30-3*n12).^2)+(3*n21-n03).^2;
    129. M4=((n30+n12).^2)+(n21+n03).^2;
    130. 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);
    131. M6=(n20+n02).*(((n30+n12).^2)-((n21+n03).^2))+4*n11.*(n30+n12).*(n21+n03);
    132. 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));
    133.  
    134. M1
    135. M2
    136. M3
    137. M4
    138. M5
    139. M6
    140. M7
    141.  
    142. set(handles.M1,'String',num2str(M1));
    143. set(handles.M2,'String',num2str(M2));
    144. set(handles.M3,'String',num2str(M3));
    145. set(handles.M4,'String',num2str(M4));
    146. set(handles.M5,'String',num2str(M5));
    147. set(handles.M6,'String',num2str(M6));
    148. set(handles.M7,'String',num2str(M7));
    149.  
    150.  
    151.  
    152. %~~~~~~~~~~~~~~~~~~~~~~~~use when train data~~~~~~~~~~~~~~~~~~~~~
    153. % stt=0; %use it when train data\\set grade value set 1 if pet, set 0 if nonpet
    154. % 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)];
    155. % Record (humoment,'C:\MATLABDATA\datatrain.csv') %use it when train data
    156. % %~~~~~~~~~~~~~~~~~~~~~~~~use when test data~~~~~~~~~~~~~~~~~~~~~~
    157. humoment=[num2str(M1) ',' num2str(M2) ',' num2str(M3) ',' num2str(M4) ',' num2str(M5) ',' num2str(M6) ',' num2str(M7) ',' num2str(perimeter) ',' num2str(area) ',' num2str(xbar) ',' num2str(ybar) ];
    158.  
    159. data= [filename ',' num2str(M1) ',' num2str(M2) ',' num2str(M3) ',' num2str(M4) ',' num2str(M5) ',' num2str(perimeter) ',' num2str(M6) ',' num2str(M7) ',' num2str(area) ',' num2str(xbar) ',' num2str(ybar)];
    160. Record(humoment,'C:\MATLABDATA\datatest.csv')
    161. Record(data,'C:\MATLABDATA\datatest.txt')  
    162. set(handles.edit45,'String','Load completed')
    163.  
    164. end
     
Loading...