Guest User

Untitled

a guest
Oct 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. import cv2 as cv
  2.  
  3. def hue_constant_interpolation(_mosaic):
  4. _mosaic = cv.normalize(_mosaic.astype('double'), None, 0.0, 1.0, cv.NORM_MINMAX)
  5. # split the image into three channel
  6. b, g, r = cv.split(_mosaic)
  7.  
  8. # set the value which is useless to 0
  9. b = fill_with_zero_b(b)
  10. g = fill_with_zero_g(g)
  11. r = fill_with_zero_r(r)
  12.  
  13. # do the linear interpolation for green channel
  14. g = bilinear_interpolation_g(g)
  15.  
  16. # using the estimated green channel to calculate
  17. # the red and blue channels, hue has been interpolated
  18. g = cv.copyMakeBorder(g, 1, 1, 1, 1, cv.BORDER_REFLECT)
  19. b = hue_constant_channel_b(b, g)
  20. r = hue_constant_channel_r(r, g)
  21.  
  22. return cv.merge((b, g, r))
  23.  
  24.  
  25. def hue_constant_channel_r(estimating, interpolated):
  26. estimating = cv.copyMakeBorder(estimating, 1, 1, 1, 1, cv.BORDER_REFLECT)
  27. e_row_num = len(estimating)
  28. for row in range(1, e_row_num - 1):
  29. e_col_num = len(estimating[row])
  30. interpolated_row_median = np.median(interpolated[row])
  31. for col in range(1, e_col_num - 1):
  32. if row % 2 != 0 and col % 2 == 0:
  33. if estimating[row][col] == 0:
  34. g1 = interpolated[row][col - 1]
  35. g2 = interpolated[row][col + 1]
  36. estimating[row][col] = 0.5 * interpolated[row][col] * (
  37. estimating[row][col - 1] / (g1 if g1 != 0 else interpolated_row_median)
  38. + estimating[row][col + 1] / (g2 if g2 != 0 else interpolated_row_median)
  39. )
  40.  
  41. elif row % 2 == 0 and col % 2 != 0:
  42. if estimating[row][col] == 0:
  43. g1 = interpolated[row - 1][col]
  44. g2 = interpolated[row + 1][col]
  45. estimating[row][col] = 0.5 * interpolated[row][col] * (
  46. estimating[row - 1][col] / (g1 if g1 != 0 else interpolated_row_median)
  47. + estimating[row + 1][col] / (g2 if g2 != 0 else interpolated_row_median)
  48. )
  49.  
  50. elif row % 2 == 0 and col % 2 == 0:
  51. if estimating[row][col] == 0:
  52. g1 = interpolated[row - 1][col - 1]
  53. g2 = interpolated[row - 1][col + 1]
  54. g3 = interpolated[row + 1][col - 1]
  55. g4 = interpolated[row + 1][col + 1]
  56. estimating[row][col] = 0.25 * interpolated[row][col] * (
  57. estimating[row - 1][col - 1] / (g1 if g1 != 0 else interpolated_row_median)
  58. + estimating[row - 1][col + 1] / (g2 if g2 != 0 else interpolated_row_median)
  59. + estimating[row + 1][col - 1] / (g3 if g3 != 0 else interpolated_row_median)
  60. + estimating[row + 1][col + 1] / (g4 if g4 != 0 else interpolated_row_median)
  61. )
  62.  
  63. return estimating
  64.  
  65.  
  66. def hue_constant_channel_b(estimating, interpolated):
  67. estimating = cv.copyMakeBorder(estimating, 1, 1, 1, 1, cv.BORDER_REFLECT)
  68. e_row_num = len(estimating)
  69. for row in range(1, e_row_num - 1):
  70. e_col_num = len(estimating[row])
  71. interpolated_row_median = np.median(interpolated[row])
  72. for col in range(1, e_col_num - 1):
  73. if row % 2 == 0 and col % 2 != 0:
  74. if estimating[row][col] == 0:
  75. g1 = interpolated[row][col - 1]
  76. g2 = interpolated[row][col + 1]
  77. estimating[row][col] = 0.5 * interpolated[row][col] * (
  78. estimating[row][col - 1] / (g1 if g1 != 0 else interpolated_row_median)
  79. + estimating[row][col + 1] / (g2 if g2 != 0 else interpolated_row_median)
  80. )
  81.  
  82. elif row % 2 != 0 and col % 2 == 0:
  83. if estimating[row][col] == 0:
  84. g1 = interpolated[row - 1][col]
  85. g2 = interpolated[row + 1][col]
  86. estimating[row][col] = 0.5 * interpolated[row][col] * (
  87. estimating[row - 1][col] / (g1 if g1 != 0 else interpolated_row_median)
  88. + estimating[row + 1][col] / (g2 if g2 != 0 else interpolated_row_median)
  89. )
  90.  
  91. elif row % 2 != 0 and col % 2 != 0:
  92. if estimating[row][col] == 0:
  93. g1 = interpolated[row - 1][col - 1]
  94. g2 = interpolated[row - 1][col + 1]
  95. g3 = interpolated[row + 1][col - 1]
  96. g4 = interpolated[row + 1][col + 1]
  97. estimating[row][col] = 0.25 * interpolated[row][col] * (
  98. estimating[row - 1][col - 1] / (g1 if g1 != 0 else interpolated_row_median)
  99. + estimating[row - 1][col + 1] / (g2 if g2 != 0 else interpolated_row_median)
  100. + estimating[row + 1][col - 1] / (g3 if g3 != 0 else interpolated_row_median)
  101. + estimating[row + 1][col + 1] / (g4 if g4 != 0 else interpolated_row_median)
  102. )
  103.  
  104. return estimating
Add Comment
Please, Sign In to add comment