Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2 as cv
- def hue_constant_interpolation(_mosaic):
- _mosaic = cv.normalize(_mosaic.astype('double'), None, 0.0, 1.0, cv.NORM_MINMAX)
- # split the image into three channel
- b, g, r = cv.split(_mosaic)
- # set the value which is useless to 0
- b = fill_with_zero_b(b)
- g = fill_with_zero_g(g)
- r = fill_with_zero_r(r)
- # do the linear interpolation for green channel
- g = bilinear_interpolation_g(g)
- # using the estimated green channel to calculate
- # the red and blue channels, hue has been interpolated
- g = cv.copyMakeBorder(g, 1, 1, 1, 1, cv.BORDER_REFLECT)
- b = hue_constant_channel_b(b, g)
- r = hue_constant_channel_r(r, g)
- return cv.merge((b, g, r))
- def hue_constant_channel_r(estimating, interpolated):
- estimating = cv.copyMakeBorder(estimating, 1, 1, 1, 1, cv.BORDER_REFLECT)
- e_row_num = len(estimating)
- for row in range(1, e_row_num - 1):
- e_col_num = len(estimating[row])
- interpolated_row_median = np.median(interpolated[row])
- for col in range(1, e_col_num - 1):
- if row % 2 != 0 and col % 2 == 0:
- if estimating[row][col] == 0:
- g1 = interpolated[row][col - 1]
- g2 = interpolated[row][col + 1]
- estimating[row][col] = 0.5 * interpolated[row][col] * (
- estimating[row][col - 1] / (g1 if g1 != 0 else interpolated_row_median)
- + estimating[row][col + 1] / (g2 if g2 != 0 else interpolated_row_median)
- )
- elif row % 2 == 0 and col % 2 != 0:
- if estimating[row][col] == 0:
- g1 = interpolated[row - 1][col]
- g2 = interpolated[row + 1][col]
- estimating[row][col] = 0.5 * interpolated[row][col] * (
- estimating[row - 1][col] / (g1 if g1 != 0 else interpolated_row_median)
- + estimating[row + 1][col] / (g2 if g2 != 0 else interpolated_row_median)
- )
- elif row % 2 == 0 and col % 2 == 0:
- if estimating[row][col] == 0:
- g1 = interpolated[row - 1][col - 1]
- g2 = interpolated[row - 1][col + 1]
- g3 = interpolated[row + 1][col - 1]
- g4 = interpolated[row + 1][col + 1]
- estimating[row][col] = 0.25 * interpolated[row][col] * (
- estimating[row - 1][col - 1] / (g1 if g1 != 0 else interpolated_row_median)
- + estimating[row - 1][col + 1] / (g2 if g2 != 0 else interpolated_row_median)
- + estimating[row + 1][col - 1] / (g3 if g3 != 0 else interpolated_row_median)
- + estimating[row + 1][col + 1] / (g4 if g4 != 0 else interpolated_row_median)
- )
- return estimating
- def hue_constant_channel_b(estimating, interpolated):
- estimating = cv.copyMakeBorder(estimating, 1, 1, 1, 1, cv.BORDER_REFLECT)
- e_row_num = len(estimating)
- for row in range(1, e_row_num - 1):
- e_col_num = len(estimating[row])
- interpolated_row_median = np.median(interpolated[row])
- for col in range(1, e_col_num - 1):
- if row % 2 == 0 and col % 2 != 0:
- if estimating[row][col] == 0:
- g1 = interpolated[row][col - 1]
- g2 = interpolated[row][col + 1]
- estimating[row][col] = 0.5 * interpolated[row][col] * (
- estimating[row][col - 1] / (g1 if g1 != 0 else interpolated_row_median)
- + estimating[row][col + 1] / (g2 if g2 != 0 else interpolated_row_median)
- )
- elif row % 2 != 0 and col % 2 == 0:
- if estimating[row][col] == 0:
- g1 = interpolated[row - 1][col]
- g2 = interpolated[row + 1][col]
- estimating[row][col] = 0.5 * interpolated[row][col] * (
- estimating[row - 1][col] / (g1 if g1 != 0 else interpolated_row_median)
- + estimating[row + 1][col] / (g2 if g2 != 0 else interpolated_row_median)
- )
- elif row % 2 != 0 and col % 2 != 0:
- if estimating[row][col] == 0:
- g1 = interpolated[row - 1][col - 1]
- g2 = interpolated[row - 1][col + 1]
- g3 = interpolated[row + 1][col - 1]
- g4 = interpolated[row + 1][col + 1]
- estimating[row][col] = 0.25 * interpolated[row][col] * (
- estimating[row - 1][col - 1] / (g1 if g1 != 0 else interpolated_row_median)
- + estimating[row - 1][col + 1] / (g2 if g2 != 0 else interpolated_row_median)
- + estimating[row + 1][col - 1] / (g3 if g3 != 0 else interpolated_row_median)
- + estimating[row + 1][col + 1] / (g4 if g4 != 0 else interpolated_row_median)
- )
- return estimating
Add Comment
Please, Sign In to add comment