def shape_index_hist(img, scale, n_bins, tonal_scale): si, si_c = ipcv.shape_index(img, scale, orientations=False) # sp.misc.imsave('woop%.2f.png' % scale, si) limits = (-np.pi / 2, np.pi / 2) si_iso = ipcv.misc.isophotes(si, n_bins, limits, tonal_scale) hist = np.sum(si_iso * si_c, axis=(1, 2)) hist /= np.sum(hist) return hist
def shape_index_hist(img, scale, n_bins, tonal_scale): si, si_c = ipcv.shape_index(img, scale, orientations=False) # sp.misc.imsave('woop%.2f.png' % scale, si) limits = (-np.pi/2, np.pi/2) si_iso = ipcv.misc.isophotes(si, n_bins, limits, tonal_scale) hist = np.sum(si_iso * si_c, axis=(1,2)) hist /= np.sum(hist) return hist
def transform(self, img): hists_shape = (self.n_bins, self.ori_n_bins, len(self.scales)) hists = np.empty(hists_shape) si, si_c, si_o, si_om = ipcv.shape_index(img, self.ori_detect_scale, orientations=True) # Detect dominant orientation ori_detect_n_bins = self.ori_n_bins * 2 iso_si_o = ipcv.misc.isophotes(si_o, ori_detect_n_bins, (-np.pi / 2, np.pi / 2), self.ori_tonal_scale, 'von_mises') ori_hist = np.sum(iso_si_o, axis=(1, 2)) ori_hist /= np.sum(ori_hist) # print(ori_hist) max_idx = np.argmax(ori_hist) ori_offset = max_idx / float(ori_detect_n_bins) * np.pi - np.pi / 2 # print(max_idx, max_idx/float(ori_detect_n_bins), ori_offset) for s_idx, s in enumerate(self.scales): si, si_c, si_o, si_om = ipcv.shape_index(img, s, orientations=True) # Shift according to dominant orientation si_o = np.mod(si_o - ori_offset + np.pi / 2, np.pi) - np.pi / 2 # Smooth bin contributions (= soft isophote images) iso_si = ipcv.misc.isophotes(si, self.n_bins, (-np.pi / 2, np.pi / 2), self.tonal_scale) iso_si_o = ipcv.misc.isophotes(si_o, self.ori_n_bins, (-np.pi / 2, np.pi / 2), self.ori_tonal_scale, 'von_mises') # Bin contributions for the joint histogram iso_j = (iso_si[:, np.newaxis, ...] * si_c * iso_si_o[np.newaxis, ...] * si_om) # Summarize bin contributions in the joint histograms hists[:, :, s_idx] = np.sum(iso_j, axis=(2, 3)) if not self.joint_hist: hists_si = np.sum(hists, axis=1) hists_ori = np.sum(hists, axis=0) hists_si = ipcv.misc.normalize(hists_si, self.norm) hists_ori = ipcv.misc.normalize(hists_ori, self.norm) hists = np.hstack([np.ravel(hists_si), np.ravel(hists_ori)]) else: hists = ipcv.misc.normalize(hists, self.norm) if self.flat: hists = np.ravel(hists) return hists
def visualize_si(): ''' Visualize the shape index responses. ''' img = sp.misc.imread('data/patterns.png', flatten=True) si, si_c, si_o, si_om = shape_index(img, 2.5, orientations=True, fft=True) imsave('si/patterns-si.png', si) imsave('si/patterns-si_c.png', si_c) imsave('si/patterns-si_weighted.png', si * si_c) imsave('si/patterns-si_o.png', si_o) imsave('si/patterns-si_om.png', si_om) imsave('si/patterns-si_o_weighted.png', si_o * si_om)
def visualize_si(): ''' Visualize the shape index responses. ''' img = sp.misc.imread('data/patterns.png', flatten=True) si, si_c, si_o, si_om = shape_index(img, 2.5, orientations=True, fft=True) imsave('si/patterns-si.png', si) imsave('si/patterns-si_c.png', si_c) imsave('si/patterns-si_weighted.png', si*si_c) imsave('si/patterns-si_o.png', si_o) imsave('si/patterns-si_om.png', si_om) imsave('si/patterns-si_o_weighted.png', si_o*si_om)
def transform(self, img): hists_shape = (self.n_bins, self.ori_n_bins, len(self.scales)) hists = np.empty(hists_shape) si, si_c, si_o, si_om = ipcv.shape_index(img, self.ori_detect_scale, orientations=True) # Detect dominant orientation ori_detect_n_bins = self.ori_n_bins*2 iso_si_o = ipcv.misc.isophotes(si_o, ori_detect_n_bins, (-np.pi/2, np.pi/2), self.ori_tonal_scale, 'von_mises') ori_hist = np.sum(iso_si_o, axis=(1,2)) ori_hist /= np.sum(ori_hist) # print(ori_hist) max_idx = np.argmax(ori_hist) ori_offset = max_idx/float(ori_detect_n_bins)*np.pi-np.pi/2 # print(max_idx, max_idx/float(ori_detect_n_bins), ori_offset) for s_idx, s in enumerate(self.scales): si, si_c, si_o, si_om = ipcv.shape_index(img, s, orientations=True) # Shift according to dominant orientation si_o = np.mod(si_o-ori_offset+np.pi/2, np.pi)-np.pi/2 # Smooth bin contributions (= soft isophote images) iso_si = ipcv.misc.isophotes(si, self.n_bins, (-np.pi/2, np.pi/2), self.tonal_scale) iso_si_o = ipcv.misc.isophotes(si_o, self.ori_n_bins, (-np.pi/2, np.pi/2), self.ori_tonal_scale, 'von_mises') # Bin contributions for the joint histogram iso_j = (iso_si[:, np.newaxis, ...] * si_c * iso_si_o[np.newaxis, ...] * si_om) # Summarize bin contributions in the joint histograms hists[:, :, s_idx] = np.sum(iso_j, axis=(2, 3)) if not self.joint_hist: hists_si = np.sum(hists, axis=1) hists_ori = np.sum(hists, axis=0) hists_si = ipcv.misc.normalize(hists_si, self.norm) hists_ori = ipcv.misc.normalize(hists_ori, self.norm) hists = np.hstack([np.ravel(hists_si), np.ravel(hists_ori)]) else: hists = ipcv.misc.normalize(hists, self.norm) if self.flat: hists = np.ravel(hists) return hists
def multiscale_shape_index(): dataset = data.UIUCTex() imgs = memory.cache(dataset.imgs)() img_idx = 401 img = imgs[img_idx] dirpath = os.path.join('multiscale_shape_index', dataset.name, 'img%.3d'%img_idx) img_save(img, os.path.join(dirpath, 'img.png')) scales = [1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38] for scale in scales: si, si_c, si_o, si_om = ipcv.shape_index(img, scale, orientations=True) img_save(si, os.path.join(dirpath, 'si_scale%.2d.png' % scale)) img_save(si_c, os.path.join(dirpath, 'si-c_scale%.2d.png' % scale)) img_save(si_o, os.path.join(dirpath, 'si-o_scale%.2d.png' % scale)) img_save(si_om, os.path.join(dirpath, 'si-om_scale%.2d.png' % scale))
def multiscale_shape_index(): dataset = data.UIUCTex() imgs = memory.cache(dataset.imgs)() img_idx = 401 img = imgs[img_idx] dirpath = os.path.join('multiscale_shape_index', dataset.name, 'img%.3d' % img_idx) img_save(img, os.path.join(dirpath, 'img.png')) scales = [ 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38 ] for scale in scales: si, si_c, si_o, si_om = ipcv.shape_index(img, scale, orientations=True) img_save(si, os.path.join(dirpath, 'si_scale%.2d.png' % scale)) img_save(si_c, os.path.join(dirpath, 'si-c_scale%.2d.png' % scale)) img_save(si_o, os.path.join(dirpath, 'si-o_scale%.2d.png' % scale)) img_save(si_om, os.path.join(dirpath, 'si-om_scale%.2d.png' % scale))
def visualize_si_orientation(): ''' Visualize the shape index orientation. ''' img = sp.misc.imread('data/rings.png', flatten=True) si, si_c, si_o, si_om = shape_index(img, 2.5, orientations=True, fft=True) import matplotlib.pyplot as plt plt.figure() plt.imshow(img, cmap=plt.gray()) X, Y = np.mgrid[0:si_o.shape[0], 0:si_o.shape[1]] u = np.cos(si_o) * si_c / np.max(si_c) v = np.sin(si_o) * si_c / np.max(si_c) quiver_mask = si_c > 10 step = 3 quiver_mask = quiver_mask[::step, ::step] Y = (Y[::step, ::step])[quiver_mask] X = (X[::step, ::step])[quiver_mask] u = (u[::step, ::step])[quiver_mask] v = (v[::step, ::step])[quiver_mask] plt.quiver(Y, X, u, v, pivot='mid', scale=30, color='r') plt.savefig('si/rings-si_o.pdf')
def visualize_si_fiducial_orientation(): ''' Visualize the shape index orientation with a fiducial coordinate system.''' img = sp.misc.imread('data/rings.png', flatten=True) si, si_c, si_o, si_om = shape_index(img, 2.5, orientations=True, fft=True) # No fiducial coordinate system. imsave('si/rings-si_o.png', si_o) imsave('si/rings-si_o_weighted.png', si_o * si_om) # Let each pixel have its own coordinate system where the origin is # the image center and the first axis is the vector from the origin to # the pixel. h, w = img.shape[:2] y = np.linspace(-h / 2., h / 2., h) x = np.linspace(-w / 2., w / 2., w) xv, yv = np.meshgrid(x, y) offsets = np.arctan(yv / (xv + 1e-10)) si_o = np.mod(si_o + offsets, np.pi) imsave('si/rings-si_o_fiducial.png', si_o) imsave('si/rings-si_o_fiducial_weighted.png', si_o * si_om)
def visualize_si_fiducial_orientation(): ''' Visualize the shape index orientation with a fiducial coordinate system.''' img = sp.misc.imread('data/rings.png', flatten=True) si, si_c, si_o, si_om = shape_index(img, 2.5, orientations=True, fft=True) # No fiducial coordinate system. imsave('si/rings-si_o.png', si_o) imsave('si/rings-si_o_weighted.png', si_o*si_om) # Let each pixel have its own coordinate system where the origin is # the image center and the first axis is the vector from the origin to # the pixel. h, w = img.shape[:2] y = np.linspace(-h/2., h/2., h) x = np.linspace(-w/2., w/2., w) xv, yv = np.meshgrid(x, y) offsets = np.arctan(yv/(xv+1e-10)) si_o = np.mod(si_o+offsets, np.pi) imsave('si/rings-si_o_fiducial.png', si_o) imsave('si/rings-si_o_fiducial_weighted.png', si_o*si_om)
def visualize_si_orientation(): ''' Visualize the shape index orientation. ''' img = sp.misc.imread('data/rings.png', flatten=True) si, si_c, si_o, si_om = shape_index(img, 2.5, orientations=True, fft=True) import matplotlib.pyplot as plt plt.figure() plt.imshow(img, cmap=plt.gray()) X, Y = np.mgrid[0:si_o.shape[0], 0:si_o.shape[1]] u = np.cos(si_o)*si_c/np.max(si_c) v = np.sin(si_o)*si_c/np.max(si_c) quiver_mask = si_c > 10 step = 3 quiver_mask = quiver_mask[::step, ::step] Y = (Y[::step, ::step])[quiver_mask] X = (X[::step, ::step])[quiver_mask] u = (u[::step, ::step])[quiver_mask] v = (v[::step, ::step])[quiver_mask] plt.quiver(Y, X, u, v, pivot='mid', scale=30, color='r') plt.savefig('si/rings-si_o.pdf')