0

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.

Here is the input image: Garfield input

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: New Garfield

Here is the napari image viewer: napari

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

Stone Gold
  • 35
  • 7
  • According to the [napari sources](https://github.com/napari/napari/blob/main/napari/utils/colormaps/vendored/_cm.py), it looks like the HSV color map of napari is different from HSV color map of OpenCV. Following the source code is challenging... Also Note that you assumption that gamma = 2 applies scaling by 2 is probably wrong. – Rotem May 05 '23 at 11:37
  • For the contrast limits if I set from 75 to 255, I think napari set the 75 to red as H=0/360, 255 to red as H=360/360. The number from 75 to 255 may scale in HSV in the range H=0/360 to H=360/360. – Stone Gold May 05 '23 at 13:21
  • I would like to set H=0 for grey scale value = 75, H=360 for grey scale value = 255. I need to rescale H from 0 to 360 for greyscale from 75 to 255. – Stone Gold May 05 '23 at 13:26

0 Answers0