| 1 |
import matplotlib |
| 2 |
matplotlib.use('Agg') |
| 3 |
import matplotlib.pyplot as plt |
| 4 |
from cv2 import cvtColor, COLOR_RGB2GRAY, COLOR_BGR2RGB, imread, IMREAD_UNCHANGED |
| 5 |
from numpy import ndarray |
| 6 |
from numpy import uint8, uint16 |
| 7 |
|
| 8 |
from frames import debayer_frame |
| 9 |
|
| 10 |
|
| 11 |
def apply_bayer(frame_in, bayer_pattern): |
| 12 |
""" |
| 13 |
Convert a color (3D) frame into a 2D one by applying a Bayer pattern. |
| 14 |
|
| 15 |
:param frame_in: Color (three channel) image, either 8 or 16 bit unsigned int. |
| 16 |
:param bayer_pattern: Pattern used to convert the input image into the output image. One out |
| 17 |
of 'Force Bayer RGGB', 'Force Bayer GRBG', 'Force Bayer GBRG', |
| 18 |
'Force Bayer BGGR' |
| 19 |
:return: B/W (one channel) image with the same type and pixel dimensions as frame_in. |
| 20 |
""" |
| 21 |
|
| 22 |
|
| 23 |
pixel_y, pixel_x, dim = frame_in.shape |
| 24 |
frame_out = ndarray((pixel_y, pixel_x), dtype=frame_in.dtype) |
| 25 |
|
| 26 |
if dim != 3: |
| 27 |
raise Exception("Error: Input frame expected to be 3D (color)") |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
if bayer_pattern == 'Force Bayer RGGB': |
| 32 |
channels = (0, 1, 1, 2) |
| 33 |
elif bayer_pattern == 'Force Bayer GRBG': |
| 34 |
channels = (1, 0, 2, 1) |
| 35 |
elif bayer_pattern == 'Force Bayer GBRG': |
| 36 |
channels = (1, 2, 0, 1) |
| 37 |
elif bayer_pattern == 'Force Bayer BGGR': |
| 38 |
channels = (2, 1, 1, 0) |
| 39 |
else: |
| 40 |
raise Exception("Invalid Bayer transformation: " + str(bayer_pattern)) |
| 41 |
|
| 42 |
frame_out[0:pixel_y:2, 0:pixel_x:2] = frame_in[0:pixel_y:2, 0:pixel_x:2, channels[0]] |
| 43 |
frame_out[0:pixel_y:2, 1:pixel_x:2] = frame_in[0:pixel_y:2, 1:pixel_x:2, channels[1]] |
| 44 |
frame_out[1:pixel_y:2, 0:pixel_x:2] = frame_in[1:pixel_y:2, 0:pixel_x:2, channels[2]] |
| 45 |
frame_out[1:pixel_y:2, 1:pixel_x:2] = frame_in[1:pixel_y:2, 1:pixel_x:2, channels[3]] |
| 46 |
|
| 47 |
return frame_out |
| 48 |
|
| 49 |
|
| 50 |
def show_image(frame, comment): |
| 51 |
""" |
| 52 |
Use Matplotlib to show a given frame. |
| 53 |
|
| 54 |
:param frame: Color or B/W image. |
| 55 |
:param comment: Text string (to be displayed as window title) |
| 56 |
:return: - |
| 57 |
""" |
| 58 |
print(comment + ", shape: " + str(frame.shape) + ", type: " + str(frame.dtype)) |
| 59 |
|
| 60 |
|
| 61 |
if frame.dtype == uint16: |
| 62 |
frame_shown = (frame >> 8).astype(uint8) |
| 63 |
else: |
| 64 |
frame_shown = frame |
| 65 |
|
| 66 |
plt.title(comment) |
| 67 |
if len(frame_shown.shape) == 3: |
| 68 |
plt.imshow(frame_shown) |
| 69 |
else: |
| 70 |
plt.imshow(frame_shown, cmap='gray') |
| 71 |
plt.show() |
| 72 |
|
| 73 |
|
| 74 |
filename = 'Photodisc.png' |
| 75 |
input_image = imread(filename, IMREAD_UNCHANGED) |
| 76 |
|
| 77 |
DEPTH = 8 |
| 78 |
|
| 79 |
if input_image is None: |
| 80 |
raise IOError("Cannot read image file. Possible cause: Path contains non-ascii characters") |
| 81 |
|
| 82 |
if DEPTH == 16: |
| 83 |
input_image = input_image.astype(uint16) << 8 |
| 84 |
|
| 85 |
|
| 86 |
if len(input_image.shape) == 3: |
| 87 |
image_color = cvtColor(input_image, COLOR_BGR2RGB) |
| 88 |
image_grayscale = cvtColor(image_color, COLOR_RGB2GRAY) |
| 89 |
else: |
| 90 |
image_grayscale = input_image |
| 91 |
pixel_y = input_image.shape[0] |
| 92 |
pixel_x = input_image.shape[1] |
| 93 |
image_color = ndarray((pixel_y, pixel_x, 3)) |
| 94 |
image_color[:, :, 0] = input_image |
| 95 |
image_color[:, :, 1] = input_image |
| 96 |
image_color[:, :, 2] = input_image |
| 97 |
|
| 98 |
show_image(image_color, "Color image read") |
| 99 |
|
| 100 |
|
| 101 |
image_bayer_encoded = apply_bayer(image_color, 'Force Bayer RGGB') |
| 102 |
|
| 103 |
show_image(image_bayer_encoded, "Image Bayer encoded RGGB") |
| 104 |
image_bayer_decoded = debayer_frame(image_bayer_encoded, 'Force Bayer RGGB') |
| 105 |
show_image(image_bayer_decoded, "Image Bayer decoded RGGB") |
| 106 |
|
| 107 |
|
| 108 |
image_bayer_encoded = apply_bayer(image_color, 'Force Bayer GRBG') |
| 109 |
image_bayer_decoded = debayer_frame(image_bayer_encoded, 'Force Bayer GRBG') |
| 110 |
show_image(image_bayer_decoded, "Image Bayer decoded GRBG") |
| 111 |
|
| 112 |
|
| 113 |
image_bayer_encoded = apply_bayer(image_color, 'Force Bayer GBRG') |
| 114 |
image_bayer_decoded = debayer_frame(image_bayer_encoded, 'Force Bayer GBRG') |
| 115 |
show_image(image_bayer_decoded, "Image Bayer decoded GBRG") |
| 116 |
|
| 117 |
|
| 118 |
image_bayer_encoded = apply_bayer(image_color, 'Force Bayer BGGR') |
| 119 |
image_bayer_decoded = debayer_frame(image_bayer_encoded, 'Force Bayer BGGR') |
| 120 |
show_image(image_bayer_decoded, "Image Bayer decoded BGGR") |
| 121 |
|
| 122 |
|
| 123 |
image_bayer_decoded = debayer_frame(image_color, 'Grayscale') |
| 124 |
show_image(image_bayer_decoded, "Image Grayscale") |
| 125 |
|
| 126 |
|
| 127 |
image_bgr = debayer_frame(image_color, 'BGR') |
| 128 |
show_image(image_bgr, "Image BGR") |