Return image after stretching or shrinking its intensity levels. The desired intensity range of the input and output, `in_range` and `out_range` respectively, are used to stretch or shrink the intensity range of the input image. See examples below. Parameters ---------- ima
(image, in_range="image", out_range="dtype")
| 129 | |
| 130 | |
| 131 | def rescale_intensity(image, in_range="image", out_range="dtype"): |
| 132 | """Return image after stretching or shrinking its intensity levels. |
| 133 | |
| 134 | The desired intensity range of the input and output, `in_range` and |
| 135 | `out_range` respectively, are used to stretch or shrink the intensity range |
| 136 | of the input image. See examples below. |
| 137 | |
| 138 | Parameters |
| 139 | ---------- |
| 140 | image : array |
| 141 | Image array. |
| 142 | in_range, out_range : str or 2-tuple, optional |
| 143 | Min and max intensity values of input and output image. |
| 144 | The possible values for this parameter are enumerated below. |
| 145 | |
| 146 | 'image' |
| 147 | Use image min/max as the intensity range. |
| 148 | 'dtype' |
| 149 | Use min/max of the image's dtype as the intensity range. |
| 150 | dtype-name |
| 151 | Use intensity range based on desired `dtype`. Must be valid key |
| 152 | in `DTYPE_RANGE`. |
| 153 | 2-tuple |
| 154 | Use `range_values` as explicit min/max intensities. |
| 155 | |
| 156 | Returns |
| 157 | ------- |
| 158 | out : array |
| 159 | Image array after rescaling its intensity. This image is the same dtype |
| 160 | as the input image. |
| 161 | |
| 162 | Notes |
| 163 | ----- |
| 164 | .. versionchanged:: 0.17 |
| 165 | The dtype of the output array has changed to match the output dtype, or |
| 166 | float if the output range is specified by a pair of floats. |
| 167 | |
| 168 | See Also |
| 169 | -------- |
| 170 | equalize_hist |
| 171 | |
| 172 | Examples |
| 173 | -------- |
| 174 | By default, the min/max intensities of the input image are stretched to |
| 175 | the limits allowed by the image's dtype, since `in_range` defaults to |
| 176 | 'image' and `out_range` defaults to 'dtype': |
| 177 | |
| 178 | >>> image = np.array([51, 102, 153], dtype=np.uint8) |
| 179 | >>> rescale_intensity(image) |
| 180 | array([ 0, 127, 255], dtype=uint8) |
| 181 | |
| 182 | It's easy to accidentally convert an image dtype from uint8 to float: |
| 183 | |
| 184 | >>> 1.0 * image |
| 185 | array([ 51., 102., 153.]) |
| 186 | |
| 187 | Use `rescale_intensity` to rescale to the proper range for float dtypes: |
| 188 |