In this exercise, we will be using a basic image processing tools (a Machine Vision Toolbox for Python). Initially, we would suggest using given examples in the toolbox to get an understanding of the methods. These instructions are tested with python version 3.8.10 and machinevision-toolbox-python version 0.9.1. If you are using another version of python and getting errors, you can install version 3.8 simultaneously and work with specific version as mentioned below.
# Installation of the toolbox
pip install machinevision-toolbox-python
# specific version
pip install machinevision-toolbox-python==0.9.1
# if you have multiple python versions installed, change your PATH variable or use specific verion as below
# windows
py -3.8 -m pip install machinevision-toolbox-python==0.9.1
# Linux
python3.8 -m pip install machinevision-toolbox-python==0.9.1
# If output of the above command shows it can not install any dependency package then install it manually using pip command as above
Please see this folder for some images. You can select an image and run the commands on that image. In the code replace “image1.jpg” with the name of the image you are trying to work with. You are encouraged to first try out with these images and then collect your own images and try on them.
# Importing toolbox
from machinevisiontoolbox import Image
# Reading an image (your python code and images should be in same directory otherwise change the directory path in the below line)
import os
img1 = Image.Read(os.path.join(os.getcwd(),'image1.jpg'))
# Dsplaying image
img1.disp()
# defining kernel
import numpy as np
# identity (no change)
k = np.array([[0,0,0] ,[0,1,0], [0,0,0]])
img1.convolve(K=k).disp()
# other kernels to try
# box blur (average)
k = np.array([[1,1,1] ,[1,1,1], [1,1,1]])/9
# gaussian blur
k = np.array([[1,2,1] ,[2,4,2], [1,2,1]])/16
# sharp
k = np.array([[0,-1,0] ,[-1,5,-1], [0,-1,0]])
# sobel/edge (left)
k = np.array([[1,0,-1] ,[2,0,-2], [1,0,-1]])
imgm = Image.Read(os.path.join(os.getcwd(),'mainbuilding_1.jpeg'))
imgm_canny = imgm.canny()
h = imgm_canny.Hough()
lines = h.lines_p(100, minlinelength=100, maxlinegap=5, seed=0)
for line in lines:
imgm.draw_line((line[0], line[1]), (line[2], line[3]), color=[0,200,0])
imgm.disp()
Next, consider the task of matching images based on SIFT features. As a recap, SIFT features are scale and translation invariant and allow matching even if the object changes its size (scaling) or moves in the image (translation). In the following example, SIFT features are extracted from each of the images and then matched.
view1 = Image.Read(os.path.join(os.getcwd(),'mainbuilding_2.jpeg'), mono=True)
view2 = Image.Read(os.path.join(os.getcwd(),'mainbuilding_3.jpeg'), mono=True)
sf1 = view1.SIFT()
sf2 = view2.SIFT()
matches = sf1.match(sf2)
print(matches)
# display the correspondences found in table view
matches[1:5].table()
# display the correspondences found graphically
matches.subset(100).plot("w")