title: Selection of data in a region of interest in a matrix (with Mathematica)
body: <p>I am having a (mathematica 8.0.1.0-) problem which I cannot solve by myself. 
  I have measurement data in a matrix, I want to select some of them and then add them up. </p>
  
  <p>To explain my problem better, here an easy example. The data can be produced by a matrix S:</p>
  
  <pre><code>S = Table[ -Sin[i/2] - Sin[j/2], {i,20}, {j,20}];
  </code></pre>
  
  <p>They can be nicely plotted with:</p>
  
  <pre><code>xmin = N[Min[S]]; 
  xmax = N[Max[S]]; 
  mycolorfun = Function[ Blend[{Blue,Cyan,Green,Yellow,Red},#] ];
  </code></pre>
  
  <p>and</p>
  
  <pre><code>MatrixPlot[S, PlotRange -&gt; {All,All,All}, AspectRatio -&gt; 1/1,
     ColorFunction -&gt; (mycolorfun[ Rescale[ #1{xmin,xmax} ] ]&amp;),
     ColorFunctionScaling -&gt; False, MaxPlotPoints -&gt; Automatic,
     FrameLabel -&gt; {y,x} ]
  </code></pre>
  
  <p>Then one should get a picture similar to this one:</p>
  
  <p><img src="https://i.stack.imgur.com/QQZZM.jpg" alt="enter image description here"> </p>
  
  <p>Now I want to select the data which are inside of the brown drawn polygon. These data should be added up at the end. </p>
  
  <p>How can I do this? 
  Ok, I could use rectangles and build a sub-matrix by choosing/guessing good start and end indices. Then I just have to build the sum of this sub-matrix. But I would prefer polygons (more precise if we do not argue about little problems with the matrix values which are crossed by the line of the polygon). And I would love it if I could select my region of interest (ROI) directly by “painting” the polygon into the Matrix (no more time consuming choosing/guessing of matrix indices).<br>
  Could somebody here help me with my problem? If it is not solvable with mathematica, is there some other program which I could use?</p>
  
  <p>I would be very happy about some help and hints! </p>