'Use a derived mapping function to modify s?

I've read this entire paper multiple times, and I've found a way to make everything in python... Except this (Article II, section C). They want me to upscale the saturation values with a new min of 2.589 times the old min, and a new max of 0.9 times the old max. How do i achieve this?

Edit: Thanks to fmw42 i now have a code that roughly does what it should, but it throws an error when i use my dtw value "'tuple' object has no attribute 'dtype'" My code:

hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv_img)
clahe = cv2.createCLAHE(clipLimit=2.6, tileGridSize=(3,3))
clahe_v = clahe.apply(v)
dwts = pywt.dwt(s, 'bior1.3')

smin = np.amin(dwts)
smax = np.amax(dwts)
print("smin:", smin, "smax:", smax)
sleep(2)
smin_new = 2.589 * smin
smax_new = 0.9 * smax
print("smin_new:", smin_new, "smax_new:", smax_new)
sleep(2)

magic_s = skimage.exposure.rescale_intensity(dwts, in_range=(smin,smax), out_range=(smin_new,smax_new)).astype(np.uint8)

sorry for the google link btw :( Article link: [https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwjx2_Cay9z3AhWDyYsKHdeFCn8QFnoECAoQAQ&url=https%3A%2F%2Fcore.ac.uk%2Fdownload%2Fpdf%2F228552574.pdf&usg=AOvVaw15wEM7_gVgvmVZhmHNTksN][1]



Solution 1:[1]

In Python/OpenCV/Numpy/Skimage, you can use np.amin() and np.amax() to get the min and max values in the image. Then compute the new min and max from your " 2.589 times the old min, and a new max of 0.9 times the old max" in Python simple math. Then use skimage.exposure.rescale_intensity() to do the processing from input values to new output values.

Here is an example using 5 times the old min and 0.5 time the old max so that the change is more visible.

Input:

enter image description here

import cv2
import numpy as np
import skimage.exposure

# read image
img = cv2.imread("lena.png")

# convert bgr to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# extract channels
h,s,v = cv2.split(hsv)

# get min and max of saturation channel
smin = np.amin(s)
smax = np.amax(s)
print("smin:", smin, "smax:", smax)

# compute new saturation values
#smin_new = 2.589 * smin
#smax_new = 0.9 * smax
smin_new = 5 * smin
smax_new = 0.5 * smax
print("smin_new:", smin_new, "smax_new:", smax_new)

# modify saturation
s_new = skimage.exposure.rescale_intensity(s, in_range=(smin,smax), out_range=(smin_new,smax_new)).astype(np.uint8)

# merge hsv channels
hsv_new = cv2.merge([h,s_new,v])

# convert hsv to bgr
result = cv2.cvtColor(hsv_new, cv2.COLOR_HSV2BGR)

# save result
cv2.imwrite('lena_modified_saturation.png', result)

cv2.imshow('saturation', s)
cv2.imshow('saturation_modified', s_new)
cv2.imshow('result', result)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Calculations:

smin: 10 smax: 255 smin_new: 50 smax_new: 127.5

Result:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fmw42