Skip to content

ImageProvider

plantimager.controller.ImageProvider Link

ImageProvider Link

ImageProvider()

Bases: QQuickImageProvider

Provides images from memory to QML

Source code in plantimager/controller/ImageProvider.py
132
133
134
def __init__(self):
    super().__init__(QQmlImageProviderBase.ImageType.Image)
    self.images: dict[str, QImage] = {}

compute_focus Link

compute_focus(image)

Computes the focus measure of an image using the Sobel filter.

This function calculates the gradient magnitude of an input image in the x and y directions, combining the results to estimate focus. The computed focus measure highlights edges and areas of high intensity change, which are often indicative of regions in focus.

see https://blog.roboflow.com/computer-vision-camera-focus-guide/#tenengrad-function

Parameters:

Name Type Description Default
image ndarray

A 2D array representing the input grayscale image whose focus measure is to be computed.

required

Returns:

Type Description
ndarray

A 2D array representing the gradient magnitude of the input image, where higher values correspond to more focused regions.

Source code in plantimager/controller/ImageProvider.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def compute_focus(image: np.ndarray, /,):
    """
    Computes the focus measure of an image using the Sobel filter.

    This function calculates the gradient magnitude of an input image in the
    x and y directions, combining the results to estimate focus. The computed
    focus measure highlights edges and areas of high intensity change, which
    are often indicative of regions in focus.

    see https://blog.roboflow.com/computer-vision-camera-focus-guide/#tenengrad-function

    Parameters
    ----------
    image : np.ndarray
        A 2D array representing the input grayscale image whose focus measure is to be
        computed.

    Returns
    -------
    np.ndarray
        A 2D array representing the gradient magnitude of the input image,
        where higher values correspond to more focused regions.
    """
    downscaled = skimage.transform.downscale_local_mean(image, 2).astype(np.uint8)
    grad_x = skimage.filters.sobel(downscaled, axis=0)
    grad_y = skimage.filters.sobel(downscaled, axis=1)
    #plt_imshow(downscaled, title="downscaled")
    #plt_imshow(grad_x, title="grad_x", vmin=grad_x.min(), vmax=grad_x.max())
    #plt_imshow(grad_y, title="grad_y", vmin=grad_y.min(), vmax=grad_y.max())
    magnitude = np.sqrt(grad_x**2 + grad_y**2)
    magnitude = np.astype((magnitude-magnitude.min())/magnitude.max()*255, np.uint8)
    #smoothed = scipy.ndimage.gaussian_filter(magnitude, sigma=1)
    #plt_imshow(smoothed, title="smoothed")
    #magnitude = scipy.ndimage.grey_opening(magnitude, size=(3,3))
    #plt_imshow(magnitude, title="magnitude after opening")
    #plt_imshow(np.floor(magnitude/magnitude.max()*255))
    return skimage.transform.resize(magnitude, image.shape, anti_aliasing=False, mode='constant')

focus_highlight Link

focus_highlight(image, percentile)

Enhances the focus areas of an image by adding a red highlight to regions of the image that have a focus value greater than a specified percentile.

Parameters:

Name Type Description Default
image QImage

The input image to be processed. Must be in the QImage format.

required
percentile float

The percentile threshold for selecting focus areas. Focus values above this percentile will be highlighted.

required

Returns:

Type Description
QImage

A new image with highlighted focus areas in red.

Source code in plantimager/controller/ImageProvider.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def focus_highlight(image: QImage, percentile: float) -> QImage:
    """
    Enhances the focus areas of an image by adding a red highlight to regions of
    the image that have a focus value greater than a specified percentile.

    Parameters
    ----------
    image : QImage
        The input image to be processed. Must be in the QImage format.
    percentile : float
        The percentile threshold for selecting focus areas. Focus values above
        this percentile will be highlighted.

    Returns
    -------
    QImage
        A new image with highlighted focus areas in red.
    """
    image_array = qimage_to_ndarray_rgb(image)
    image_grayscale = qimage_to_ndarray_gray(image)
    focus_array = compute_focus(image_grayscale)
    focus_array = np.astype((focus_array-focus_array.min())/focus_array.max()*255, np.uint8)
    #plt_imshow(focus_array, vmin=focus_array.min(), vmax=focus_array.max())
    highlight_mask = focus_array > np.percentile(focus_array.astype(np.float32), percentile, overwrite_input=True)
    highlight_image = image_array.copy()
    highlight_image[:, :, :] = focus_array[:, :, None]
    new_mask = np.zeros_like(highlight_image, dtype=bool)
    #new_mask[:,:,1] = highlight_mask
    new_mask[:,:,0] = highlight_mask
    #highlight_image[highlight_mask] = 255 - highlight_image[highlight_mask]
    #highlight_image[new_mask] = 255
    #plt_imshow(255*highlight_mask.astype(np.uint8))
    #plt_imshow(highlight_image)
    return ndarray_to_qimage(
        highlight_image,
        QImage.Format.Format_RGB888,
    )