[feature request] Image Histogram Transformation
See original GitHub issueIt is often useful (especially in the field of astronomy) to transform the histogram of images. I would like to suggest an image histogram transformation function (under torchvision.transforms) that transforms the histogram of an image to match that of a template image as closely as possible. For instance, consider the following function:
def match_histogram(source, template):
source = np.asanyarray(source)
template = np.asanyarray(template)
oldshape = source.shape
source = source.ravel()
template = template.ravel()
# get the set of unique pixel values and their corresponding indices and
# counts
s_values, bin_idx, s_counts = np.unique(source, return_inverse=True,
return_counts=True)
t_values, t_counts = np.unique(template, return_counts=True)
# take the cumsum of the counts and normalize by the number of pixels to
# get the empirical cumulative distribution functions for the source and
# template images (maps pixel value --> quantile)
s_quantiles = np.cumsum(s_counts).astype(np.float32)
s_quantiles /= s_quantiles[-1]
t_quantiles = np.cumsum(t_counts).astype(np.float32)
t_quantiles /= t_quantiles[-1]
# interpolate linearly to find the pixel values in the template image
# that corresponds most closely to the quantiles in the source image
interp_t_values = np.interp(s_quantiles, t_quantiles, t_values)
return interp_t_values[bin_idx].reshape(oldshape)
The function above is not optimal since it has to recalculate template image information. It is not discretized for float type images. It only performs for highly discretized images such as png (0-255 bins). It also performs poorly when the number of diverse pixels is too low which might be fixed by adding small noise.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Histogram transformations — Basics of Image Processing
An histogram transformation consists in applying a mathematical function to the intensity distribution. Generally, the transformations are useful to improve ...
Read more >Image Processing
Histogram equalization is a method to process images in order to adjust the contrast of an image by modifying the intensity distribution of...
Read more >Histogram Equalization | by Shreenidhi Sudhakar
Image Contrast Enhancement ... Histogram Equalization is a computer image processing technique used to improve contrast in images . ... Get this newsletter ......
Read more >A Tutorial to Histogram Equalization | by Kyaw Saw Htoon
Histogram Equalization is an image processing technique that adjusts the contrast of an image by using its histogram. To enhance the image's ......
Read more >Image histogram—ArcGIS Pro | Documentation
Transformation. Some analytical methods require that data be normally distributed. When the data is skewed (the distribution is disproportionate), you might ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey 😃
I was browsing throught the vison issues and found that one, turns out I actually did some work on histogram specification some time ago. Something like that :
I wrote it as a cuda module as I was running the transform in an optimisation loop and needed it to be fast. The code is available over here if that can be useful : https://github.com/pierre-wilmot/NeuralTextureSynthesis/ Happy to help cleaning it up if you think it’s worth adding to the vision repo.
@Miladiouss So, I created this function that essentially matches the histogram of one image to another image, and it should hopefully help individuals with use cases like astronomy & neural style transfer.
I wrote the code for a different PyTorch project (pytorch/captum), but Torchvision is free to use it as well! @fmassa
The inner functions can be eliminated easily for TorchScript / JIT compatibility, and it’s fully autograd compatible.