title: Incorrect Mode Calculation or is this salt and pepper?
body: <p>I have a very simple usecase, I am gridding an image and calculating the most frequent occurring colour in each cell. </p>
  
  <p><a href="https://i.stack.imgur.com/jXFHf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jXFHf.png" alt="enter image description here"></a></p>
  
  <p>I have a problem where certain cells that <em>visually</em> look like black should be the most frequent colour have a mode of white (255). An example of this is cell (6,3) - <em>zero-based index column first then row</em>.</p>
  
  <p>It should be black shouldn't it but it turns out white. Is there an issue with my code or is there salt and pepper noise I cannot see?</p>
  
  <pre><code>def mode_filter(self, roi):
      values = np.zeros((1, 256), dtype="uint8")
  
      for pos, val in np.ndenumerate(roi):
          values[0, val] += 1
  
      print(values)
      return int(np.argmax(values[0]))
  
  def get_roi(self, src, pt1, pt2):
  
      col1, col2 = (pt1[0], pt2[0]) if pt1[0] &lt; pt2[0] else (pt2[0], pt1[0])
      row1, row2 = (pt1[1], pt2[1]) if pt1[1] &lt; pt2[1] else (pt2[1], pt1[1])
      return src[row1:row2, col1:col2]
  
  def grid_img(self, src, nCols=7, nRows=7):
  
      gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
      cells = np.zeros(gray.shape, dtype="uint8")
      cell_w = int(gray.shape[1] / nCols)
      cell_h = int(gray.shape[0] / nRows)
  
      for c in range(nCols): 
          for r in range(nRows):
              roi = self.get_roi(gray, (c*cell_w, r*cell_h), ((c+1)*cell_w, (r+1)*cell_h))
  
              mode = self.mode_filter(roi)
              cv2.rectangle(cells, (c*cell_w, r*cell_h), ((c+1)*cell_w, (r+1)*cell_h), (mode,), -1)
  
              cv2.imshow('roi', roi)
              cv2.imshow('cells', cells)
              cv2.imshow('src', src)
              print('{}, {}'.format((c,r), mode))
              cv2.waitKey(0)
  
      return cells
  </code></pre>
  
  <p>A full working example can be found here: <a href="https://pastebin.com/k7kUTmXG" rel="nofollow noreferrer">https://pastebin.com/k7kUTmXG</a></p>
  
  <p><a href="https://i.stack.imgur.com/KXYrf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KXYrf.png" alt="enter image description here"></a></p>