I am trying to work on HSV -> RGB with formula manually and without openCV.
I tried to calculate HSV -> RGB color space, and gives me almost the same picture I wanted, but there is some kind of noise.
Some image has many noise, and some really don't have noise(there are "some" noises.)
And I tried debugging.. and can't seem to know what is wrong with my code.
is it the Formulla I wrote ? Or did I miss something?..
I can't know what I don't know. So, I need help..
here's my code, and I'll post original images and result images that have noises below.
First, here's my code.
def HSI_to_bgr(h, s, i):
h = degrees(h)
if 0 < h <= 120 :
b = i * (1 - s)
r = i * (1 + (s * cos(radians(h)) / cos(radians(60) - radians(h))))
g = i * 3 - (r + b)
elif 120 < h <= 240:
h -= 120
r = i * (1 - s)
g = i * (1 + (s * cos(radians(h)) / cos(radians(60) - radians(h))))
b = 3 * i - (r + g)
elif 0 < h <= 360:
h -= 240
g = i * (1 - s)
b = i * (1 + (s * cos(radians(h)) / cos(radians(60) - radians(h))))
r = i * 3 - (g + b)
return [b, g, r]
def rgb_to_hue(b, g, r):
angle = 0
if b != g != r:
angle = 0.5 * ((r - g) + (r - b)) / sqrt(((r - g) ** 2) + (r - b) * (g - b))
if b <= g:
return acos(angle)
else:
return 2 * pi - acos(angle)
def rgb_to_intensity(b, g, r):
val = (b + g + r) / 3.
if val == 0:
return 0
else:
return val
def rgb_to_saturity(b, g, r):
if r + g + b != 0:
return 1. - 3. * np.min([r, g, b]) / (r + g + b)
else:
return 0
def point_process_colorscale_negative_intensity(file_path):
src = cv2.imread(file_path, cv2.IMREAD_COLOR)
height, width = src.shape[0], src.shape[1]
new_image = np.zeros((height, width, 3), dtype=np.uint8)
I = np.zeros((height, width))
S = np.zeros((height, width))
H = np.zeros((height, width))
for i in range(height) :
for j in range(width) :
b = src[i][j][0] / 255.
g = src[i][j][1] / 255.
r = src[i][j][2] / 255.
H[i][j] = rgb_hsi_conversion.rgb_to_hue(b, g, r)
S[i][j] = rgb_hsi_conversion.rgb_to_saturity(b, g, r)
I[i][j] = rgb_hsi_conversion.rgb_to_intensity(b, g, r)
# I[i][j] = 1. - I[i][j]
bgr_tuple = rgb_hsi_conversion.HSI_to_bgr(H[i][j], S[i][j], I[i][j])
new_image[i][j][0] = round(bgr_tuple[0] * 255.)
new_image[i][j][1] = round(bgr_tuple[1] * 255.)
new_image[i][j][2] = round(bgr_tuple[2] * 255.)
return new_image, src
Here's my result Image.
They all have some noises. especially, the baboon's nose and the kid's purple noise is worse.
Thank you in advance, and if I did not give enough information, I will try my best to add.



