Commit 89fa886c by Sushil BHATTACHARJEE

Merge branch 'cleanup' into 'master'

```speed-up code in some critical places

See merge request !2```
parents 25391245 de7369fd
Pipeline #10498 failed with stages
in 7 minutes and 42 seconds
 ... @@ -727,36 +727,26 @@ def edge_thinning(bx, by, thinning=1): ... @@ -727,36 +727,26 @@ def edge_thinning(bx, by, thinning=1): # np.spacing(1) is the same as eps in matlab. # np.spacing(1) is the same as eps in matlab. myEps = np.spacing(1) * 100.0 myEps = np.spacing(1) * 100.0 # compute the edge-map a la Matlab # compute the edge-map a la Matlab for r in range(m): for c in range(n): if not thinning: if thinning: e = b > cutoff if r < 0 or r > (m - 1) or (c - 1) < 0: else: b1 = True b1 = np.ones_like(b, dtype=bool) else: b2 = np.ones_like(b, dtype=bool) b1 = (b[r, c - 1] < b[r, c]) b3 = np.ones_like(b, dtype=bool) b4 = np.ones_like(b, dtype=bool) if(r < 0) or r > (m - 1) or (c + 1) > (n - 1): b2 = True c1 = b > cutoff else: b2 = (b[r, c] > b[r, c + 1]) b1[:, 1:] = (np.roll(b, 1, axis=1) < b)[:, 1:] b2[:, :-1] = (np.roll(b, -1, axis=1) < b)[:, :-1] if (c < 0) or c > (n - 1) or (r - 1) < 0: c2 = (bx >= (by - myEps)) & b1 & b2 b3 = True else: b3[1:, :] = (np.roll(b, 1, axis=0) < b)[1:, :] b3 = (b[r, c] > b[r - 1, c]) b4[:-1, 1:] = (np.roll(b, -1, axis=0) < b)[:-1, 1:] c3 = (by >= (bx - myEps)) & b3 & b4 if(c < 1) or c > (n - 1) or (r + 1) > (m - 1): b4 = True e = c1 & (c2 | c3) else: b4 = (b[r, c] > b[r + 1, c]) c1 = (b[r, c] > cutoff) c2 = ((bx[r, c] >= (by[r, c] - myEps)) & b1 & b2) c3 = ((by[r, c] >= (bx[r, c] - myEps)) & b3 & b4) e[r, c] = c1 & (c2 | c3) else: e[r, c] = (b[r, c] > cutoff) return e return e ... @@ -813,33 +803,17 @@ def angle_similarity(refImage, testImage, diffImage): ... @@ -813,33 +803,17 @@ def angle_similarity(refImage, testImage, diffImage): refNorm = np.linalg.norm(refImage, axis=1) refNorm = np.linalg.norm(refImage, axis=1) testNorm = np.linalg.norm(testImage, axis=1) testNorm = np.linalg.norm(testImage, axis=1) thetaVec = np.zeros([refImage.shape[0], 1]) diffNorm = np.linalg.norm(diffImage, axis=1) diffNorm = np.linalg.norm(diffImage, axis=1) magnitVec = diffNorm / 255.0 # Galbally divides by sqrt(255**2) magnitVec = diffNorm / 255.0 # Galbally divides by sqrt(255**2) magnitVec = np.reshape(magnitVec, (refImage.shape[0], 1)) magnitVec = np.reshape(magnitVec, (refImage.shape[0], 1)) for i in range(refImage.shape[0]): # np.einsum('ij,ij->i',a,b) is equivalent to np.diag(np.dot(a,b.T)) refR = refImage[i, :] cosTheta = np.einsum('ij,ij->i', refImage, testImage) / \ testR = testImage[i, :] (refNorm * testNorm) cosTheta = np.dot(refR, testR) / (refNorm[i] * testNorm[i]) cosTheta[cosTheta < -1.0] = -1.0 if(cosTheta < -1.0): cosTheta[cosTheta > 1.0] = 1.0 cosTheta = -1.0 cosTheta = np.nan_to_num(cosTheta) if(cosTheta > 1.0): thetaVec = np.arccos(cosTheta).reshape((refImage.shape[0], 1)) cosTheta = 1.0 theta = np.arccos(cosTheta) thetaVec[i] = theta # the following (commented out) code should be more efficient than the for- # loop above, but seems to be buggy. # rowDP= np.diag(np.dot(refImage,testImage.T)) # normRefrows= np.linalg.norm(refImage,axis=1) # normTestrows= np.linalg.norm(testImage,axis=1) # cosThetaVec = rowDP/(normRefrows * normTestrows) # cosThetaVec = np.nan_to_num(cosThetaVec) #nan occurs when one of the # # norms is 0, ie. vector is all 0s. In that case set cosTheta to 0 # thetaVec = np.arccos(cosThetaVec) tmp2 = thetaVec * 2.0 / np.pi tmp2 = thetaVec * 2.0 / np.pi ... ...
 ... @@ -399,13 +399,17 @@ def rgbhist(image, maxval, nBins, normType=0): ... @@ -399,13 +399,17 @@ def rgbhist(image, maxval, nBins, normType=0): im = image.reshape(3, numPix).copy() im = image.reshape(3, numPix).copy() im = im.T im = im.T for i in range(0, numPix): # for i=1:size(I,1)*size(I,2) p = np.floor(im.astype(float) / decimator).astype(np.uint32) p = (im[i, :]).astype(float) # p = double(im(i,:)); # in future versions of numpy (1.13 and above) you can replace this with: # p = floor(p/(maxval/nBins))+1; # unique_p, count = np.unique(p, return_counts=True, axis=0) p = (np.floor(p / decimator)).astype(np.uint32) # the following lines were taken from: https://stackoverflow.com/a/16973510 # H(p(1),p(2),p(3)) = H(p(1),p(2),p(3)) + 1; p2 = np.ascontiguousarray(p).view( H[p[0], p[1], p[2]] += 1 np.dtype((np.void, p.dtype.itemsize * p.shape[1]))) # end unique_p, count = np.unique(p2, return_counts=True) unique_p = unique_p.view(p.dtype).reshape(-1, p.shape[1]) # till here H[unique_p[:, 0], unique_p[:, 1], unique_p[:, 2]] = count H = H.ravel() # H = H(:); H = H.ravel() # H = H(:); # Un-Normalized histogram # Un-Normalized histogram ... ...
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!