|
| 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() |
0 commit comments