簡體   English   中英

如何從另一個 RGB 列表中找到一個 RGB 列表的最近像素點索引?

[英]How to find the closest pixel points indexes of one RGB list from another RGB list?

我一直在為一些圖像處理任務工作。 我在下面提供了我想做的所有細節。

我打算做什么:

我有兩個列表

colorlist = 大約 500 rgb 值(來自我已有的圖片 1)

顏色= 大約 1200 或更多 RGB 值(來自我也有的圖片 2)

注意:上面的列表可以改變它們的大小,比如colorlist有時可以比color

現在我想找到顏色的所有元素以在colorlist中獲取其最接近的像素值,然后以列表的形式返回最接近的像素值 [假設list_1 ],最后返回該列表的索引 [ list_1 ] 來自圖片 2 整個圖像 RGB列表。

例如:

picture2 = 
[[ 59 218 186]
 [161 144 250]
 [133 165 249]
 [135 125 125]
 ...
 [59  58 145]
 [153  144 155]
 [ 61 225  87]
 [153 149  63]]

colorlist = 

[[ 59 218 186]
 [161 144 250]
 [133 165 249]
 ...
 [155  58 184]
 [ 61 225  87]
 [153 149  63]]

color = 

[[201 224 194]
 [121  93 158]
 [ 99 143 153]
 ...
 [193  67 171]
 [ 80  49  94]
 [125 121 219]]


#Suppose these two values are closest found  in color array from colorlist
list1 = [(161, 144, 250), (133, 165, 249) ] 



index = [2 , 3] #Found from picture2 whole image array

###the index here is assumed for demonstration it can be larger or different from colorlist





我嘗試了什么:

方法一:使用numpy linalg范數

def closest_points(color,colorlist):
  for x in range (0, len(color):
    dist = np.linalg.norm(colorlist - color[x], axis=1)
    tmp = colorlist[np.argmin(dist)]
    list1.append(tmp)
  return list1

錯誤拋出:與方法 2 相同(維度錯誤)

方法二:使用Scipy KDTree

def closest_points(color,colorlist):
  for x in range (0, len(color):
    tree = sp.KDTree(colorlist)
    tmp = colorlist[tree.query(color[x])[1]]
    list1.append(tmp)
  return list1

錯誤拋出:

super().__init__(data, leafsize, compact_nodes, copy_data,
File "_ckdtree.pyx", line 560, in scipy.spatial._ckdtree.cKDTree.__init__
ValueError: data must be 2 dimensions


方法三:

def closest_points(rgb,newlist):
    r, g, b = rgb
    color_diffs = []
    for x in newlist:
        cr, cg, cb = x
        color_diff = math.sqrt((r - cr)**2 + (g - cg)**2 + (b - cb)**2)
        color_diffs.append((color_diff, x))
    return min(color_diffs)[1]

for x in range (0, len(color)):
    
    tmp = closest_color(color[x],colorlist)
    list1.append(tmp)

錯誤拋出:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

希望我的問題很清楚。 謝謝您的幫助 !

我自己找到了解決方案。 在方法 2 中,無需迭代。

def closest_points(color,colorlist): 
  tree = sp.KDTree(colorlist)
  tmp = colorlist[tree.query(color)[1]]
return tmp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM