Skip to content

Commit 57cdf9e

Browse files
Priyanka VermaPriyanka Verma
Priyanka Verma
authored and
Priyanka Verma
committed
TemplateMatching scripts
1 parent eafe15a commit 57cdf9e

18 files changed

+613
-0
lines changed

TemplateMatching/tm10.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import numpy as np
2+
import cv2
3+
4+
# gives an output
5+
cap = cv2.VideoCapture('1ph meter video.mp4')
6+
7+
#Set frame_no in range 0.0-1.0
8+
#In this example we have a video of 30 seconds having 25 frames per seconds, thus we have 750 frames.
9+
#The examined frame must get a value from 0 to 749.
10+
#For more info about the video flags see here: https://stackoverflow.com/questions/11420748/setting-camera-parameters-in-opencv-python
11+
#Here we select the last frame as frame sequence=749. In case you want to select other frame change value 749.
12+
#BE CAREFUL! Each video has different time length and frame rate.
13+
#So make sure that you have the right parameters for the right video!
14+
# time_length = 164
15+
# fps=25
16+
# frame_seq = 749
17+
# frame_no = (frame_seq /(time_length*fps))
18+
19+
#The first argument of cap.set(), number 2 defines that parameter for setting the frame selection.
20+
#Number 2 defines flag CV_CAP_PROP_POS_FRAMES which is a 0-based index of the frame to be decoded/captured next.
21+
#The second argument defines the frame number in range 0.0-1.0
22+
cap.set(1,2900);
23+
24+
#Read the next frame from the video. If you set frame 749 above then the code will return the last frame.
25+
ret, frame = cap.read()
26+
27+
#Set grayscale colorspace for the frame.
28+
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
29+
30+
#Cut the video extension to have the name of the video
31+
my_video_name = "1ph meter video"
32+
33+
#Display the resulting frame
34+
cv2.imshow(my_video_name+ 'frame' + str(2900),frame)
35+
36+
#Set waitKey
37+
cv2.waitKey()
38+
39+
#Store this frame to an image
40+
cv2.imwrite(my_video_name+'frame'+str(2900)+'.jpg',frame)
41+
42+
# When everything done, release the capture
43+
cap.release()
44+
cv2.destroyAllWindows()
File renamed without changes.

TemplateMatching/tm11.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import cv2
2+
import numpy as np
3+
4+
5+
voltage_temp = cv2.imread('C:/Users/Win10/Documents/4th Semester Summer/GUVNL/v_bin.png')
6+
ampere_temp = cv2.imread('C:/Users/Win10/Documents/4th Semester Summer/GUVNL/a_bin.png')
7+
kW_temp = cv2.imread('C:/Users/Win10/Documents/4th Semester Summer/GUVNL/kW_bin.png')
8+
kWh_temp = cv2.imread('C:/Users/Win10/Documents/4th Semester Summer/GUVNL/kWh_bin.png')
9+
voltage_temp = cv2.cvtColor(voltage_temp,cv2.COLOR_BGR2GRAY)
10+
ampere_temp = cv2.cvtColor(ampere_temp,cv2.COLOR_BGR2GRAY)
11+
kW_temp = cv2.cvtColor(kW_temp,cv2.COLOR_BGR2GRAY)
12+
kWh_temp = cv2.cvtColor(kWh_temp,cv2.COLOR_BGR2GRAY)
13+
14+
V_max = np.max(cv2.matchTemplate(screen1_gray,voltage_temp,cv2.TM_CCOEFF_NORMED))
15+
A_max = np.max(cv2.matchTemplate(screen1_gray,ampere_temp,cv2.TM_CCOEFF_NORMED))
16+
kW_max = np.max(cv2.matchTemplate(screen1_gray,kW_temp,cv2.TM_CCOEFF_NORMED))
17+
kWh_max = np.max(cv2.matchTemplate(screen1_gray,kWh_temp,cv2.TM_CCOEFF_NORMED))
18+
print"V = ", V_max
19+
print"A = ", A_max
20+
print "kW =", kW_max
21+
print "kWh=", kWh_max
22+
23+
threshold = 0.9
24+
maxval = 0
25+
if(V_max>maxval):
26+
symbol = "V"
27+
maxval = V_max
28+
if(A_max>maxval):
29+
symbol = "A"
30+
maxval = A_max
31+
if(kW_max>maxval):
32+
symbol = "kW"
33+
maxval = kW_max
34+
if(kWh_max>maxval):
35+
symbol = "kWh"
36+
maxval = kWh_max
37+
if(maxval<threshold):
38+
symbol = ""

