def compare_transforms(letter): img = img_utils.generate_letter(letter) img_translated = img_utils.translate(img, axis=0, pixels=20) img_scaled = img_utils.zoom(img, times=1.2) img_rotated = img_utils.rotate(img, degrees=45) img_noise = img_utils.noise(img, low=-30, high=30) img_flipped = img_utils.flip(img, axis=0) Hu_moments = OrderedDict() Hu_moments["original"] = cv2.HuMoments(cv2.moments(img)).flatten() Hu_moments["translated"] = cv2.HuMoments( cv2.moments(img_translated)).flatten() Hu_moments["scaled"] = cv2.HuMoments(cv2.moments(img_scaled)).flatten() Hu_moments["rotated"] = cv2.HuMoments(cv2.moments(img_rotated)).flatten() Hu_moments["noise"] = cv2.HuMoments(cv2.moments(img_noise)).flatten() Hu_moments["flipped"] = cv2.HuMoments(cv2.moments(img_flipped)).flatten() x_labels = [ "Original", "Translated", "Scaled", "Rotated", "Noise", "Flipped" ] xs = list(range(6)) for i in range(7): plt.title(f"Hu moment {i + 1} for {letter}") plt.xticks(xs, x_labels) ys = [moments[i] for moments in Hu_moments.values()] plt.scatter(xs, ys) plt.savefig( os.path.join("plots", "moment_" + str(i + 1) + "_" + letter)) plt.clf()
def make_thumb(fid, filedata): key = 'z' + fid key = str(key) if not CACHE.get(key): filedata = zoom(filedata, 200, 200) CACHE.set(key, filedata) return True return False
def check_invariance(letter): img = img_utils.generate_letter(letter) img_translated_1 = img_utils.translate(img, axis=0, pixels=20) img_translated_2 = img_utils.translate(img, axis=1, pixels=-20) img_scaled_1 = img_utils.zoom(img, times=1.2) img_scaled_2 = img_utils.zoom(img, times=1.5) img_rotated_1 = img_utils.rotate(img, degrees=45) img_rotated_2 = img_utils.rotate(img, degrees=-90) hu_moments = OrderedDict() hu_moments["original"] = cv2.HuMoments(cv2.moments(img)).flatten() hu_moments["translated_1"] = cv2.HuMoments( cv2.moments(img_translated_1)).flatten() hu_moments["translated_2"] = cv2.HuMoments( cv2.moments(img_translated_2)).flatten() hu_moments["scaled_1"] = cv2.HuMoments(cv2.moments(img_scaled_1)).flatten() hu_moments["scaled_2"] = cv2.HuMoments(cv2.moments(img_scaled_2)).flatten() hu_moments["rotated_1"] = cv2.HuMoments( cv2.moments(img_rotated_1)).flatten() hu_moments["rotated_2"] = cv2.HuMoments( cv2.moments(img_rotated_2)).flatten() for i in range(7): ith_moment_vals = np.array( [moments[i] for moments in hu_moments.values()]) # get only moments for transforms ith_moment_vals = ith_moment_vals[1:] stdev = ith_moment_vals.std() original_val = hu_moments["original"][i] print(original_val, stdev) moment_1_vals = [] for _ in range(1000): degrees = np.random.randint(low=-90, high=90) new_img = img_utils.rotate(img, degrees) moment_1_vals.append(cv2.HuMoments(cv2.moments(new_img))[0]) moment_1_vals = np.array(moment_1_vals) print(letter, moment_1_vals.std())
def thumbnail(fid): key = 'z' + fid key = str(key) filedata = CACHE.get(key) if not filedata: filedata = get_file_data(fid) if not filedata: abort(404, 'File not found') filedata = zoom(filedata, 200, 200) CACHE.set(key, filedata) response = app.make_response(filedata) response.headers['Content-Type'] = mimetype(filedata[:1024]) response.headers['Content-Length'] = len(filedata) return response