I would like to clip and map certain range in HSV color map. I have tried the below code to attempt to replicate the napari image viewer image processing but could not rescale the HSV range with my Python code.
import os, glob
import cv2
import matplotlib.pyplot as plt
from skimage import io, exposure
from natsort import natsorted
import numpy as np
image1 = cv2.imread('Garfield.tiff')
image2 = exposure.rescale_intensity (image1, in_range=(100, 230))
image3 = np.zeros(image2.shape, image2.dtype)
#alpha = 1.5 # Simple contrast control
#beta = 0 # Simple brightness control
for y in range(image2.shape[0]):
for x in range(image2.shape[1]):
for c in range(image2.shape[2]):
image3[y,x,c] = np.clip(2 * image2[y,x,c], 0, 255)
image4 = cv2.applyColorMap(image3, cv2.COLORMAP_HSV)
cv2.imwrite("New_Garfield.tiff", image4)
Here is the output image of my python code:
Here is the napari image viewer:
From napari gui: The contrast limits are a 2-tuple where the second value is larger than the first. The smaller contrast limit corresponds to the value of the image data that will get mapped to the color defined by 0 in the colormap. All values of image data smaller than this value will also get mapped to this color. The larger contrast limit corresponds to the value of the image data that will get mapped to the color defined by 1 in the colormap. All values of image data larger than this value will also get mapped to this color.
Hope you can help me to rescale HSV color map to a new range. Many thanks.
I also tried:
import cv2
import numpy as np
image1 = cv2.imread('Garfield.png')
for y in range(image1.shape[0]):
for x in range(image1.shape[1]):
for c in range(image1.shape[2]):
image1[y,x,c] = np.clip(2*image1[y,x,c], 0, 255)
OldMax = 255
OldMin = 75
NewMax = 180
NewMin = 0
OldRange = (OldMax - OldMin)
NewRange = (NewMax - NewMin)
for x in range (image1.shape[0]):
for y in range(image1.shape[1]):
for c in range(image1.shape[2]):
b = image1[:,:,0]
g = image1[:,:,1]
r = image1[:,:,2]
h = (((g - OldMin) * NewRange) / OldRange) + NewMin
c = (h, 255, 255)
image2 = cv2.cvtColor(image1, cv2.COLOR_HSV2BGR)
cv2.imwrite("Adjusted_Garfield.tiff", image2)
It produces which is not I wanted: Adjusted_Garfield