title: R raster - counting black pixels within an extent
body: <p>My question is based upon <a href="https://stackoverflow.com/questions/29907919/r-raster-recognizing-black-color-raster-image">this</a>. Answers on that page solved the problem that i was facing at that time. My new question is - </p>
  
  <p>Below code plots two square on my image. For each square, I want to count how many black color pixels are within it. </p>
  
  <p>My original questions answers suggests that I can flatten a RGB using <code>flat &lt;- sum(x * c(0.2989, 0.5870, 0.1140))</code>. In such case, I want to count pixels that have color value below 25. </p>
  
  <p>I tried to leverage the answer suggested by the earlier post but couldnt figure out how to look within an extent.</p>
  
  <pre><code>library(raster)
  r1 &lt;- brick(system.file("external/rlogo.grd", package="raster"))
  x &lt;- crop(r1, extent(0,50,0,50))
  plotRGB(x)
  plot(extent(c(0,20,0,20)), lwd=2, col="red", add=TRUE)
  plot(extent(c(21,35,0,10)), lwd=2, col="Green", add=TRUE)
  </code></pre>