def phog(image, bin, angle, L, roi): """ phog -- Function that prepares data structures and histogram indexing and then calls other functions to execute the pryamidal histogram of gradient code. inputs: image -- String that names the file path to the image to compute on. bin -- Number of components to use for each subwindow histogram angle -- 180 or 360 if you want half-circle or whole-circle angles, respectively. L -- Number of pyramid levels to use. roi -- List with 4 entries, [top_y, bottom_y, left_x, right_x], that bounds the image region for which you want to compute the HoG feature. outputs: p -- A long vector that contains the pHoG descriptor. """ Img = imread(image) if len(Img.shape) == 3: G = clr.rgb2gray(Img) else: G = Img if np.sum(G) > 100: E = canny(G) GradientX, GradientY = np.gradient(np.double(G)) Gr = np.sqrt(GradientX**2 + GradientY**2) GradientX[GradientX == 0] = 1 * 10**(-5) YX = np.divide(GradientY, GradientX) if angle == 180: A = (np.arctan(YX + np.pi / 2.0) * 180) / np.pi if angle == 360: A = (np.arctan2(GradientY, GradientX) + np.pi) * 180 / np.pi # Function call bh, bv = binMatrix(A, E, Gr, angle, bin) else: bh = np.zeros((G.shape[0], G.shape[1])) bv = np.zeros((G.shape[0], G.shape[1])) bh_roi = bh[roi[0]:roi[1], roi[2]:roi[3]] bv_roi = bv[roi[0]:roi[1], roi[2]:roi[3]] # Function call to actually extract the HoG vector. p = phogDescriptor(bh_roi, bv_roi, L, bin) return p
def phog(image, bin, angle, L, roi): """ phog -- Function that prepares data structures and histogram indexing and then calls other functions to execute the pryamidal histogram of gradient code. inputs: image -- String that names the file path to the image to compute on. bin -- Number of components to use for each subwindow histogram angle -- 180 or 360 if you want half-circle or whole-circle angles, respectively. L -- Number of pyramid levels to use. roi -- List with 4 entries, [top_y, bottom_y, left_x, right_x], that bounds the image region for which you want to compute the HoG feature. outputs: p -- A long vector that contains the pHoG descriptor. """ Img = imread(image) if len(Img.shape) == 3: G = clr.rgb2gray(Img) else: G = Img if np.sum(G) > 100: E = canny(G) GradientX, GradientY = np.gradient(np.double(G)) Gr = np.sqrt(GradientX**2 + GradientY**2) GradientX[GradientX==0] = 1*10**(-5) YX = np.divide(GradientY,GradientX) if angle == 180: A = (np.arctan(YX + np.pi/2.0)*180)/np.pi if angle == 360: A = (np.arctan2(GradientY,GradientX)+np.pi)*180/np.pi # Function call bh, bv = binMatrix(A,E,Gr,angle,bin) else: bh = np.zeros((G.shape[0],G.shape[1])) bv = np.zeros((G.shape[0],G.shape[1])) bh_roi = bh[roi[0]:roi[1], roi[2]:roi[3]] bv_roi = bv[roi[0]:roi[1], roi[2]:roi[3]] # Function call to actually extract the HoG vector. p = phogDescriptor(bh_roi,bv_roi,L,bin) return p
def test_01_02_circle_with_noise(self): '''Test that the Canny filter finds the circle outlines in a noisy image''' np.random.seed(0) i, j = np.mgrid[-200:200, -200:200].astype(float) / 200 c = np.abs(np.sqrt(i * i + j * j) - .5) < .02 cf = c.astype(float) * .5 + np.random.uniform(size=c.shape) * .5 result = F.canny(cf, 4, .1, .2, np.ones(c.shape, bool)) # # erode and dilate the circle to get rings that should contain the # outlines # cd = binary_dilation(c, iterations=4) ce = binary_erosion(c, iterations=4) cde = np.logical_and(cd, np.logical_not(ce)) self.assertTrue(np.all(cde[result])) point_count = np.sum(result) self.assertTrue(point_count > 1200) self.assertTrue(point_count < 1600)
def test_01_02_circle_with_noise(self): '''Test that the Canny filter finds the circle outlines in a noisy image''' np.random.seed(0) i,j = np.mgrid[-200:200,-200:200].astype(float) / 200 c = np.abs(np.sqrt(i*i+j*j) - .5) < .02 cf = c.astype(float) * .5 + np.random.uniform(size=c.shape)*.5 result = F.canny(cf, 4, .1, .2,np.ones(c.shape,bool)) # # erode and dilate the circle to get rings that should contain the # outlines # cd = binary_dilation(c, iterations=4) ce = binary_erosion(c,iterations=4) cde = np.logical_and(cd, np.logical_not(ce)) self.assertTrue(np.all(cde[result])) point_count = np.sum(result) self.assertTrue(point_count > 1200) self.assertTrue(point_count < 1600)
def test_01_01_circle(self): '''Test that the Canny filter finds the outlines of a circle''' i, j = np.mgrid[-200:200, -200:200].astype(float) / 200 c = np.abs(np.sqrt(i * i + j * j) - .5) < .02 result = F.canny(c.astype(float), 4, 0, 0, np.ones(c.shape, bool)) # # erode and dilate the circle to get rings that should contain the # outlines # cd = binary_dilation(c, iterations=3) ce = binary_erosion(c, iterations=3) cde = np.logical_and(cd, np.logical_not(ce)) self.assertTrue(np.all(cde[result])) # # The circle has a radius of 100. There are two rings here, one # for the inside edge and one for the outside. So that's 100 * 2 * 2 * 3 # for those places where pi is still 3. The edge contains both pixels # if there's a tie, so we bump the count a little. # point_count = np.sum(result) self.assertTrue(point_count > 1200) self.assertTrue(point_count < 1600)
def test_01_01_circle(self): '''Test that the Canny filter finds the outlines of a circle''' i,j = np.mgrid[-200:200,-200:200].astype(float) / 200 c = np.abs(np.sqrt(i*i+j*j) - .5) < .02 result = F.canny(c.astype(float), 4, 0, 0,np.ones(c.shape,bool)) # # erode and dilate the circle to get rings that should contain the # outlines # cd = binary_dilation(c, iterations=3) ce = binary_erosion(c,iterations=3) cde = np.logical_and(cd, np.logical_not(ce)) self.assertTrue(np.all(cde[result])) # # The circle has a radius of 100. There are two rings here, one # for the inside edge and one for the outside. So that's 100 * 2 * 2 * 3 # for those places where pi is still 3. The edge contains both pixels # if there's a tie, so we bump the count a little. # point_count = np.sum(result) self.assertTrue(point_count > 1200) self.assertTrue(point_count < 1600)
def test_00_00_zeros(self): '''Test that the Canny filter finds no points for a blank field''' result = F.canny(np.zeros((20, 20)), 4, 0, 0, np.ones((20, 20), bool)) self.assertFalse(np.any(result))
def test_00_01_zeros_mask(self): '''Test that the Canny filter finds no points in a masked image''' result = (F.canny(np.random.uniform(size=(20, 20)), 4, 0, 0, np.zeros((20, 20), bool))) self.assertFalse(np.any(result))
def test_00_00_zeros(self): '''Test that the Canny filter finds no points for a blank field''' result = F.canny(np.zeros((20,20)), 4, 0, 0, np.ones((20,20),bool)) self.assertFalse(np.any(result))
def test_00_01_zeros_mask(self): '''Test that the Canny filter finds no points in a masked image''' result = (F.canny(np.random.uniform(size=(20,20)), 4,0,0, np.zeros((20,20),bool))) self.assertFalse(np.any(result))
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)') # Line finding, using the Probabilistic Hough Transform image = data.camera() edges = canny(image, 2, 1, 25) lines = probabilistic_hough(edges, threshold=10, line_length=5, line_gap=3) plt.figure(figsize=(12, 4)) plt.subplot(131) plt.imshow(image, cmap=plt.cm.gray) plt.title('Input image') plt.subplot(132) plt.imshow(edges, cmap=plt.cm.gray) plt.title('Sobel edges') plt.subplot(133) plt.imshow(edges * 0)