Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correction of spelling mistakes of "classfiy" to "classify" #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## 检测一个文件夹中的相似图片

```
bash test.sh folder
```

将每张图片和他之后的五张图片比较(ls默认顺序),相似的会输出两张图片名称。

test.py中修改使用的算法。具体算法见下。




[中文](Chinese.md)

Expand Down
16 changes: 9 additions & 7 deletions aHash.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from PIL import Image
from PIL import ImageFilter
from PIL import ImageOps
import sys

#This module can classfy the image by Average Hash Method
#The Hash Method is too strict,so this moudel suitable for finding image by Thumbnail
Expand Down Expand Up @@ -34,16 +35,16 @@ def compCode(code1,code2):
for index in range(0,len(code1)):
if code1[index] != code2[index]:
num+=1
return num
return num

def classfiy_aHash(image1,image2,size=(8,8),exact=25):
def classify_aHash(image1,image2,size=(8,8),exact=25):
''' 'image1' and 'image2' is a Image Object.
You can build it by 'Image.open(path)'.
'Size' is parameter what the image will resize to it and then image will be compared by the algorithm.
It's 8 * 8 when it default.
It's 8 * 8 when it default.
'exact' is parameter for limiting the Hamming code between 'image1' and 'image2',it's 25 when it default.
The result become strict when the exact become less.
This function return the true when the 'image1' and 'image2' are similar.
The result become strict when the exact become less.
This function return the true when the 'image1' and 'image2' are similar.
'''
image1 = image1.resize(size).convert('L').filter(ImageFilter.BLUR)
image1 = ImageOps.equalize(image1)
Expand All @@ -53,10 +54,11 @@ def classfiy_aHash(image1,image2,size=(8,8),exact=25):
code2 = getCode(image2, size)

assert len(code1) == len(code2),"error"

return compCode(code1, code2)<=exact



__all__=[classfiy_aHash]
__all__=[classify_aHash]


14 changes: 7 additions & 7 deletions dHash.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def getCode(img,size):
result = []
# print("x==",size[0])
# print("y==",size[1]-1)

x_size = size[0]-1#width
y_size = size[1] #high
for x in range(0,x_size):
Expand All @@ -34,15 +34,15 @@ def compCode(code1,code2):
for index in range(0,len(code1)):
if code1[index] != code2[index]:
num+=1
return num
return num

def classfiy_dHash(image1,image2,size=(9,8)):
def classify_dHash(image1,image2,size=(9,8)):
''' 'image1' and 'image2' is a Image Object.
You can build it by 'Image.open(path)'.
'Size' is parameter what the image will resize to it and then image will be compared to another image by the dHash.
It's 9 * 8 when it default.
It's 9 * 8 when it default.

The function will return the hamming code,less is correct.
The function will return the hamming code,less is correct.
'''
image1 = image1.resize(size).convert('L')
code1 = getCode(image1, size)
Expand All @@ -52,10 +52,10 @@ def classfiy_dHash(image1,image2,size=(9,8)):
code2 = getCode(image2, size)

assert len(code1) == len(code2),"error"

return compCode(code1, code2)



__all__=[classfiy_dHash]
__all__=[classify_dHash]

9 changes: 5 additions & 4 deletions histogram.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from PIL import Image
import sys

#This module can classify the image by histogram.
#This method is easy for someone who is a beginner in Image classification.
#
#author MashiMaroLjc
#version 2016-2-16

def classfiy_histogram(image1,image2,size = (256,256)):
def classify_histogram(image1,image2,size = (256,256)):
''' 'image1' and 'image2' is a Image Object.
You can build it by 'Image.open(path)'.
'Size' is parameter what the image will resize to it.It's 256 * 256 when it default.
'Size' is parameter what the image will resize to it.It's 256 * 256 when it default.
This function return the similarity rate betweene 'image1' and 'image2'
'''
image1 = image1.resize(size).convert("RGB")
Expand All @@ -27,7 +28,7 @@ def classfiy_histogram(image1,image2,size = (256,256)):
data.append(1 - abs(g[index] - s[index])/max(g[index],s[index]) )
else:
data.append(1)

