2017-05-06 53 views
0

的元素我有它的大小被存儲爲元組的列表中的一些圖像小得多,如下:從列表中刪除元素它大得多或比大多數列表

(79, 1100) 
(51, 1100) 
(56, 1100) 
(49, 1100) 
(47, 1100) 
(44, 1100) 
(54, 1100) 
(15,1100) 
(52, 1100) 
(115, 1100) 
(44, 1100) 
(60, 1100) 
(51, 1100) 
(56, 1100) 
(19,1100) 
(110,1100) 

我想是刪除所有第一維尺寸比這個列表中的大多數元素小得多(在本例中爲15,115,19,110)的圖像。在這裏,我無法定義閾值,因爲數字的範圍(第一維)可能會根據我選擇的圖像的比例而發生很大的變化。

+0

如果不設置一個「很多」的因子,你無法真正定義'很多'。如果你想完成這個任務,你可能只需要使用與平均值相差最大的百分比。 –

回答

0

您可以使用圖像尺寸的均值和標準偏差來構建閾值。 讓我們使用numpy的計算這些指標:

import numpy as np 

#convert the list of tuples l_tuples to a numpy array: 
l_new = np.array(l_tuples) 

#calculate the mean and standard deviation of the first column 
mean=np.mean(l_new,axis=0)[0] 
std=np.std(l_new,axis=0)[0] 

#create a mask to find all elements which are in the interval [mean-std, mean+std] 
mask=[abs(x[0]-mean)<std for x in l_new] 

#pictures you want to use: 
accepted = l_new[mask] 
#pictures you dont want to use: 
remove = l_new[[not i for i in mask]] 

如果你覺得一個標準差是不適合你一個很好的估計,你可以很容易地與不同的東西交換這個指標,如平均或任何的固定百分比。