-1

This has been somewhat answered before, however the solution is extremely slow compared to MATLAB's equivalent function. On my computer, the function takes 1.7 seconds to execute in python, while it only takes 0.009 seconds for the same image in MATLAB. The result is the same, but the speed gap is enormous.

Can this be optimized? Or are there any other python libraries that can do histogram stretching fast?

Yannis
  • 13
  • 3

1 Answers1

3

Those answers are not very good. imadjust simply does a linear stretch. You need to find the lower and upper bound (by default it uses the 1% and 99% of the data). Once you have lower and upper, you can just

out = (img - lower) * (255 / (upper - lower)
np.clip(out, 0, 255, out) # in-place clipping

You probably need img to be of a floating-point type for that to work correctly.

See this question for linear mapping in Numpy.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120