return sum(data)/len(g)

return sum(data)/len(g)

__all__=[classify_histogram]
10 changes: 5 additions & 5 deletions histogram2.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def calculate(image1,image2):
data.append(1 - abs(g[index] - s[index])/max(g[index],s[index]) )
else:
data.append(1)

return sum(data)/len(g)


Expand All @@ -38,10 +38,10 @@ def split_imgae(image,part_size):

return sub_image_list

def classfiy_histogram_with_split(image1,image2,size = (256,256),part_size=(64,64)):
def classify_histogram_with_split(image1,image2,size = (256,256),part_size=(64,64)):
''' 'image1' and 'image2' is a Image Object.
You can build it by 'Image.open(path)'.
'Size' is parameter what the image will resize to it.It's 256 * 256 when it default.
'Size' is parameter what the image will resize to it.It's 256 * 256 when it default.
'part_size' is size of piece what the image will be divided.It's 64*64 when it default.
This function return the similarity rate betweene 'image1' and 'image2'
'''
Expand All @@ -59,7 +59,7 @@ def classfiy_histogram_with_split(image1,image2,size = (256,256),part_size=(64,6
y = size[1]/part_size[1]

pre = round((sub_data/(x*y) ),3 )
return pre
return pre

__all__ = [classfiy_histogram_with_split]
__all__ = [classify_histogram_with_split]

6 changes: 6 additions & 0 deletions result
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
similar/0.jpg similar/1.jpg
similar/0.jpg similar/2.jpg
similar/1.jpg similar/2.jpg
similar/3.jpg similar/4.jpg
similar/5.jpg similar/6.jpg
similar/7.jpg similar/8.jpg
Binary file added similar/0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added similar/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added similar/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added similar/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added similar/4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added similar/5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added similar/6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added similar/7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added similar/8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/python3

#================================================================
# Copyright (C) 2018 Sangfor Ltd. All rights reserved.
#
# filename:test.py
# author:ljx (jiaxinustc#gmail.com)
# date:2018.12.11
# description:传入两张图片,检测其相似与否
#
#================================================================
from PIL import Image
from aHash import classify_aHash
from dHash import classify_dHash
from pHash import classify_DCT
from histogram import classify_histogram
from histogram2 import classify_histogram_with_split
import sys


if __name__=="__main__":

#phash dhash ahash
#method="ahash"
#method="dhash"
#method="phash"
#method="histogram"
method="histogram2"

image1=Image.open(sys.argv[1])
image2=Image.open(sys.argv[2])
if method=="ahash":
re=classify_aHash(image1,image2)
if(re):
print(sys.argv[1],sys.argv[2])
elif method=="dhash":
re=classify_dHash(image1,image2)
if(re<30):
print(sys.argv[1],sys.argv[2])
elif method=="phash":
re=classify_DCT(image1,image2)
if(re<=10):
print(sys.argv[1],sys.argv[2])
elif method=="histogram":
re=classify_histogram(image1,image2)
if(re>0.8):
print(sys.argv[1],sys.argv[2])
elif method=="histogram2":
re=classify_histogram_with_split(image1,image2)
if(re>0.55):
print(sys.argv[1],sys.argv[2])
#print(re)
34 changes: 34 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

#================================================================
# Copyright (C) 2018 Sangfor Ltd. All rights reserved.
#
# filename:test.sh
# author:ljx (jiaxinustc#gmail.com)
# date:2018.12.11
# description:比较$1目录下所有图片,找出相似图片
#
#================================================================

#export PYTHONPATH=`pwd`
#图片数目
s=0

for file in $1/*
do
filelist[$s]=$file
#echo ${filelist[$i]}
((s++))
done

for((i=0;i<s;i++))
do
for((j=i+1;j<s;j++))
do
#echo ${filelist[$i]} ${filelist[$j]}
python3 test.py ${filelist[$i]} ${filelist[$j]}
done
done

#运行结果定向到result,然后将第二列移走即可,第二列都是相似图片。
#cat result| awk '{printf("mv %s s/\n",[)}''])}'