TemplateMatching/tm12.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import cv2
2+
import math
3+
import numpy as np
4+
5+
videoFile = '1ph meter video.mp4'
6+
vidcap = cv2.VideoCapture(videoFile)
7+
success,image = vidcap.read()
8+
9+
#################### Setting up parameters ################
10+
11+
seconds = 5
12+
fps = vidcap.get(cv2.cv.CAP_PROP_FPS) # Gets the frames per second
13+
14+
multiplier = fps * seconds
15+
16+
#################### Initiate Process ################
17+
18+
while success:
19+
frameId = int(round(vidcap.get(1))) #current frame number, rounded b/c sometimes you get frame intervals which aren't integers...this adds a little imprecision but is likely good enough
20+
success, image = vidcap.read()
21+
22+
if frameId % multiplier == 0:
23+
cv2.imwrite("FolderSeconds/frame%d.jpg" % frameId, image)
24+
25+
vidcap.release()
26+
print "Complete"

TemplateMatching/tm2.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import cv2
2+
import numpy as np
3+
import glob
4+
5+
# multiple templates
6+
7+
#empty list to store template images
8+
X_data = []
9+
#make a list of all template images from a directory
10+
files1= glob.glob('*.png')
11+
12+
for myfile in files1:
13+
image = cv2.imread(myfile,0)
14+
X_data.append (image)
15+
cv2.imshow('image',image)
16+
cv2.waitKey(0)
17+
18+
print('X_data shape:', np.array(X_data).shape)
19+
20+
# test_image=cv2.imread('you\\testimage\\testimage.png')
21+
# test_image= cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY)
22+
23+
# #loop for matching
24+
# for tmp in template_data:
25+
# (tH, tW) = tmp.shape[:2]
26+
# cv2.imshow("Template", tmp)
27+
# cv2.waitKey(1000)
28+
# cv2.destroyAllWindows()
29+
# result = cv2.matchTemplate(test_image, tmp, cv2.TM_CCOEFF)
30+
# min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
31+
# top_left = max_loc
32+
# bottom_right = (top_left[0] + tW, top_left[1] + tH)
33+
# cv2.rectangle(test_image,top_left, bottom_right,255, 2)
34+
35+
# cv2.imshow('Result',test_image)
36+
# cv2.waitKey(0)

TemplateMatching/tm3.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import cv2
2+
import numpy as np
3+
4+
5+
cap = cv2.VideoCapture('electric meter 2.mp4')
6+
# template = cv2.imread('kwh temp 3.png',0)
7+
# w, h = template.shape[::-1]
8+
while(cap.isOpened()):
9+
10+
_, frame = cap.read()
11+
# hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
12+
# vid_gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
13+
14+
# res = cv2.matchTemplate(vid_gray,template,cv2.TM_CCOEFF_NORMED)
15+
cv2.imshow('frame',frame)
16+
k = cv2.waitKey(5) & 0xFF
17+
if k == 27:
18+
break
19+
# threshold = 0.92
20+
# loc = np.where( res >= threshold)
21+
# for pt in zip(*loc[::-1]):
22+
# cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 1)
23+
# cv2.imshow('mask',mask)
24+
# cv2.imshow('res',res)
25+
cv2.destroyAllWindows()
26+
cap.release()

