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

SIFT FOR IMAGES #1

Open
wants to merge 3 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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
## SIFT.py
- This code will calc the difference between the images and read all the images matrix one by one, the function findSimilarities will apply the sift method saving all images that dont repeat it self and discard the rest.

### Requirements
- Python 2.7
- pip install opencv-python==3.4.2.16
- pip install opencv-contrib-python==3.4.2.16
- pip install termcolor

### Optionals
- Change the variable **frame_start** to start the video in a specific frame.
- Change the variable **IMAGE_DIR** to point your video path.
- Change the variable **folderTosave** to point to folder that save the frames.
- Change the variable **thresholdDifference** to change the minimum threshold at compare two frames.

## differenceBetweenImages.py
- Calc the difference between two frames, and save the next frame that difference is less than a threshold

Expand Down
119 changes: 119 additions & 0 deletions SIFT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env python
# coding: utf-8

# In[1]:


#pip install opencv-python==3.4.2.16
#pip install opencv-contrib-python==3.4.2.16
#pip install termcolor


# In[1]:


from PIL import Image
import glob
import cv2
from termcolor import colored
from os.path import join


# In[3]:


IMAGE_DIR = join('zip10')
images = glob.glob(f'{IMAGE_DIR}/*.jpg')
folderTosave = "Tosave"
print(f'Found {len(images)} images')
thresholdDifference = 5
frame_start = -1


# In[5]:


def main():
print("Image Path: %s" % IMAGE_DIR)
for image in images:
img = Image.open(f'{image}')
width, height = img.size[:2]
width, height = int(width), int(height)

count = -1
origin = 0
frameOriginal = 0
############################
#Frame Size
yframeFrom = 0
yframeTo = height

xFrameFrom = 0
xFrameTo = width

for img in glob.glob(f'{IMAGE_DIR}/*.jpg'):
count+=1
cap = cv2.imread(img)
ret = cap
print("Frame: "+str(count))
if count == frame_start+1:
frameOriginal = cap[yframeFrom:yframeTo, xFrameFrom:xFrameTo]
pathToSave = join(folderTosave, "%s.jpg" % (count))
cv2.imwrite(pathToSave, frameOriginal)
else:
if not ret.any():
break
frameCompare = cap[yframeFrom:yframeTo,xFrameFrom:xFrameTo]
#cv2.imshow("Original", frameOriginal)
#cv2.imshow("Comparacao",frameCompare)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
try:
difference = findSimilarities(frameOriginal,frameCompare)
if(difference > thresholdDifference):
print(colored("Accepted",'green'))
pathToSave = join(folderTosave, "%s.jpg" % (count))
cv2.imwrite(pathToSave, frameCompare)
frameOriginal = frameCompare.copy()
else:
print("Diff expected, less than %s" % str(thresholdDifference))
print(colored("Denied",'red'))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print("\n")
except:
continue

def findSimilarities(original, compare):
sift = cv2.xfeatures2d.SIFT_create()
kp_1, desc_1 = sift.detectAndCompute(original, None)
kp_2, desc_2 = sift.detectAndCompute(compare, None)

index_params = dict(algorithm=0, trees=5)
search_params = dict()
flann = cv2.FlannBasedMatcher(index_params, search_params)

matches = flann.knnMatch(desc_1, desc_2, k=2)

good_points = []
ratio = 0.6
for m, n in matches:
if m.distance < ratio*n.distance:
good_points.append(m)
lGoodPoints = len(good_points)

#TO SEE THE FRAME COMPARED UNCOMMENT THE LINES BELOW
#result = cv2.drawMatches(original, kp_1, compare, kp_2, good_points, None)
#cv2.imshow("result", result)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
return lGoodPoints
if __name__ == '__main__':
main()


# In[ ]: