Skip to content

Image Analyzer

Provides utility methods to analyze image quality and pixel data.

This class extracts physical metrics from images, such as brightness, contrast, and sharpness (blur score), which are essential for understanding the quality of a computer vision dataset.

Source code in tools/stats/image_analyzer.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class ImageContentAnalyzer:
    """
    Provides utility methods to analyze image quality and pixel data.

    This class extracts physical metrics from images, such as brightness,
    contrast, and sharpness (blur score), which are essential for
    understanding the quality of a computer vision dataset.
    """

    @staticmethod
    def analyze_metrics(img_path: str) -> Dict[str, float]:
        """
        Calculates brightness, contrast, and blur score for an image file.

        The method performs the following steps:
        1. Reads the image from the disk in BGR format.
        2. Converts the image to grayscale.
        3. Computes the mean (brightness), standard deviation (contrast),
           and Laplacian variance (blur score).

        Args:
            img_path (Path): The file system path to the image.

        Returns:
            Dict[str, float]: A dictionary containing calculated metrics.
                Returns an empty dictionary if the image cannot be read.
        """
        try:
            image = cv2.imread(str(img_path))
        except Exception:
            return {}

        if image is None:
            return {}

        image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        data = {
            ImageStatsKeys.im_brightness: round(float(np.mean(image_gray)), 2),
            ImageStatsKeys.im_contrast: round(float(np.std(image_gray)), 2),
            ImageStatsKeys.im_blur_score: round(float(cv2.Laplacian(image_gray, cv2.CV_64F).var()), 2)
        }
        return data

analyze_metrics(img_path) staticmethod

Calculates brightness, contrast, and blur score for an image file.

The method performs the following steps: 1. Reads the image from the disk in BGR format. 2. Converts the image to grayscale. 3. Computes the mean (brightness), standard deviation (contrast), and Laplacian variance (blur score).

Parameters:

Name Type Description Default
img_path Path

The file system path to the image.

required

Returns:

Type Description
Dict[str, float]

Dict[str, float]: A dictionary containing calculated metrics. Returns an empty dictionary if the image cannot be read.

Source code in tools/stats/image_analyzer.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@staticmethod
def analyze_metrics(img_path: str) -> Dict[str, float]:
    """
    Calculates brightness, contrast, and blur score for an image file.

    The method performs the following steps:
    1. Reads the image from the disk in BGR format.
    2. Converts the image to grayscale.
    3. Computes the mean (brightness), standard deviation (contrast),
       and Laplacian variance (blur score).

    Args:
        img_path (Path): The file system path to the image.

    Returns:
        Dict[str, float]: A dictionary containing calculated metrics.
            Returns an empty dictionary if the image cannot be read.
    """
    try:
        image = cv2.imread(str(img_path))
    except Exception:
        return {}

    if image is None:
        return {}

    image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    data = {
        ImageStatsKeys.im_brightness: round(float(np.mean(image_gray)), 2),
        ImageStatsKeys.im_contrast: round(float(np.std(image_gray)), 2),
        ImageStatsKeys.im_blur_score: round(float(cv2.Laplacian(image_gray, cv2.CV_64F).var()), 2)
    }
    return data