Commit 5bd6c411b1110e213255a33c76f40bff574508d7
1 parent
be1064b1
added contrast stretching to stim::image
Showing
1 changed file
with
15 additions
and
0 deletions
Show diff stats
stim/image/image.h
@@ -384,6 +384,21 @@ public: | @@ -384,6 +384,21 @@ public: | ||
384 | 384 | ||
385 | } | 385 | } |
386 | 386 | ||
387 | + /// Stretch the contrast of the image such that the minimum and maximum intensity match the given values | ||
388 | + image<T> stretch(T low, T high) { | ||
389 | + T maxval = maxv(); | ||
390 | + T minval = minv(); | ||
391 | + | ||
392 | + image<T> result = *this; //create a new image for output | ||
393 | + size_t N = size(); //get the number of values in the image | ||
394 | + double range = maxval - minval; //calculate the current range of the image | ||
395 | + double desired_range = high - low; //calculate the desired range of the image | ||
396 | + for (size_t n = 0; n < N; n++) { //for each element in the image | ||
397 | + result.data()[n] = desired_range * (img[n] - minval) / range; | ||
398 | + } | ||
399 | + return result; | ||
400 | + } | ||
401 | + | ||
387 | /// Copy the given data to the specified channel | 402 | /// Copy the given data to the specified channel |
388 | 403 | ||
389 | /// @param c is the channel number that the data will be copied to | 404 | /// @param c is the channel number that the data will be copied to |