Sun, 22/Oct/2017 16:50

Some years ago I wrote a script to take output images from the Arbitrary Transmission Line Calculator (ATLC) and convert them to quantitave contour plots. This is work I did with Scott Jones, David Robinson and Mark Blonquist. I am starting to remember more of that work. I will keep a copy here for reference

  1. function [OUTPUT,vals]=bin_to_contour_rowsplit(pixel_width,pixel_height,width_in_meters,height_in_meters,num_contours,filename,percent_lim,split)
  2. %This MAtlab function was created by:
  3. % Seth Humphries 10-MAY-2004
  4. % It if for use with the ATLC program and it imports the binary data that ATLC generates,
  5. % and creates a contour plot of this data.
  6. %
  7. %NOTE: for large values of pixel_height and pixel_width this will take a LONG time to process
  8. %
  9. %An example of how to call this program from the matlab command prompt is as follows:
  10. % [outputmatrix,inputvalues]=bin_to_contour(600,600,0.001,0.001,60,'BLAH.U.bin');
  11. % where BLAH is the name of whatever image process by ATLC, 0.001 is 1mm, and the data is 600 by 600 pixels
  12. %
  13. %The following is an explanation of the values input by the user:
  14. %pixel_width is the width of the original image in pixels
  15. %pixel_height is the height of the original image in pixels
  16. %width_in_meters is the physical width of the original image in meters
  17. %height_in_meters is the physical height of the original image in meters
  18. % the units of output in the .bin file from ATLC are in joules/(m*pixel^2) those values
  19. % must be adjusted for the diference in units (should be joules/(m^3) )
  20. % see conversionH and conversionW (which must be the same for the program to work).
  21. %
  22. %num_countours is the number of contours desired by the user.
  23. % they are in even increments between 100% and 1% of maximum power
  24. %
  25. %filename is the file from which to import the data, it must be the .bin output from the ATLC program
  26. % the extension of filename is altered to .dat and "_Xpercent" (where X is percent_lim) is added to the end of the name.
  27. % The data is exported as comma seperated values so that the data may be read into other
  28. % programs such as excel, sigma plot, etc. At the top of the data file statistic values from the
  29. % data are placed for the user's information
  30.  
  31. percent=percent_lim/100;
  32. %read in the binary data
  33. fid = fopen(filename,'r');
  34. [INPUT,count] = fread(fid,'double');
  35. fclose(fid);
  36. average=1;
  37. area=1;
  38.  
  39. %find the conversion factor
  40. conversionH=height_in_meters/pixel_height;
  41. conversionW=width_in_meters/pixel_width;
  42.  
  43. %verify that the factor is the same in both directions and if so apply it to the matrix
  44. if (conversionW == conversionH)
  45. conversionH=1/conversionW;
  46. INPUT = INPUT .*conversionH;
  47. average=conversionH;
  48. area=conversionH;
  49. else
  50. % error=['The height conversion factor is not the same';'as the width conversion factor. The contour ';'plot is not adjusted for units ';]
  51. error=['The height conversion factor is not the same as the width conversion factor. The contour plot is not adjusted for units ']
  52. end;
  53.  
  54. %convert the data from a vector to a matrix of dimensions "pixel_height by pixel_width"
  55. Asize=max(size(INPUT));
  56. maximum= max(INPUT);
  57. one=maximum*percent;
  58. row=1;col=1;q=1;r=1;
  59. sum=0;count=0;p=1;
  60. for (i=1:Asize);
  61. if (INPUT(i) > 0.0)
  62. count=count+1;
  63. if (INPUT(i) >= one)
  64. data(p)=INPUT(i);
  65. p=p+1;
  66. if (row >=split)
  67. top_half(q)=INPUT(i);
  68. q=q+1;
  69. else
  70. bot_half(r)=INPUT(i);
  71. r=r+1;
  72. end;
  73. end;
  74. end;
  75. OUTPUT(row,col) = INPUT(i);
  76. if (col == pixel_width)
  77. row=mod(row,pixel_height)+1;
  78. end;
  79. col=mod(col,pixel_width)+1;
  80. end;
  81. average=average*sum/count; %average is either 1 or conversionH
  82. area = p/(area*area); %area is either 1 or conversionH
  83.  
  84. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% this part of the function calculates how to adjust the
  85. % axes of the figure to fit the 1percent contour nicely on the plot
  86. % ymin=0;ymax=0;xmin=0;xmax=0;
  87. % i=1;j=1;
  88. % while (ymin == 0 && i<=pixel_width)
  89. % while (ymin == 0 && j<=pixel_height)
  90. % if (OUTPUT(i,j) >= one)
  91. % ymin=i;
  92. % end;
  93. % j=j+1;
  94. % end;
  95. % j=1;
  96. % i=i+1;
  97. % end;
  98. %
  99. % i=1;j=1;
  100. % while (xmin == 0 && j<=pixel_height)
  101. % while (xmin == 0 && i<=pixel_width)
  102. % if (OUTPUT(i,j) >= one)
  103. % xmin=j;
  104. % end;
  105. % i=i+1;
  106. % end;
  107. % i=1;
  108. % j=j+1;
  109. % end;
  110. %
  111. % i=pixel_height;j=1;
  112. % while (ymax == 0 && i>=1)
  113. % while (ymax == 0 && j<=pixel_height)
  114. % if (OUTPUT(i,j) >= one)
  115. % ymax=i;
  116. % end;
  117. % j=j+1;
  118. % end;
  119. % j=1;
  120. % i=i-1;
  121. % end;
  122. %
  123. % j=pixel_width;i=1;
  124. % while (xmax == 0 && j>=1)
  125. % while (xmax == 0 && i<=pixel_width)
  126. % if (OUTPUT(i,j) >= one)
  127. % xmax=j;
  128. % end;
  129. % i=i+1;
  130. % end;
  131. % i=1;
  132. % j=j-1;
  133. % end;
  134.  
  135. %caculate where to place the contours
  136. incr=(1/num_contours);
  137. vals=[percent:incr:1].*maximum;
  138.  
  139. w=0.05*pixel_width;
  140. h=0.05*pixel_height;
  141. %create a new figure and plot a contour on it. It is faster for smaller number of contour lines
  142. fig_id=figure;
  143. contourf(OUTPUT,vals); %vals can be replaced with a number specifying how many contours
  144. %to draw, matlab then calculates where to place them.
  145. %set(gca,'YLim',[ymin-h ymax+h]);
  146. %set(gca,'XLim',[xmin-w xmax+w]);
  147. fsize=max(size(filename));
  148. xlabel('Distance [cm]');
  149. ylabel('Distance [cm]');
  150. ylim([0 pixel_height]);
  151. xlim([0 pixel_width]);
  152. yticks=0:pixel_height/4:pixel_height;
  153. xticks=0:pixel_width/4:pixel_width;
  154. set(gca,'ytick',yticks);
  155. set(gca,'yticklabel',[-6.0 -3.0 0.0 3.0 6.0]);
  156. set(gca,'xtick',xticks);
  157. set(gca,'xticklabel',[-6.0 -3.0 0.0 3.0 6.0]);
  158.  
  159. % title(filename(1:fsize-4));
  160. tempname=[filename(1:fsize-4),num2str(percent_lim),'percent_contour_rowsplit.fig'];
  161. saveas(fig_id,tempname,'fig'); %save in Matlab's native format ".fig"
  162. tempname=[filename(1:fsize-4),num2str(percent_lim),'percent_contour_rowsplit.jpg'];
  163. saveas(fig_id,tempname,'jpg'); %save as a Jpeg image
  164.  
  165.  
  166. % Output the data to a data file as described below
  167. % format will be comma seperate values. this takes SOME time
  168. tempname=filename(fsize-4:fsize);
  169. if (tempname(1:4) ~= '.dat')
  170. outputname=[filename(1:fsize-4),num2str(percent_lim),'percent_rowsplit.dat'];
  171. surf_file=[filename(1:fsize-4),num2str(percent_lim),'percent_rowsplit_for_plotting_in_surfer.dat'];
  172. else
  173. outputname=[filename(1:fsize-4),'unbinary',num2str(percent_lim),'percent_rowsplit.dat'];
  174. surf_file=[filename(1:fsize-4),'unbinary',num2str(percent_lim),'percent_rowsplit_for_plotting_in_surfer.dat'];
  175. end;
  176. outID=fopen(outputname,'w');
  177. surf_ID=fopen(surf_file,'w');
  178. fprintf(outID,'Stats for all the data:\n');
  179. fprintf(outID,'Number of Pixels greater than %g percent = %g',percent_lim,p);
  180. fprintf(outID,'\nArea of Pixels greater than %g percent in meter^2 = %g',percent_lim,area);
  181. fprintf(outID,'\nMaximum= %g',maximum);
  182. fprintf(outID,'\nMinium= %g',min(data));
  183. fprintf(outID,'\nMean= %g',mean(data));
  184. fprintf(outID,'\nMedian= %g',median(data));
  185. fprintf(outID,'\nStandard Deviation= %g',std(data));
  186. fprintf(outID,'\nCovariance= %g',cov(data));
  187. fprintf(outID,'\nCorrelation Coefficients= %g',corrcoef(data));
  188. fprintf(outID,'\n\n'); %2 lines after the information above
  189.  
  190. fprintf(outID,'Stats for top part of the data:\n');
  191. fprintf(outID,'Number of Pixels greater than %g percent = %g',percent_lim,q);
  192. fprintf(outID,'\nArea of Pixels greater than %g percent in meter^2 = %g',percent_lim,q/(conversionH*conversionH));
  193. fprintf(outID,'\nMaximum= %g',max(top_half));
  194. fprintf(outID,'\nMinium= %g',min(top_half));
  195. fprintf(outID,'\nMean= %g',mean(top_half));
  196. fprintf(outID,'\nMedian= %g',median(top_half));
  197. fprintf(outID,'\nStandard Deviation= %g',std(top_half));
  198. fprintf(outID,'\nCovariance= %g',cov(top_half));
  199. fprintf(outID,'\nCorrelation Coefficients= %g',corrcoef(top_half));
  200. fprintf(outID,'\n\n'); %2 lines after the information above
  201.  
  202. fprintf(outID,'Stats for bottom part of the data:\n');
  203. fprintf(outID,'Number of Pixels greater than %g percent = %g',percent_lim,r);
  204. fprintf(outID,'\nArea of Pixels greater than %g percent in meter^2 = %g',percent_lim,r/(conversionH*conversionH));
  205. fprintf(outID,'\nMaximum= %g',max(bot_half));
  206. fprintf(outID,'\nMinium= %g',min(bot_half));
  207. fprintf(outID,'\nMean= %g',mean(bot_half));
  208. fprintf(outID,'\nMedian= %g',median(bot_half));
  209. fprintf(outID,'\nStandard Deviation= %g',std(bot_half));
  210. fprintf(outID,'\nCovariance= %g',cov(bot_half));
  211. fprintf(outID,'\nCorrelation Coefficients= %g',corrcoef(bot_half));
  212. fprintf(outID,'\n\n'); %2 lines after the information above
  213.  
  214. x_per=100*width_in_meters/pixel_width;%cm
  215. y_per=100*height_in_meters/pixel_height;
  216. for (i=1:pixel_height)%rows
  217. for(j=1:pixel_width)%columns
  218. if (OUTPUT(i,j) >= one)
  219. fprintf(outID,'%g,',OUTPUT(i,j)); % use OUTPUT so that the data structure is intact
  220. fprintf(surf_ID,'%g,%g,%g\n',(j-pixel_width/2)*x_per,(i-pixel_height/2)*y_per,OUTPUT(i,j));
  221. else
  222. fprintf(outID,',');
  223. end;
  224. fprintf(surf_ID,'%g,%g,%g\n',(j-pixel_width/2)*x_per,(i-pixel_height/2)*y_per,0);
  225. end;
  226. fprintf(outID,'\n'); %at the end of each row put a carraige return
  227. end;
  228. fclose(outID); %close the output data file
  229. fclose(surf_ID);
  230.  

Comments:

Leave a Reply



(Your email will not be publicly displayed.)


Captcha Code

Click the image to see another captcha.