Skip to content

[QUESTION] Image histogram example #4549

@xevolesi

Description

@xevolesi

Hi, thank you very much for you work, DALI is really a great library. DALI accelerated my model training by almost 19 times compared to another augmentation library and it's really impressive. Although I'm still missing a few things. For example, i want to use custom augmentation and i need to calculate image histogram in the following way (I need it for Otsu thresholding):

import cv2
import numpy as np

IMAGE_PATH = "image.jpeg"


if __name__ == "__main__":
    image = cv2.imread(IMAGE_PATH)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ...
    # Find normalized_histogram, and its cumulative distribution function
    hist = cv2.calcHist([image], [0], None, [256], [0, 256])
    hist_norm = hist.ravel() / hist.sum()
    Q = hist_norm.cumsum()

Could you please guide me? I've tried something like this:

def dali_calc_hist(decoded_images: DALITensorList, image_sizes: DALITensorList):
       gray = fn.color_space_conversion(
            decoded_images,
            image_type=dali_types.RGB,
            output_type=dali_types.GRAY,
        )
        hist = [0] * 256
        cumsum = [0] * 256
        n = image_size[0] * image_size[1]
        _sum = fn.constant(idata=[0], dtype=dali_types.FLOAT)
        for gray_lvl in range(256):
            hist[gray_lvl] = fn.reductions.sum(
                fn.cast(gray == gray_lvl, dtype=dali_types.UINT8)
            ) / n
            _sum = _sum + hist[gray_lvl]
            cumsum[gray_lvl] = _sum
        return hist, cumsum
        

but i really feel that i'm doing something wrong, because actually in hist and cumsum i have nested DataNodes.

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions