怎么在python中使用opencv对目录中的图片去重

  介绍

怎么在python中使用opencv对目录中的图片去重?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

<强>版本:

平台:ubuntu 14/I5/4 g内存

python版本:python2.7

opencv版本:2.13.4

<强>依赖:

如果系统没有python,则需要进行安装

sudo apt-get安装python

sudo apt-get安装python-dev

sudo apt-get安装python-pip

sudo pip安装numpy mathplotlib

sudo apt-get安装libcv-dev

sudo apt-get安装python-opencv

使用感知哈希算法进行图片去重

原理:对每个文件进行遍历所有进行去重,因此图片越多速度越慢,但是可以节省手动操作

<强>感知哈希原理:

1,需要比较的图片都缩放成8 * 8大小的灰度图

2,获得每个图片每个像素与平均值的比较,得到指纹

3,根据指纹计算汉明距离

5,如果得出的不同的元素小于5则为相同(相似?)的图片

# !/usr/bin/python   #,- *安康;编码:UTF-8  - * -   ,   import  cv2   import  numpy  as  np   import 操作系统、系统类型 def  cmpandremove2(路径):=,,dirs  os.listdir(路径)   ,dirs.sort ()   ,if  len (dirs), & lt;=, 0:   ,返回   ,dict={}   ,for 小姐:dirs拷贝:   prepath 才能=,path  +,“/?,我   时间=preimg 才能;cv2.imread (prepath)   if 才能;类型(preimg), is  types.NoneType:   ,才能继续   preresize 才能=,cv2.resize (preimg,, (8))   时间=pregray 才能;cv2.cvtColor (preresize, cv2.COLOR_BGR2GRAY)   时间=premean 才能;cv2.mean (pregray) [0]   时间=prearr 才能;np.array (pregray.data)   for 才能;j 拷贝范围(0,len (prearr)):   ,,if  prearr [j],祝辞=,premean:   ,,,prearr [j],=, 1   ,,:   ,,,prearr [j],=0   print 才能“get", prepath   dict[我],才能=prearr=,,dictkeys  dict.keys ()   ,dictkeys.sort ()   index =, 0   ,while 真正的:   if 才能;index 祝辞=,len (dictkeys):   ,才能休息   时间=curkey 才能;dictkeys(指数)   dellist才能=[]   print  curkey才能   时间=index2 才能;指数   while 才能正确的:   ,,if  index2 祝辞=,len (dictkeys):   ,才能打破   ,,j =, dictkeys [index2]   ,,if  curkey ==,珍:   ,,,index2 =, index2  + 1   ,才能继续   ,,arr1 =, dict [curkey]   ,,arr2 =, dict [j]   ,,diff =0   ,,for  k 拷贝范围(0,len (arr2)):   ,,,if  arr1 [k], !=, arr2 [k]:   ,,,,diff =, diff  + 1   ,,if  diff  & lt;=, 5:   ,,,dellist.append (j)   ,,index2 =, index2  + 1   if 才能len (dellist),祝辞,0:   ,,for  j  dellist:拷贝   ,,,file =, path  +,“/?, j   ,,,print “remove",文件   ,,,os.remove(文件)   ,,,dict.pop (j)   ,,dictkeys =, dict.keys ()   ,,dictkeys.sort ()   index 才能=,index  +, 1 def  cmpandremove(路径):   index =, 0   flag =, 0=,,dirs  os.listdir(路径)   ,dirs.sort ()   ,if  len (dirs), & lt;=, 0:   ,return  0   ,while 真正的:   if 才能;index 祝辞=,len (dirs):   ,才能休息   时间=prepath 才能;path  +, dirs(指数)   print  prepath才能   index2 才能=0   时间=preimg 才能;cv2.imread (prepath)   if 才能;类型(preimg), is  types.NoneType:   ,,index =, index  + 1   ,才能继续   preresize 才能=,cv2.resize (preimg (8、8))   时间=pregray 才能;cv2.cvtColor (preresize, cv2.COLOR_BGR2GRAY)   时间=premean 才能;cv2.mean (pregray) [0]   时间=prearr 才能;np.array (pregray.data)   for 才能小姐:拷贝范围(0,len (prearr)):   ,,if  prearr[我],祝辞=,premean:   ,,,prearr[我],=,1   ,,:   ,,,prearr[我],=0   时间=removepath 才能;[]   while 才能正确的:   ,,if  index2 祝辞=,len (dirs):   ,才能打破   ,,if  index2  !=,指数:   ,,,curpath =, path  +, dirs [index2]   ,,# print  curpath   ,,,curimg =, cv2.imread (curpath)   ,,,if 类型(curimg), is  types.NoneType:   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在python中使用opencv对目录中的图片去重