Utility to equalize input image based on the histogram. If `skimage` installed, will leverage `skimage.exposure.histogram`, otherwise, use `np.histogram` instead. Args: img: input image to equalize. mask: if provided, must be ndarray of bools or 0s and 1s, and same
(
img: np.ndarray, mask: np.ndarray | None = None, num_bins: int = 256, min: int = 0, max: int = 255
)
| 1838 | |
| 1839 | |
| 1840 | def equalize_hist( |
| 1841 | img: np.ndarray, mask: np.ndarray | None = None, num_bins: int = 256, min: int = 0, max: int = 255 |
| 1842 | ) -> np.ndarray: |
| 1843 | """ |
| 1844 | Utility to equalize input image based on the histogram. |
| 1845 | If `skimage` installed, will leverage `skimage.exposure.histogram`, otherwise, use |
| 1846 | `np.histogram` instead. |
| 1847 | |
| 1848 | Args: |
| 1849 | img: input image to equalize. |
| 1850 | mask: if provided, must be ndarray of bools or 0s and 1s, and same shape as `image`. |
| 1851 | only points at which `mask==True` are used for the equalization. |
| 1852 | num_bins: number of the bins to use in histogram, default to `256`. for more details: |
| 1853 | https://numpy.org/doc/stable/reference/generated/numpy.histogram.html. |
| 1854 | min: the min value to normalize input image, default to `0`. |
| 1855 | max: the max value to normalize input image, default to `255`. |
| 1856 | |
| 1857 | """ |
| 1858 | |
| 1859 | orig_shape = img.shape |
| 1860 | hist_img = img[np.array(mask, dtype=bool)] if mask is not None else img |
| 1861 | if has_skimage: |
| 1862 | hist, bins = exposure.histogram(hist_img.flatten(), num_bins) |
| 1863 | else: |
| 1864 | hist, bins = np.histogram(hist_img.flatten(), num_bins) |
| 1865 | bins = (bins[:-1] + bins[1:]) / 2 |
| 1866 | |
| 1867 | cum = hist.cumsum() |
| 1868 | # normalize the cumulative result |
| 1869 | cum = rescale_array(arr=cum, minv=min, maxv=max) |
| 1870 | |
| 1871 | # apply linear interpolation |
| 1872 | img = np.interp(img.flatten(), bins, cum) |
| 1873 | return img.reshape(orig_shape) |
| 1874 | |
| 1875 | |
| 1876 | class Fourier: |
no test coverage detected
searching dependent graphs…