def staves(self): """The staves detected for a single staffline distance. Returns: A staves tensor of shape (N, 2, 2). """ height = tf.shape(self.image)[0] width = tf.shape(self.image)[1] staff_center = staves_filter.staff_center_filter( self.image, self.estimated_staffline_distance, self.estimated_staffline_thickness) all_thetas = tf.linspace(math.pi / 2 - self.max_abs_theta, math.pi / 2 + self.max_abs_theta, self.num_theta) hough_bins = hough.hough_lines(staff_center, all_thetas) staff_rhos, staff_thetas = hough.hough_peaks( hough_bins, all_thetas, minval=MIN_STAFF_SLICES * tf.cast(width, tf.float32), invalidate_distance=self.estimated_staffline_distance * 4) num_staves = tf.shape(staff_rhos)[0] # Interpolate the start and end points for the staff center line. x0 = tf.zeros([num_staves], tf.int32) y0 = tf.cast( tf.cast(staff_rhos, tf.float32) / tf.sin(staff_thetas), tf.int32) x1 = tf.fill([num_staves], width - 1) y1 = tf.cast((tf.cast(staff_rhos, tf.float32) - tf.cast(width - 1, tf.float32) * tf.cos(staff_thetas)) / tf.sin(staff_thetas), tf.int32) # Cut out staves which have a start or end y outside of the image. is_valid = tf.logical_and(tf.logical_and(0 <= y0, y0 < height), tf.logical_and(0 <= y1, y1 < height)) staves = tf.reshape(tf.stack([x0, y0, x1, y1], axis=1), [-1, 2, 2]) return tf.boolean_mask(staves, is_valid)
def testHoughPeaks_minvalTooLarge(self): image = np.asarray( [[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]]) # pyformat: disable thetas = np.linspace(0, np.pi / 2, 17) hough_bins = hough.hough_lines(image, thetas) peak_rho_t, peak_theta_t = hough.hough_peaks(hough_bins, thetas, minval=3.1) with self.test_session() as sess: peak_rho, peak_theta = sess.run((peak_rho_t, peak_theta_t)) self.assertEqual(peak_rho.shape, (0,)) self.assertEqual(peak_theta.shape, (0,))
def testHoughPeaks_verticalLines(self): image = np.asarray( [[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 1, 0], [0, 1, 0, 0, 0, 0, 1, 0]]) # pyformat: disable # Test the full range of angles. thetas = np.linspace(-np.pi, np.pi, 101) hough_bins = hough.hough_lines(image, thetas) peak_rho_t, peak_theta_t = hough.hough_peaks(hough_bins, thetas) with self.test_session() as sess: peak_rho, peak_theta = sess.run((peak_rho_t, peak_theta_t)) # Vertical line self.assertEqual(peak_rho[0], 1) self.assertAlmostEqual(peak_theta[0], 0) # Rotated line self.assertEqual(peak_rho[1], 3) self.assertAlmostEqual(peak_theta[1], -np.pi / 8, places=1)