TemplateMatching/tm4.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import cv2
2+
import numpy as np
3+
import glob
4+
import imutils
5+
6+
cap = cv2.VideoCapture('1ph meter video.mp4')
7+
template = cv2.imread('kw temp2.png',0)
8+
w, h = template.shape[::-1]
9+
10+
while(cap.isOpened()):
11+
ret, frame = cap.read()
12+
13+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
14+
15+
res = cv2.matchTemplate(gray,template,cv2.TM_CCOEFF_NORMED)
16+
threshold = 0.85
17+
18+
loc = np.where( res >= threshold)
19+
20+
for pt in zip(*loc[::-1]):
21+
cv2.rectangle(frame, pt, (pt[0] + w, pt[1] + h), (0,255,255), 1)
22+
cv2.imshow('frames',frame)
23+
cv2.imshow('temp',template)
24+
# cv2.waitKey(0)
25+
# if cv2.waitKey(5) & 0xFF == ord('q'): // doesn't exit with escape key
26+
# break //
27+
k = cv2.waitKey(1) & 0xFF
28+
if k == 27:
29+
break
30+
31+
cap.release()
32+
cv2.destroyAllWindows()

TemplateMatching/tm5.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import cv2
2+
import numpy as np
3+
import glob
4+
import imutils
5+
# Read the main image
6+
img_rgb = cv2.imread('kwh i3.jpg')
7+
8+
# Convert to grayscale
9+
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
10+
# Read the templatex
11+
template = cv2.imread('kw temp2.png',0)
12+
13+
# Store width and heigth of template in w and h
14+
w, h = template.shape[::-1]
15+
found = None
16+
17+
for scale in np.linspace(1, .05, 30)[::-1]:
18+
19+
# resize the image according to the scale, and keep track
20+
# of the ratio of the resizing
21+
resized = imutils.resize(template, width = int(template.shape[1] * scale))
22+
r = template.shape[1] / float(resized.shape[1])
23+
24+
# if the resized image is smaller than the template, then break
25+
# from the loop
26+
# detect edges in the resized, grayscale image and apply template
27+
# matching to find the template in the image
28+
edged = cv2.Canny(img_gray, 50, 200)
29+
result = cv2.matchTemplate(template,img_gray, cv2.TM_CCOEFF)
30+
threshold = 0.85
31+
(_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)
32+
# if we have found a new maximum correlation value, then update
33+
# the found variable if found is None or maxVal > found[0]:
34+
# if resized.shape[0] < h or resized.shape[1] < w:
35+
# break
36+
found = (maxVal, maxLoc, r)
37+
# //no threshold value ? ?
38+
# unpack the found varaible and compute the (x, y) coordinates
39+
# of the bounding box based on the resized ratio
40+
(_, maxLoc, r) = found
41+
(startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
42+
(endX, endY) = (int((maxLoc[0] + w) * r), int((maxLoc[1] + h) * r))
43+
44+
# draw a bounding box around the detected result and display the image
45+
cv2.rectangle(img_rgb, (startX, startY), (endX, endY), (0, 0, 255), 2)
46+
cv2.imshow('img_rgb',img_rgb)
47+
cv2.imshow('template',template)
48+
cv2.waitKey(0)

TemplateMatching/tm6.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import cv2
2+
import numpy as np
3+
import glob
4+
import imutils
5+
6+
# Read the main image
7+
img_rgb = cv2.imread('kwh i3.jpg')
8+
9+
# Convert to grayscale
10+
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
11+
12+
# Read the templatex
13+
template = cv2.imread('kw temp2.png',0)
14+
15+
# Store width and heigth of template in w and h
16+
w, h = template.shape[::-1]
17+
found = None
18+
19+
for scale in np.linspace(.3, 1, 5)[::-1]:
20+
21+
# resize the image according to the scale, and keep track
22+
# of the ratio of the resizing
23+
resized = imutils.resize(template, width = int(template.shape[1] * scale))
24+
r = template.shape[1] / float(resized.shape[1])
25+
cv2.imshow('resized',resized)
26+
# if the resized image is smaller than the template, then break
27+
# from the loop
28+
# detect edges in the resized, grayscale image and apply template
29+
# matching to find the template in the image
30+
edged = cv2.Canny(img_gray, 50, 200)
31+
result = cv2.matchTemplate(template,img_gray, cv2.TM_CCOEFF)
32+
threshold = 0.85
33+
(_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)
34+
# if we have found a new maximum correlation value, then update
35+
# the found variable if found is None or maxVal > found[0]:
36+
# if resized.shape[0] < h or resized.shape[1] < w:
37+
# break
38+
found = (maxVal, maxLoc, r)
39+
# //no threshold value ? ?
40+
# unpack the found varaible and compute the (x, y) coordinates
41+
# of the bounding box based on the resized ratio
42+
(_, maxLoc, r) = found
43+
(startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
44+
(endX, endY) = (int((maxLoc[0] + w) * r), int((maxLoc[1] + h) * r))
45+
46+
# draw a bounding box around the detected result and display the image
47+
cv2.rectangle(img_rgb, (startX, startY), (endX, endY), (0, 0, 255), 2)
48+
cv2.imshow('img_rgb',img_rgb)
49+
cv2.imshow('template',template)
50+
cv2.waitKey(0)

TemplateMatching/tm7.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import cv2
2+
import numpy as np
3+
import glob
4+
import imutils
5+
6+
#kwh
7+
# detects kW for V template @ .85 and @.9
8+
#runs slow at .91 onwards , detects only V frame : ask mayur to check
9+
cap = cv2.VideoCapture('1phmetervideo.mp4')
10+
template = cv2.imread("3vtemp2.png",0)
11+
w, h = template.shape[::-1]
12+
13+
# for scale in np.linspace(.35, 1, 5)[::-1]:
14+
resized = imutils.resize(template, width = int(template.shape[1] * .35))
15+
r = template.shape[1] / float(resized.shape[1])
16+
17+
while(cap.isOpened()):
18+
ret, frame = cap.read()
19+
20+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
21+
res = cv2.matchTemplate(gray,resized,cv2.TM_CCOEFF_NORMED)
22+
threshold = 0.85
23+
24+
loc = np.where( res >= threshold)
25+
for pt in zip(*loc[::-1]):
26+
cv2.rectangle(frame, pt, (pt[0] + int(w*0.35), pt[1] + int(h*0.35)), (0,255,255), 1)
27+
cv2.imshow('frame',frame)
28+
cv2.imshow('resized',resized)
29+
cv2.imshow('template',template)
30+
31+
k = cv2.waitKey(1) & 0xFF
32+
if k == 27:
33+
break
34+
35+
cap.release()
36+
cv2.destroyAllWindows()

TemplateMatching/tm7b.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import cv2
2+
import numpy as np
3+
4+
5+
cap= cap = cv2.VideoCapture('1ph meter video.mp4')
6+
7+
while True:
8+
ret, frame = cap.read()
9+
blurred_frame = cv2.GaussianBlur(frame, (5, 5), 0)
10+
hsv = cv2.cvtColor(blurred_frame, cv2.COLOR_BGR2HSV)
11+
12+
lower_green = np.array([50, 155, 155])
13+
upper_green = np.array([70, 255, 255])
14+
mask = cv2.inRange(hsv, lower_green, upper_green)
15+
16+
contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
17+
18+
#for contour in contours:
19+
#area = cv2.contourArea(contour)
20+
21+
# if area > 5000:
22+
#cv2.drawContours(frame, contours, -1, (0, 0, 255), 3)
23+
24+
25+
#find the biggest area
26+
c = max(contours, key = cv2.contourArea)
27+
28+
x,y,w,h = cv2.boundingRect(c)
29+
# draw the reading area contour (in blue)
30+
im=cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
31+
32+
roi=im[y:y+h,x:x+w]
33+
34+
35+
cv2.imshow("Frame", frame)
36+
# cv2.imshow("Mask", mask)
37+
cv2.imshow("Result",roi)
38+
39+
key = cv2.waitKey(1)
40+
if key == 27:
41+
break
42+
43+
cap.release()
44+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)