Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %matplotlib inline
- from skimage.io import imread, imshow, imsave
- from skimage import img_as_float
- import numpy as np
- import logging
- logger = logging.getLogger()
- logger.setLevel(logging.DEBUG)
- def read_and_split(image):
- img = imread(image)
- logger.debug("size of photo:", img.shape)
- height_div_3 = img.shape[0] // 3
- b = img[ : height_div_3, : ]
- g = img[height_div_3 : 2 * height_div_3, : ]
- r = img[2 * height_div_3 : 3 * height_div_3, : ]
- logger.debug(b.shape, g.shape, r.shape)
- b, g, r = list(map(img_as_float, (b, g, r, )))
- return b, g, r
- def clip_border(b, g, r):
- height, width = b.shape
- centre_part = lambda x: x[height//20 : -height//20 , width//20 : -width//20]
- b = centre_part(b)
- g = centre_part(g)
- r = centre_part(r)
- return b, g, r
- def find_shift(canal_1, canal_2, axis, max_shift = 15):
- min_corelation = 0
- right_shift = 0
- for shift in range(-max_shift, max_shift + 1):
- canal_2_shift = np.roll(canal_2, shift, axis)
- current_corelation = (canal_1 * canal_2_shift).sum()
- if current_corelation > min_corelation:
- min_corelation = current_corelation
- right_shift = shift
- return right_shift
- def combine_image(r, g, b, gr_x, gr_y, gb_x, gb_y):
- r_sh = np.roll(r, gr_x, axis = 0)
- r_sh = np.roll(r_sh, gr_y, axis = 1)
- b_sh = np.roll(b, gb_x, axis = 0)
- b_sh = np.roll(b_sh, gb_y, axis = 1)
- combined_img = np.stack((r_sh, g, b_sh), 2)
- return combined_img
- def result_img(image):
- b, g, r = read_and_split(image)
- b, g, r = clip_border(b, g, r)
- gr_x = find_shift(g, r, 0)
- gr_y = find_shift(g, r, 1)
- gb_x = find_shift(g, b, 0)
- gb_y = find_shift(g, b, 1)
- logger.debug(gr_x, gr_y)
- logger.debug(gb_x, gb_y)
- result_img = combine_image(r, g, b, gr_x, gr_y, gb_x, gb_y)
- return result_img
- def align(image, green_point_coord):
- row_g, col_g = green_point_coord
- b, g, r = read_and_split(image)
- h_dif = b.shape[0]
- b, g, r = clip_border(b, g, r)
- gb_x = find_shift(g, b, 0)
- gb_y = find_shift(g, b, 1)
- gr_x = find_shift(g, r, 0)
- gr_y = find_shift(g, r, 1)
- print(gr_x, gr_y)
- print(gb_x, gb_y
- row_b = row_g - gb_x - h_dif
- col_b = col_g - gb_y
- row_r = row_g - gr_x + h_dif
- col_r = col_g - gr_y
- return row_b, col_b, row_r, col_r
- align("https://stepik.org/media/attachments/lesson/58182/00.png", (508, 237))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement