Skip to content

Commit e857079

Browse files
committed
object detection - template matching
1 parent c0b68a7 commit e857079

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

Object detection in Image/ReadMe.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Object detection - Template Matching
2+
This python script will allow us to identify objects in an image.
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 Numpy library with pip
10+
pip3 install numpy
11+
### Install OpenCV library with pip
12+
pip3 install opencv-python
13+
### Install tkinter library
14+
sudo apt-get install python3-tk
15+
16+
## Details/Output
17+
User is prompted to select two images -
18+
1. the image in which we wish to detect objects
19+
2. the template/image of object which is to be identified in the original image.
20+
(**Note** The size of the image is larger than that of template)
21+
22+
The output image is written/stored in the current folder.
23+
24+
## Author
25+
Github: invigorzz313
10.6 KB
Loading
44.9 KB
Loading
3.36 KB
Loading
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import cv2
2+
import numpy as np
3+
import tkinter as tk
4+
from tkinter.filedialog import *
5+
6+
7+
window = tk.Tk()
8+
window.title("Object finding")
9+
window.geometry('400x100')
10+
label = tk.Label(window, text="Choose an image first and then the object to be found in it").grid(row=0, column=0)
11+
label = tk.Label(window, text="Output is stored in this folder").grid(row=1, column=0)
12+
13+
photo = askopenfilename()
14+
img = cv2.imread(photo)
15+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
16+
17+
temp = askopenfilename()
18+
template = cv2.imread(temp, cv2.IMREAD_GRAYSCALE)
19+
w,h = template.shape[::-1]
20+
21+
result = cv2.matchTemplate(gray, template, cv2.TM_CCOEFF_NORMED)
22+
threshold = 0.8
23+
loc = np.where(result>=threshold)
24+
for pt in zip(*loc[::-1]):
25+
cv2.rectangle(img,pt,(pt[0]+w, pt[1]+h), (0,0,255), 1)
26+
27+
cv2.imshow("output", img)
28+
cv2.imwrite('output.png', img)
29+
cv2.waitKey(0)
30+
cv2.destroyAllWindows()
31+
window.mainloop()

0 commit comments

Comments
 (0)