Skip to content

Commit 460b019

Browse files
committed
matching shapes
1 parent 0765566 commit 460b019

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

Matching shapes/ReadMe.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Matching shapes
2+
This python script shows the degree of matching of shapes.
3+
4+
## Setup Instructions
5+
### Install python3
6+
sudo apt-get install python3
7+
### Install pip (package installer for python)
8+
sudo apt-get install python3-pip
9+
### Install OpenCV library with pip
10+
pip3 install opencv-python
11+
### Install tkinter library
12+
sudo apt-get install python3-tk
13+
14+
## Details/Output
15+
A dialog box appears with option to select images 1 and 2. Then clicking proceed will display the degree of similarity of the shapes in the two selected images.
16+
17+
## Author
18+
Github: invigorzz313

Matching shapes/matching_shapes.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import cv2
2+
import tkinter as tk
3+
from tkinter.filedialog import *
4+
5+
window = tk.Tk()
6+
window.title("Matching shapes")
7+
window.geometry('350x200')
8+
9+
def image1(): # getting image 1
10+
photo1 = askopenfilename()
11+
global gray1
12+
img1 = cv2.imread(photo1)
13+
img1 = cv2.resize(img1,(500,500))
14+
gray1 = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)
15+
16+
17+
def image2(): # getting image 2
18+
photo2 = askopenfilename()
19+
global gray2
20+
img2 = cv2.imread(photo2)
21+
img2 = cv2.resize(img2,(500,500))
22+
gray2 = cv2.cvtColor(img2, cv2.COLOR_RGB2GRAY)
23+
24+
25+
def proceeds(): # detecting shape matching using contours
26+
ret, threshold = cv2.threshold(gray1, 127, 255, 0)
27+
ret, threshold2 = cv2.threshold(gray2, 127, 255, 0)
28+
contours, hierarchy, rem1 = cv2.findContours(threshold, 2, 1)
29+
cnt1 = contours[0]
30+
contours, hierarchy, rem2 = cv2.findContours(threshold2, 2, 1)
31+
cnt2 = contours[0]
32+
ret = cv2.matchShapes(cnt1, cnt2, 1, 0.0)
33+
print(ret)
34+
label = tk.Label(window, text="Probability of shapes matching: "+str(1-ret)).grid(row=4,column=1)
35+
36+
label = tk.Label(window, text="Image 1").grid(row=1, column=0)
37+
label = tk.Label(window, text="Image 2").grid(row=2, column=0)
38+
39+
b1 = tk.Button(window, text='choose image 1', command=image1)
40+
b2 = tk.Button(window, text='choose image 2', command=image2)
41+
proceed = tk.Button(window, text='Proceed', command=proceeds)
42+
43+
b1.grid(row=1,column=1)
44+
b2.grid(row=2,column=1)
45+
proceed.grid(row=3,column=1)
46+
47+
window.mainloop()
48+
cv2.destroyAllWindows()

Matching shapes/star1.png

1.57 KB
Loading

Matching shapes/star2.png

3.82 KB
Loading

0 commit comments

Comments
 (0)