Harvester returns acquired images as 1D NumPy arrays to avoid imposing a specific shape that might limit downstream algorithms. To use the image in applications like VisPy or OpenCV, you must reshape the array using the metadata provided by the component.
Mono Formats
For monochrome images, reshape using height and width:
content = component.data.reshape(height, width)
Color Formats (RGB, RGBA, BGR, BGRA)
For color images, you must include the number of components per pixel. Note that component.num_components_per_pixel returns a float, so you must cast it to an int to avoid NumPy errors.
If the format is bgr_formats, you may need to swap the R and B channels to convert to RGB:
content = component.data.reshape(
height,
width,
int(component.num_components_per_pixel)
)
if data_format in bgr_formats:
content = content[:, :, ::-1]
from harvesters.util.pfnc import mono_location_formats, rgb_formats, bgr_formats, rgba_formats, bgra_formats
payload = buffer.payload
component = payload.components[0]
width = component.width
height = component.height
data_format = component.data_format
if data_format in mono_location_formats:
content = component.data.reshape(height, width)
elif data_format in rgb_formats or data_format in rgba_formats or data_format in bgr_formats or data_format in bgra_formats:
content = component.data.reshape(
height, width,
int(component.num_components_per_pixel)
)
if data_format in bgr_formats:
content = content[:, :, ::-1]