def test_hough_peaks_dist(): img = np.zeros((100, 100), dtype=np.bool_) img[:, 30] = True img[:, 40] = True hspace, angles, dists = tf.hough(img) assert len(tf.hough_peaks(hspace, angles, dists, min_distance=5)[0]) == 2 assert len(tf.hough_peaks(hspace, angles, dists, min_distance=15)[0]) == 1
def test_hough_angles(): img = np.zeros((10, 10)) img[0, 0] = 1 out, angles, d = tf.hough(img, np.linspace(0, 360, 10)) assert_equal(len(angles), 10)
def test_hough_peaks_angle(): img = np.zeros((100, 100), dtype=np.bool_) img[:, 0] = True img[0, :] = True hspace, angles, dists = tf.hough(img) assert len(tf.hough_peaks(hspace, angles, dists, min_angle=45)[0]) == 2 assert len(tf.hough_peaks(hspace, angles, dists, min_angle=90)[0]) == 1 theta = np.linspace(0, np.pi, 100) hspace, angles, dists = tf.hough(img, theta) assert len(tf.hough_peaks(hspace, angles, dists, min_angle=45)[0]) == 2 assert len(tf.hough_peaks(hspace, angles, dists, min_angle=90)[0]) == 1 theta = np.linspace(np.pi / 3, 4. / 3 * np.pi, 100) hspace, angles, dists = tf.hough(img, theta) assert len(tf.hough_peaks(hspace, angles, dists, min_angle=45)[0]) == 2 assert len(tf.hough_peaks(hspace, angles, dists, min_angle=90)[0]) == 1
def test_hough(): # Generate a test image img = np.zeros((100, 100), dtype=int) for i in range(25, 75): img[100 - i, i] = 1 out, angles, d = tf.hough(img) y, x = np.where(out == out.max()) dist = d[y[0]] theta = angles[x[0]] assert_equal(dist > 70, dist < 72) assert_equal(theta > 0.78, theta < 0.79)
def hough_detect(diffs, vote_thresh=15): """ Use the Hough detection method to detect lines in the data. With enough lines, you can fill in the wave front.""" detection = [] print("Performing hough transform on binary maps...") for img in diffs: # Perform the hough transform on each of the difference maps transform, theta, d = hough(img) # Filter the hough transform results and find the best lines in the # data. Keep detections that exceed the Hough vote threshold. indices = (transform > vote_thresh ).nonzero() distances = d[indices[0]] theta = theta[indices[1]] n =len(indices[1]) print("Found " + str(n) + " lines.") # Perform the inverse transform to get a series of rectangular # images that show where the wavefront is. # Create a map which is the same as the invTransform = sunpy.make_map(np.zeros(img.shape), img._original_header) invTransform.data = np.zeros(img.shape) # Add up all the detected lines over each other. The idea behind # adding up all the lines on top of each other is that pixels that # have larger number of detections are more likely to be in the # wavefront. Note that we are using th Hough transform - which is used # to detect lines - to detect and fill in a region. You might see this # as an abuse of the Hough transform! for i in range(0,len(indices[1])): nextLine = htLine(distances[i], theta[i], np.zeros(shape=img.shape)) invTransform = invTransform + nextLine detection.append(invTransform) return detection
diffs = [] diffmap = 255*(abs(input_maps[1] - input_maps[0]) > diffthresh) for i in range(0,ndiff): # difference map diffmap = 255*(abs(input_maps[i+1] - input_maps[i]) > diffthresh) # keep diffs.append(diffmap) # extract the image img = diffmap # Perform the hough transform on each of the difference maps transform,theta,d = hough(img) # Filter the hough transform results and find the best lines # in the data indices = (transform >votethresh).nonzero() distances = d[indices[0]] theta = theta[indices[1]] n =len(indices[1]) print("Found " + str(n) + " lines.") # Perform the inverse transform to get a series of rectangular # images that show where the wavefront is. invTransform = sunpy.map.BaseMap(input_maps[i+1]) invTransform.data = np.zeros(imgShape) for i in range(0,n): nextLine = htLine( distances[i],theta[i], np.zeros(shape=imgShape) )
import numpy as np import matplotlib.pyplot as plt # Construct test image image = np.zeros((100, 100)) # Classic straight-line Hough transform idx = np.arange(25, 75) image[idx[::-1], idx] = 255 image[idx, idx] = 255 h, theta, d = hough(image) plt.figure(figsize=(8, 4)) plt.subplot(121) plt.imshow(image, cmap=plt.cm.gray) plt.title('Input image') plt.subplot(122) plt.imshow(np.log(1 + h), extent=[np.rad2deg(theta[-1]), np.rad2deg(theta[0]), d[-1], d[0]], cmap=plt.cm.gray, aspect=1/1.5) plt.title('Hough transform') plt.xlabel('Angles (degrees)') plt.ylabel('Distance (pixels)')
#temp = 255*(abs(input_maps[14] - input_maps[13]) > diffthresh) for i in range(0,ndiff): # difference map - create separate maps for +ve and -ve differences diffmap_plus = 255*((input_maps[i+1] - input_maps[i]) > diffthresh) diffmap_minus = 255*((input_maps[i] - input_maps[i+1]) > diffthresh) # keep diffs.append(diffmap_plus) # extract the image img = diffmap_plus img2 = diffmap_minus # Perform the hough transform on the positive and negative difference maps separately transform,theta,d = hough(img) transform2,theta2,d2 = hough(img2) # Filter the hough transform results and find the best lines # in the data #indices = (transform >votethresh).nonzero() #indices = (transform == transform.max()).nonzero() #instead of getting all lines above some threshold, just get the *strongest* line only #from the positive diffmap and the negative diffmap. May get more than 2 lines due to ties #in the accumulator #indices=((transform == transform.max())+(transform2 == transform2.max())).nonzero() indices = ((transform> votethresh)+(transform2 > votethresh)).nonzero()
from skimage import data import numpy as np import matplotlib.pyplot as plt # Construct test image image = np.zeros((100, 100)) # Classic straight-line Hough transform idx = np.arange(25, 75) image[idx[::-1], idx] = 255 image[idx, idx] = 255 h, theta, d = hough(image) plt.figure(figsize=(8, 4)) plt.subplot(121) plt.imshow(image, cmap=plt.cm.gray) plt.title('Input image') plt.subplot(122) plt.imshow(np.log(1 + h), extent=[np.rad2deg(theta[-1]), np.rad2deg(theta[0]), d[-1], d[0]], cmap=plt.cm.gray, aspect=1 / 1.5) plt.title('Hough transform') plt.xlabel('Angles (degrees)')
import numpy as np import matplotlib.pyplot as plt from skimage.transform import hough img = np.zeros((100, 150), dtype=bool) img[30, :] = 1 img[:, 65] = 1 img[35:45, 35:50] = 1 for i in range(90): img[i, i] = 1 img += np.random.random(img.shape) > 0.95 out, angles, d = hough(img) plt.subplot(1, 2, 1) plt.imshow(img, cmap=plt.cm.gray) plt.title('Input image') plt.subplot(1, 2, 2) plt.imshow(out, cmap=plt.cm.bone, extent=(d[0], d[-1], np.rad2deg(angles[0]), np.rad2deg(angles[-1]))) plt.title('Hough transform') plt.xlabel('Angle (degree)') plt.ylabel('Distance (pixel)') plt.subplots_adjust(wspace=0.4) plt.show()
import Whale as w spec_data = w.get_spectrogram(5) munged_data = ndimage.gaussian_filter(spec_data, sigma=1.0) black_white = munged_data > 2.0 * munged_data.mean() data = black_white # Compute the medial axis (skeleton) and the distance transform skel, distance = medial_axis(data, return_distance=True) skel1 = skeletonize(data) # Distance to the background for pixels of the skeleton dist_on_skel = distance * skel dist_on_skel1 = distance * skel1 h, theta, d = hough(skel1) lines = probabilistic_hough(skel1, threshold=6, line_length=5, line_gap=3) lines = sorted(lines) plt.figure(figsize=(8, 4)) plt.subplot(141) plt.imshow(data, cmap=plt.cm.gray, interpolation='nearest') plt.axis('off') plt.subplot(142) plt.imshow(dist_on_skel, cmap=plt.cm.spectral, interpolation='nearest') plt.contour(data, [0.5], colors='w') plt.axis('off') plt.subplot(143) plt.imshow(dist_on_skel1, cmap=plt.cm.spectral, interpolation='nearest') plt.contour(data, [0.5], colors='w') plt.axis('off')
#temp = 255*(abs(input_maps[14] - input_maps[13]) > diffthresh) for i in range(0, ndiff): # difference map - create separate maps for +ve and -ve differences diffmap_plus = 255 * ((input_maps[i + 1] - input_maps[i]) > diffthresh) diffmap_minus = 255 * ((input_maps[i] - input_maps[i + 1]) > diffthresh) # keep diffs.append(diffmap_plus) # extract the image img = diffmap_plus img2 = diffmap_minus # Perform the hough transform on the positive and negative difference maps separately transform, theta, d = hough(img) transform2, theta2, d2 = hough(img2) # Filter the hough transform results and find the best lines # in the data #indices = (transform >votethresh).nonzero() #indices = (transform == transform.max()).nonzero() #instead of getting all lines above some threshold, just get the *strongest* line only #from the positive diffmap and the negative diffmap. May get more than 2 lines due to ties #in the accumulator indices = ((transform == transform.max()) + (transform2 == transform2.max())).nonzero() distances = d[indices[0]] theta = theta[indices[1]] n = len(indices[1]) print("Found " + str(n) + " lines.")