Skip to content

Instantly share code, notes, and snippets.

@Qblack
Created October 24, 2022 03:38
Show Gist options
  • Save Qblack/e756d19f6c77ba8f01e6498fffad81db to your computer and use it in GitHub Desktop.
Save Qblack/e756d19f6c77ba8f01e6498fffad81db to your computer and use it in GitHub Desktop.
from IPython.display import clear_output
from pathlib import Path
from datetime import datetime
import PIL
from PIL import Image, ImageDraw, ImageChops
import pandas as pd
import numpy as np
from numpy import linalg
from copy import deepcopy
from IPython.display import display # to display images
"""
We illustrate the procedures in Figure 2.
- An RGB image is first converted to grey scale.
For each pixel column, the algorithm determines its optimal shift by comparing the current column
with its previous shifted neighbouring columns by taking the sum of the vector norms that are weighted inversely by the distances between columns.
That is, the weights are proportional to the inverse of the distance raised to the power p.
Denote the pixel values in the ith column by ci
. Let the number of neighbours be n and the norm
order be k. The optimal shift sb for column i, i > 1, is obtained by searching among all possible shifts s ∈ represents pixels of the ith column shifted by s.
The values of n, p, and k are fine-tuned to achieve optimal results. Taking the extent of misalignment into account,
we set n = 100, k = 2, and p = 1 to search for the optimal shifts for each pixel column. This method proves to be
effective in resolving the misalignment in the lumber images. A simplified example is depicted in Figure 2. It can
be seen that our proposed greedy method is robust to the
nonuniform dark background and edge-lying knots
"""
def get_norms(column, window):
# print(column.shape)
# print(window.shape)
# print((window.shape))
# Create window size column matrix copy of coluimn
x = np.full((window.shape[0], window.shape[1]), column)
# Subtract each column from the window
sub = x - window
# Apply to get norm
distances = np.array(list(map(linalg.norm, sub)))
# weighted sum
# wweights = np.arange(1, distances.shape[])
# print(distances.shape)
weights = np.array([1 / i for i in range(distances.shape[0], 0, -1)])
weighted_sum = np.dot(distances, weights)
return weighted_sum
def shift_func(arr, num, fill_value=0):
# print(arr)
# print(num)
arr = np.roll(arr, num)
if num < 0:
arr[num:] = fill_value
elif num > 0:
arr[:num] = fill_value
return arr
def get_shift(column, window):
# for each shift we shift the column
shifts = np.arange(-15, 16, 1)
# df = pd.Series(shifts)
# df = pd.Series(column)
# columns = map(shift_func, column, shifts)
# v = np.vectorize(shift_func)
# print(column)
# columns = np.array(np.full((shifts.shape[0], window.shape[1]), column))
shifted = np.array(list(map(lambda s: shift_func(column, s), shifts)))
# columns = np.apply_along_axis(shift_func, 0 columns, [shifts])
# result = np.array(list(map( get_norms, columns, window)))
result = np.array(list(map(lambda c: get_norms(c, window), shifted)))
# print(shifted.shape)
# print(result)
# print(result.shape)
# print(np.min(result))
z = np.argmin(result)
# print(z, result[z], shifts[z])
return shifts[z]
# norms = map(get_norms, columns, window )
# return shift
def main():
rain_path = r'E:\Rain'
img = Image.open(Path(rain_path, "053B_1632TLine.png"))
gimg = img.convert('L')
image_data = np.array(gimg).astype(np.int32)
v_output_data = np.empty(image_data.shape)
shifts = []
for q in range(image_data.shape[0]):
cur_column = image_data[q]
if q % 1000 == 0:
print(datetime.now(), q, v_output_data.shape, cur_column.shape)
# print(datetime.now(), q, v_output_data.shape, cur_column.shape, cur_column, file=fh)
if q > 7280:
print(q)
if q > 0:
shift = get_shift(cur_column, v_output_data[max(0, q - 100): q])
shifts.append(shift)
print(f'Shifting {q} by {shift}')
fixed_column = shift_func(cur_column, shift)
v_output_data[q] = fixed_column
else:
v_output_data[q] = cur_column
print(shifts)
return v_output_data
if __name__ == '__main__':
out = main()
with open('output_data.txt', 'w+') as fh:
print(out)
Image.fromarray(out).convert('L').save('output.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment