Commit 89fa886c authored by Sushil BHATTACHARJEE's avatar 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):
# np.spacing(1) is the same as eps in matlab.
myEps = np.spacing(1) * 100.0
# compute the edge-map a la Matlab
for r in range(m):
for c in range(n):
if thinning:
if r < 0 or r > (m - 1) or (c - 1) < 0:
b1 = True
else:
b1 = (b[r, c - 1] < b[r, c])
if(r < 0) or r > (m - 1) or (c + 1) > (n - 1):
b2 = True
else:
b2 = (b[r, c] > b[r, c + 1])
if (c < 0) or c > (n - 1) or (r - 1) < 0:
b3 = True
else:
b3 = (b[r, c] > b[r - 1, c])
if(c < 1) or c > (n - 1) or (r + 1) > (m - 1):
b4 = True
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)
if not thinning:
e = b > cutoff
else:
b1 = np.ones_like(b, dtype=bool)
b2 = np.ones_like(b, dtype=bool)
b3 = np.ones_like(b, dtype=bool)
b4 = np.ones_like(b, dtype=bool)
c1 = b > cutoff
b1[:, 1:] = (np.roll(b, 1, axis=1) < b)[:, 1:]
b2[:, :-1] = (np.roll(b, -1, axis=1) < b)[:, :-1]
c2 = (bx >= (by - myEps)) & b1 & b2
b3[1:, :] = (np.roll(b, 1, axis=0) < b)[1:, :]
b4[:-1, 1:] = (np.roll(b, -1, axis=0) < b)[:-1, 1:]
c3 = (by >= (bx - myEps)) & b3 & b4
e = c1 & (c2 | c3)
return e
......@@ -813,33 +803,17 @@ def angle_similarity(refImage, testImage, diffImage):
refNorm = np.linalg.norm(refImage, axis=1)
testNorm = np.linalg.norm(testImage, axis=1)
thetaVec = np.zeros([refImage.shape[0], 1])
diffNorm = np.linalg.norm(diffImage, axis=1)
magnitVec = diffNorm / 255.0 # Galbally divides by sqrt(255**2)
magnitVec = np.reshape(magnitVec, (refImage.shape[0], 1))
for i in range(refImage.shape[0]):
refR = refImage[i, :]
testR = testImage[i, :]
cosTheta = np.dot(refR, testR) / (refNorm[i] * testNorm[i])
if(cosTheta < -1.0):
cosTheta = -1.0
if(cosTheta > 1.0):
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)
# np.einsum('ij,ij->i',a,b) is equivalent to np.diag(np.dot(a,b.T))
cosTheta = np.einsum('ij,ij->i', refImage, testImage) / \
(refNorm * testNorm)
cosTheta[cosTheta < -1.0] = -1.0
cosTheta[cosTheta > 1.0] = 1.0
cosTheta = np.nan_to_num(cosTheta)
thetaVec = np.arccos(cosTheta).reshape((refImage.shape[0], 1))
tmp2 = thetaVec * 2.0 / np.pi
......
......@@ -399,13 +399,17 @@ def rgbhist(image, maxval, nBins, normType=0):
im = image.reshape(3, numPix).copy()
im = im.T
for i in range(0, numPix): # for i=1:size(I,1)*size(I,2)
p = (im[i, :]).astype(float) # p = double(im(i,:));
# p = floor(p/(maxval/nBins))+1;
p = (np.floor(p / decimator)).astype(np.uint32)
# H(p(1),p(2),p(3)) = H(p(1),p(2),p(3)) + 1;
H[p[0], p[1], p[2]] += 1
# end
p = np.floor(im.astype(float) / decimator).astype(np.uint32)
# in future versions of numpy (1.13 and above) you can replace this with:
# unique_p, count = np.unique(p, return_counts=True, axis=0)
# the following lines were taken from: https://stackoverflow.com/a/16973510
p2 = np.ascontiguousarray(p).view(
np.dtype((np.void, p.dtype.itemsize * p.shape[1])))
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(:);
# Un-Normalized histogram
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment