def staff_center_filter()

in moonlight/staves/filter.py [0:0]


def staff_center_filter(image,
                        staffline_distance,
                        staffline_thickness,
                        threshold=127):
  """Filters the image for candidate staff center lines.

  Args:
    image: The 2D tensor image.
    staffline_distance: The estimated staffline distance. Scalar tensor.
    staffline_thickness: The estimated staffline thickness. Scalar tensor.
    threshold: Scalar tensor. Pixels below the threshold are black (possible
      stafflines).

  Returns:
    A boolean tensor of the same shape as image. The candidate center staff
        lines.
  """
  image = image < threshold

  # Add is not supported for unsigned ints, so use int8 instead of uint8.
  # Dark: the image is dark where we expect a staffline.
  dark_staffline_count = tf.zeros_like(image, tf.int8)
  # Space: the image is light above and below where we expect a staffline,
  # indicating a horizontal line.
  space_staffline_count = tf.zeros_like(image, tf.int8)
  for staffline_pos in range(-2, 3):
    expected_y_line = staffline_pos * staffline_distance
    # Allow each staffline to differ slightly from the expected position.
    # The second and fourth lines can differ by 1 pixel, and the first and fifth
    # lines can differ by 2 pixels.
    # At each possible location, look for a dark pixel and light space above and
    # below.
    found_dark = tf.zeros_like(image, tf.bool)
    found_space = tf.zeros_like(image, tf.bool)
    y_adjustments = range(-abs(staffline_pos), abs(staffline_pos) + 1)
    for y_adjustment in y_adjustments:
      y_line = expected_y_line + y_adjustment
      y_above = y_line - 2 * staffline_thickness
      y_below = y_line + 2 * staffline_thickness
      found_dark |= _shift_y(image, y_line)
      found_space |= tf.logical_not(
          tf.logical_or(_shift_y(image, y_above), _shift_y(image, y_below)))
    dark_staffline_count += tf.cast(found_dark, tf.int8)
    space_staffline_count += tf.cast(found_space, tf.int8)
  return tf.logical_and(
      tf.equal(dark_staffline_count, 5),
      tf.greater_equal(space_staffline_count, 3))