(digits)
| 126 | return np.float32(digits).reshape(-1, SZ*SZ) / 255.0 |
| 127 | |
| 128 | def preprocess_hog(digits): |
| 129 | samples = [] |
| 130 | for img in digits: |
| 131 | gx = cv2.Sobel(img, cv2.CV_32F, 1, 0) |
| 132 | gy = cv2.Sobel(img, cv2.CV_32F, 0, 1) |
| 133 | mag, ang = cv2.cartToPolar(gx, gy) |
| 134 | bin_n = 16 |
| 135 | bin = np.int32(bin_n*ang/(2*np.pi)) |
| 136 | bin_cells = bin[:10,:10], bin[10:,:10], bin[:10,10:], bin[10:,10:] |
| 137 | mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:] |
| 138 | hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)] |
| 139 | hist = np.hstack(hists) |
| 140 | |
| 141 | # transform to Hellinger kernel |
| 142 | eps = 1e-7 |
| 143 | hist /= hist.sum() + eps |
| 144 | hist = np.sqrt(hist) |
| 145 | hist /= norm(hist) + eps |
| 146 | |
| 147 | samples.append(hist) |
| 148 | return np.float32(samples) |
| 149 | |
| 150 | |
| 151 | if __name__ == '__main__': |
no outgoing calls
no test coverage detected