def dxdy_hist(img_gray, num_bins): assert len(img_gray.shape) == 2, 'image dimension mismatch' assert img_gray.dtype == 'float', 'incorrect image type' sigma, caps = 3, (-6, 6) kernel = gauss_module.gaussdx(sigma)[0] kernel = kernel[int((len(kernel) - 1) / 2) + caps[0]: int((len(kernel) + 1) / 2) + caps[1]] kernel = (kernel / kernel.sum()).reshape(1, kernel.shape[0]) imgDx = conv2(img_gray, kernel, 'same') imgDy = conv2(img_gray, kernel.transpose(), 'same') # rescaling to [0, 255] def scale(img): img_flat = img.flatten() img_scaled = ((img - min(img_flat)) / (max(img_flat) - min(img_flat))) * 255 return img_scaled imgDx, imgDy, imgDz = scale(imgDx), scale(imgDy), np.zeros_like(imgDx) img_concat = np.zeros(shape=(imgDx.shape[0], imgDx.shape[1], 3)) img_concat[:, :, 0], img_concat[:, :, 1], img_concat[:, :, 2] = imgDx, imgDy, imgDz # Define a 2D histogram with "num_bins^2" number of entries hists = rg_hist(img_concat.astype('double'), num_bins) return hists
smooth_img = gauss_module.gaussianfilter(img, sigma) plt.figure(2) ax1 = plt.subplot(1, 2, 1) ax2 = plt.subplot(1, 2, 2) plt.sca(ax1) plt.imshow(img, cmap='gray', vmin=0, vmax=255) plt.sca(ax2) plt.imshow(smooth_img, cmap='gray', vmin=0, vmax=255) plt.show() ## function gaussdx (Question 1.c) sigma = 4.0 [Gx, x] = gauss_module.gauss(sigma) [Dx, x] = gauss_module.gaussdx(sigma) plt.figure(5) plt.plot(x, Gx, 'b.-') plt.plot(x, Dx, 'r-') plt.legend(('gauss', 'gaussdx')) plt.show() ## function gaussdx (Question 1.d) img_imp = np.zeros([27, 27]) img_imp[13, 13] = 1.0 plt.figure(6), plt.imshow(img_imp, cmap='gray') sigma = 7.0 [Gx, x] = gauss_module.gauss(sigma)
ax2 = plt.subplot(1, 2, 2) plt.sca(ax1) plt.imshow(img, cmap='gray', vmin=0, vmax=255) plt.sca(ax2) plt.imshow(smooth_img, cmap='gray', vmin=0, vmax=255) plt.show() ## function gaussdx (Question 1.c) img = np.zeros([27, 27]) img[13, 13] = 1.0 plt.figure(), plt.imshow(img, cmap='gray') sigma = 7.0 [G, x] = gauss_module.gauss(sigma) [D, x] = gauss_module.gaussdx(sigma) plt.figure() plt.plot(x, G, 'b.-') plt.plot(x, D, 'r-') plt.legend(('gauss', 'gaussdx')) plt.show() G = G.reshape(1, G.size) D = D.reshape(1, D.size) plt.figure() plt.subplot(2, 3, 1) plt.imshow(conv2(img, G, 'same'), cmap='gray') plt.subplot(2, 3, 2) plt.imshow(conv2(img, G.T, 'same'), cmap='gray')