我正在嘗試將 RGB 影像轉換為其值組件。我不能使用 RGB2HSV 函式,因為我被指示在沒有它的情況下轉換它。如果我沒記錯的話,影像的值是像素中 R、G 和 B 分量的最大值;所以這就是我試圖實作的。
'''
rgb_copy = self.rgb.copy()
width, height, other = rgb_copy.shape
for i in range(0, width-1):
for j in range(0, height-1):
# normalize the rgb values
[r, g, b] = self.rgb[i, j]
r_norm = r/255.0
g_norm = g/255.0
b_norm = b/255.0
# find the maximum of the three values
max_value = max(r_norm, g_norm, b_norm)
rgb_copy[i, j] = (max_value, max_value, max_value)
cv2.imshow('Value', rgb_copy)
cv2.waitKey()
'''
不幸的是,這似乎不起作用。當我使用內置函式轉換它時,它只回傳一個與 Value 組件不同的黑色影像。
誰能幫忙或看看我哪里出錯了?
uj5u.com熱心網友回復:
詳細說明我上面的評論:
因為您嘗試通過除以 255 來標準化 RGB 值,所以我假設您的 RGB 影像是 3 通道無符號字符影像(每個通道都是 0..255 之間的無符號字符值)。
當你克隆它時:
rgb_copy = self.rgb.copy()
您制作rgb_copy相同型別的影像(即 3 通道無符號字符)。然后,您嘗試使用每個通道的標準化 0..1 浮點值填充它。相反,您可以簡單地將每個像素的 RGB 通道的最大值。
就像是:
rgb_copy = self.rgb.copy()
width, height, other = rgb_copy.shape
for i in range(0, width - 1):
for j in range(0, height - 1):
# find the maximum of the three values
max_value = max(self.rgb[i, j])
rgb_copy[i, j] = (max_value, max_value, max_value)
cv2.imshow('Value', rgb_copy)
cv2.waitKey()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/463210.html
上一篇:如何在單詞上繪制矩形?
