home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 3.ddi / DEMOS.DI$ / IMAGEDEM.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  5.2 KB  |  201 lines

  1. %IMAGEDEMO Demonstrate V4's image capability.
  2.  
  3. %       Copyright (c) 1984-93 by The MathWorks, Inc.
  4.  
  5. clf reset
  6. colormap(gray)
  7. format short
  8. format compact
  9. echo on
  10. clc
  11.  
  12. % For any matrix X, the command
  13. %
  14. %    image(X)
  15. % displays a graphical image with brightness or color chosen from
  16. % the elements of X used as indices into the colormap.  A simple,
  17. % visually interesting, example is
  18.  
  19.      X = spiral(8), image(X)
  20.  
  21. pause      % Press any key to continue.
  22.  
  23. clc
  24.  
  25. % The spiral pattern of the matrix elements is apparent in the display.
  26. % The small values in the center of the matrix are mapped to black
  27. % and dark gray, while the larger values around the edge of the matrix
  28. % are mapped to light gray and white.
  29.  
  30. X
  31.  
  32. pause      % Press any key to continue.
  33.  
  34. clc
  35.  
  36. % Color is added or changed with the "colormap" function.
  37. % For example
  38.  
  39.     colormap(hsv)
  40.  
  41. % varies the hue in the hue-saturation-value color model.
  42. % This is the default color map.  The map had been set with
  43. % colormap(gray) before echo on at the beginning of this script.
  44.  
  45. pause      % Press any key to continue.
  46.  
  47. clc
  48.  
  49. % A better map for this example is
  50.  
  51.     colormap(hot)
  52.  
  53. % This ranges from black through shades of red and yellow to white.
  54.  
  55. pause      % Press any key to continue.
  56.  
  57. clc
  58.  
  59. % The quantities "hsv" and "hot" used with the colormap function are,
  60. % of course, matrices.  (More precisely, they are the names of functions
  61. % which return matrices.)  Color map matrices have three columns which
  62. % specify intensities of the red, green and blue video components.  The
  63. % number of rows depends upon the particular image.  In this example, the
  64. % elements of X = spiral(8) range from 1 to 64, so we are using 64 rows.
  65.  
  66.     M = hot;
  67.     size(M)
  68.  
  69. pause      % Press any key to continue.
  70.  
  71. clc
  72.  
  73. % The elements of X are used as indices into the color map and so X must
  74. % have positive, integer elements between 1 and the length of the map.
  75. % To see how an individual color is determined, pick one element of X, say
  76.  
  77.     X(7,1)
  78.  
  79. % The corresponding color map entry is
  80.  
  81.     M(37,:)
  82.  
  83. % This has full intensity in the red gun, a little over half intensity
  84. % in the green gun, and no blue.  It produces the shade of orange in
  85. % the cell in the (7,1) position near the lower left corner.
  86.  
  87. pause      % Press any key to continue.
  88.  
  89. clc
  90.  
  91. % In general, the statements
  92.  
  93.     image(X), colormap(M)
  94.  
  95. % produces a display of colored cells where the RGB intensity of
  96. % the (i,j)-th cell is the 3-vector
  97. %
  98. %   M(X(i,j),:)
  99. %
  100. % The matrix X can be of any size, but its elements must be positive
  101. % integers between 1 and m.  The matrix M should then have m rows,
  102. % 3 columns, and elements between 0.0 and 1.0.
  103. %
  104. % colormap(M) also sets the colors used by pcolor(X), surf(Z) and
  105. % mesh(Z), but in these cases the data matrix, X or Z, is rescaled
  106. % to provide indices into the color map.
  107.  
  108. pause      % Press any key to continue.
  109.  
  110. clc
  111.  
  112. % A completely different feature of our spiral example is revealed by
  113.  
  114.     colormap(flag)
  115.  
  116. % The "flag" color map is simply m/4 copies of the matrix
  117.  
  118.     flag(4)
  119.  
  120. % stacked on top of each other.
  121.  
  122. pause      % Press any key to continue.
  123.  
  124. clc
  125.  
  126. % The colors red, white, blue and black are used cyclically as the
  127. % elements of X vary and so finer details of the image data become apparent.
  128. % In this example, we can see the diagonal patterns in the matrix
  129.  
  130.     rem(X,4)
  131.  
  132. pause      % Press any key to continue.
  133.  
  134. clc
  135.  
  136. % Since color maps are matrices, it is possible to modify them, or
  137. % create new ones, with MATLAB's array operations.  For example
  138. % the hot color map can be softened by adding some gray.
  139.  
  140.     S = (hot + gray)/2;
  141.     colormap(S)
  142.  
  143. pause
  144.  
  145. % This can be brightened by raising the elements of the color map
  146. % to a power less than 1.
  147.  
  148.     gamma = .6;
  149.     S = S.^gamma;
  150.     colormap(S)
  151.  
  152. pause      % Press any key to continue.
  153.  
  154. clc
  155.  
  156. % The command
  157.  
  158.     rgbplot(S)
  159.  
  160. % produces a plot of the color map.  The x-axis is the map index,
  161. % which corresponds to the elements of X in image(X), and the y-axis
  162. % is the intensity of the red, green and blue components.
  163.  
  164. pause      % Press any key to continue.
  165.  
  166. clc
  167.  
  168. % A sparse matrix display function, spy, is useful for displaying
  169. % the location of image elements which point to a particular color
  170. % map entry.  For example
  171.  
  172.     load cape
  173.     spy((X==2) | (X==3))
  174.  
  175. % loads a file containing altitude data for eastern New England and
  176. % displays all the elements which use the second or third element of
  177. % the color map.  Locations with X==1 correspond to sea level, so we
  178. % see a crude representation of the coast line.
  179.  
  180. pause      % Press any key to continue.
  181.  
  182. clc
  183.  
  184. % Our 8-by-8 spiral matrix is only a small, illustrative example.
  185. % Larger matrices resulting from extensive computations, or images
  186. % obtained from photographs, satellites or scanners are more typical.
  187. %
  188. % The demos directory contains several sample images with their own
  189. % color maps and the color directory contains M-files which generate
  190. % other useful color maps.
  191. %
  192. % Here is an interactive display, built with UIControls, which allows
  193. % you to explore these images and maps.
  194.  
  195. clf
  196. imageext
  197.  
  198. echo off
  199. format loose
  200.