vertopal.com_26_04_25_Image_Processing_lab (2)
vertopal.com_26_04_25_Image_Processing_lab (2)
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('tank.bmp',0)
org_img = image.copy()
# Harris corner detection parameters
plt.subplot(1,2,1)
plt.title('Original Image')
plt.imshow(org_img, cmap='gray')
plt.axis(False)
plt.subplot(1,2,2)
plt.title('Harris Corner Detection')
plt.imshow(image, cmap='gray')
plt.axis(False)
plt.tight_layout()
plt.show()
• SIFT is one of the important algorithms that detect objects irrelevant to the scale
and rotation of the image and the reference.
• This helps a lot while we are comparing the real-world objects to an image though it
is independent of the angle and scale of the image.
• This method will return the key points of the images which we need to mark in the
image.
import cv2
import numpy as np
import matplotlib.pyplot as plt
sift = cv2.SIFT_create()
plt.subplot(1,2,1)
plt.title('Original Image')
plt.imshow(image)
plt.axis(False)
plt.subplot(1,2,2)
plt.title('SIFT Keypoints')
plt.imshow(image_with_keypoints)
plt.axis(False)
plt.tight_layout()
plt.show()
speeded up robust features (SURF)
• In computer vision, speeded up robust features (SURF) is a patented local feature
detector and descriptor. It can be used for tasks such as object recognition, image
registration, classification, or 3D reconstruction.
• The standard version of SURF is several times faster than SIFT and claimed by its
authors to be more robust against different image transformations than SIFT.
import cv2
import matplotlib.pyplot as plt
# Load an image
image = cv2.imread('tank.bmp', cv2.IMREAD_COLOR)
plt.subplot(1,2,2)
plt.title('SURF Keypoints')
plt.imshow(image_with_keypoints)
plt.axis(False)
plt.tight_layout()
plt.show()
----------------------------------------------------------------------
-----
error Traceback (most recent call
last)
<ipython-input-25-af8db48d390c> in <cell line: 0>()
6
7 # Create a SURF object
----> 8 surf = cv2.xfeatures2d.SURF_create()
9
10 # Detect and compute SURF keypoints and descriptors
error: OpenCV(4.11.0)
/io/opencv_contrib/modules/xfeatures2d/src/surf.cpp:1026: error: (-
213:The function/feature is not implemented) This algorithm is
patented and is excluded in this configuration; Set
OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function
'create'
Hessian operator
• The Hessian operator is a mathematical tool used in image processing and computer
vision for analyzing the second-order spatial derivatives of an image.
• In the context of feature detection, the Hessian matrix is used to compute the
Hessian determinant, which is a measure of local structure and can be used for
features like blobs or regions of interest.
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load the image
image = cv2.imread('tank.bmp', 0)
# Image
plt.subplot(2, 4, 1)
plt.imshow(image.astype(np.uint8), cmap='gray')
plt.title('Image')
plt.axis('off')
# Ix
plt.subplot(2, 4, 2)
plt.imshow(Ix, cmap='gray')
plt.title('Ix')
plt.axis('off')
# Iy
plt.subplot(2, 4, 3)
plt.imshow(Iy, cmap='gray')
plt.title('Iy')
plt.axis('off')
# Ixx
plt.subplot(2, 4, 4)
plt.imshow(Ixx, cmap='gray')
plt.title('Ixx')
plt.axis('off')
# Iyy
plt.subplot(2, 4, 5)
plt.imshow(Iyy, cmap='gray')
plt.title('Iyy')
plt.axis('off')
# Ixy
plt.subplot(2, 4, 6)
plt.imshow(Ixy, cmap='gray')
plt.title('Ixy')
plt.axis('off')
# Hessian Determinant
plt.subplot(2, 4, 7)
plt.imshow(hessian_det.astype(np.uint8), cmap='gray')
plt.title('Hessian Determinant')
plt.axis('off')
plt.tight_layout()
plt.show()
FAST(Features from Accelerated Segment Test)
• SURF is fast when compared to SIFT but not as fast to use with real-time devices like
mobile phones and surveillance cameras.
• However FAST gives us only the key points and we may need to compute
descriptors with other algorithms like SIFT and SURF.
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
# Print parameters
print("Threshold: {}".format(fast.getThreshold()))
print("nonmaxSuppression: {}".format(fast.getNonmaxSuppression()))
print("neighborhood: {}".format(fast.getType()))
print("Total Keypoints with nonmaxSuppression: {}".format(len(kp1)))
plt.subplot(1, 3, 1)
plt.imshow(img, cmap='gray')
plt.title('Orignal')
plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(img_with_kp1, cmap='gray')
plt.title('With nonmaxSuppression')
plt.axis('off')
plt.subplot(1,3,3)
plt.imshow(img_with_kp2, cmap='gray')
plt.title('Without nonmaxSuppression')
plt.axis('off')
plt.tight_layout()
plt.show()
Threshold: 10
nonmaxSuppression: True
neighborhood: 2
Total Keypoints with nonmaxSuppression: 7966
Total Keypoints without nonmaxSuppression: 20360
• BRIEF easily outperforms other fast descriptors such as SURF and SIFT in terms of
speed and terms of recognition rate in many cases.
import cv2
import numpy as np
from matplotlib import pyplot as plt
plt.subplot(1,2,1)
plt.title('Original Image')
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.subplot(1,2,2)
plt.imshow(image_with_keypoints)
plt.title('FAST + BRIEF Keypoints')
plt.axis('off')
plt.show()
ORB (Oriented FAST and Rotated Brief)
• builds upon the FAST keypoint detector and the BRIEF descriptor, adding
enhancements to make it more robust to rotation and scale changes.
import cv2
import matplotlib.pyplot as plt
image = cv2.imread('tank.bmp')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.subplot(1,2,2)
plt.imshow(kp_image_rgb)
plt.axis('off')
plt.title('ORB Keypoints')
plt.show()
• The HOG method involves computing the gradient magnitude and orientation for
each pixel in an image and then dividing the image into small cells.
• It is better than any edge descriptor as it uses magnitude as well as angle of the
gradient to compute the features.
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage.feature import hog
from skimage import exposure
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(hog_image_rescaled, cmap='gray')
plt.title('HOG Visualization')
plt.axis('off')
plt.tight_layout()
plt.show()
Feature Matching
• It computes the distances between descriptors and finds the best matches based on
a chosen criterion (e.g., Euclidean distance).
import cv2
import numpy as np
import matplotlib.pyplot as plt
import cv2
import numpy as np
import matplotlib.pyplot as plt
# KNN matching
matches = bf.knnMatch(descriptors1, descriptors2, k=2)
plt.imshow(matched_img)
plt.axis('off')
plt.show()
# Normalize histograms
hist1 = hist1 / hist1.sum()
hist2 = hist2 / hist2.sum()
# Print result
print("Earth Mover's Distance (EMD):", emd_result)
# Query point
query_point = np.array([[3, 5]], dtype=np.float32)