Template matching technique using opencv
Starship Detection using opencv template matching
Setting up Environment¶
Downloading Dataset¶
So we will first need to download the python library by AIcrowd that will allow us to download the dataset by just inputting the API key.
In [1]:
!pip install aicrowd-cli
%load_ext aicrowd.magic
In [2]:
%aicrowd login
In [3]:
# Downloading the Dataset
!rm -rf data
!mkdir data
%aicrowd ds dl -c starship-detection -o data
In [4]:
!unzip data/data.zip -d /content/images >> /dev/null
Importing Libraries¶
In [5]:
# Image Reading & Preprocessing
from PIL import Image, ImageDraw
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Misc.
import pandas as pd
from tqdm.notebook import tqdm
import os
from natsort import natsorted
Image Preprocessing¶
In this section we are going to learn template matching opencv function which can help us detecting the starship body!
In [6]:
from google.colab.patches import cv2_imshow
img = cv2.imread("images/2.jpg",0)
cv2_imshow(img)
In [7]:
cv2_imshow(cv2.rectangle(img.copy(), (245,210), (290,320),(255, 0, 0),2))
In [8]:
template = img[210:320,245:290]
Template matching with threshold value of 95% similarity
In [28]:
#refer this link for other methods availabl
w,h = template.shape[::-1]
res = cv2.matchTemplate(img.copy(),template,eval('cv2.TM_CCOEFF_NORMED'))
#print(res)
loc = np.where( res >= 0.95)
print(len(list(zip(*loc[::-1]))))
for pt in zip(*loc[::-1]):
result = cv2.rectangle(img.copy(), pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv2_imshow(result)
For the instances where is the starship is in horizontal postion we need to take another template for the same.
In [ ]:
Content
Comments
You must login before you can post a comment.