def test_rectangles(self): from numpy import fliplr, flipud from numpy.random import random_sample, seed from integral_image import * seed(42) I = random_sample((11, 11)) # symmetrize by copying the first quadrant I[:, 6:] = fliplr(I[:, 0:5]) I[6:, :] = flipud(I[0:5, :]) intimg = integral_image(I) symmimg = integral_image(I[:6, :6]) for (x0, x1, y0, y1) in [ (-1, 0, -1, 0), # first element (1, 4, 1, 4), # first quadrant (simple) (-1, 5, -1, 5), # first quadrant (edges) (6, 9, 1, 4), # quadrant 2 (simple) (5, 10, 1, 4), # quadrant 2 (edges) (4, 5, -1, 5), # quadrant 1/2 boundary (4, 6, -1, 5), # quadrant 1/2 boundary + 1 over (-1, 10, -1, 5), # quadrants 1,2 (up to edges) (7, 9, 7, 9), # quadrant 3 (1, 4, 7, 9), # quadrant 4 (3, 7, 4, 8), # (-1, 10, -1, 10), # (5, 6, -1, 0) ]: R0 = intimg_rect(intimg, x0, x1, y0, y1) R1 = symm_intimg_rect(symmimg, x0, x1, y0, y1, 5) print(round(R0, 5), round(R1, 5), round(R0, 5) == round(R1, 5)) self.assertAlmostEqual(R1, R0, 8)
def test_rectangles(self): from numpy import fliplr, flipud from numpy.random import random_sample, seed from integral_image import * seed(42) I = random_sample((11,11)) # symmetrize by copying the first quadrant I[:,6:] = fliplr(I[:,0:5]) I[6:,:] = flipud(I[0:5,:]) intimg = integral_image(I) symmimg = integral_image(I[:6,:6]) for (x0,x1,y0,y1) in [(-1,0,-1,0), # first element (1,4,1,4), # first quadrant (simple) (-1,5,-1,5), # first quadrant (edges) (6,9,1,4), # quadrant 2 (simple) (5,10,1,4), # quadrant 2 (edges) (4,5,-1,5), # quadrant 1/2 boundary (4,6,-1,5), # quadrant 1/2 boundary + 1 over (-1,10,-1,5), # quadrants 1,2 (up to edges) (7,9,7,9), # quadrant 3 (1,4,7,9), # quadrant 4 (3,7,4,8), # (-1,10,-1,10), # (5,6,-1,0)]: R0 = intimg_rect(intimg, x0, x1, y0, y1) R1 = symm_intimg_rect(symmimg, x0, x1, y0, y1, 5) print round(R0,5), round(R1,5), round(R0,5) == round(R1,5) self.assertAlmostEqual(R1, R0, 8)
def _create(self, profile_func=None, modelname=None, re=None, nrad=None, ab=1.0, subnpix=10, nsub=101): assert(ab == 1) self.modelname = modelname self.cre = float(re) self.cnrad = nrad self.cab = float(ab) self.csize = int(ceil(self.cnrad * self.cre)) self.cn = 2*self.csize + 1 # Sample the whole thing... we will overwrite the subsampled region. (gridi,gridj) = meshgrid(arange(self.csize+1)-self.csize, arange(self.csize+1)-self.csize) R = sqrt((gridi/self.cab)**2 + gridj**2) profile = profile_func(R/self.cre) # Subsample the bottom-right corner. g = (arange(nsub)-(nsub-1)/2) / float(nsub) (subgridi,subgridj) = meshgrid(g, g) #print 'g: min %g, max %g, len %i' % (g.min(), g.max(), len(g)) for subi in range(subnpix): for subj in range(subi+1): #subnpix: R = sqrt(((subi + subgridi) / self.cab)**2 + (subj + subgridj)**2) p = mean(profile_func(R/self.cre)) #print 'subsampling pixel', subi,subj, ('(%i,%i)' % (self.csize-subi, self.csize-subj)), #print ' : %g -> %g' % (profile[self.csize-subi, self.csize-subj], p) profile[self.csize-subi, self.csize-subj] = p profile[self.csize-subj, self.csize-subi] = p # Normalize by the sum of the whole (mirrored) profile. profile /= (sum(profile) + 3 * sum(profile[:-1,:-1])) self.intimg = integral_image(profile)