Setting Up OpenCV with Python Development Environment on Ubuntu and Building Recognition Demos

Installling Python

Begin with installing Python version 2.7 as the development environment.

sudo apt-get install python2.7
sudo apt-get install python2.7-dev

Installing OpenCV

Multiple installation methods exist, starting with the most straightforward apporach.

sudo apt-get install python-opencv

Camera Access Demo

Basic Test Example

import cv2
import numpy
cam_capture = cv2.VideoCapture(0)
while True:
    status, image_frame = cam_capture.read()
    cv2.imshow("camera_view", image_frame)
    if cv2.waitKey(100) & 0xff == ord('q'):
        break
cam_capture.release()
cv2.destroyAllWindows()

Template Matching Implementation

Matching Example Code

import cv2
import numpy

# Load reference image
reference_img = cv2.imread("src.png")

# Load search image
search_img = cv2.imread("dst.png")

# Extract dimensions from reference
height, width = reference_img.shape[:2]
print height, width

# Execute matching operation
match_result = cv2.matchTemplate(search_img, reference_img, cv2.TM_SQDIFF_NORMED)

# Apply normalization
cv2.normalize(match_result, match_result, 0, 1, cv2.NORM_MINMAX, -1)

minimum_val, maximum_val, min_position, max_position = cv2.minMaxLoc(match_result)

formatted_min = str(minimum_val)
print formatted_min

# Draw rectangle around matched area
start_point = min_position
end_point = (start_point[0] + width, start_point[1] + height)
cv2.rectangle(search_img, start_point, end_point, (0,0,255), 2)

cv2.imshow("output", search_img)
cv2.waitKey()
cv2.destroyAllWindows()

FLANN Feature Point Matching

Version Downgrade Requirements

After OpenCV 3.4.x and in the 4.x series, SIFT algorithms were patented and became unavailable. Since FLANN requires:

extractor = cv2.xfeatures2d.SIFT_create()

Version downgrade is necessary:

sudo apt-get remove python-opencv
sudo pip install opencv-python==3.4.2.16

Install matplotlib library module:

python -m pip install matplotlib
sudo apt-get install python-tk
pip install opencv-contrib-python==3.4.2.16

FLANN Matching Example

# FLANN-based matching implementation
import numpy as np
import cv2
from matplotlib import pyplot as plt
 
# Minimum required matches threshold
THRESHOLD_MATCHES = 10 

# Reference image
reference_image = cv2.imread('src.png',0) 

# Target image
target_image = cv2.imread('dst.png',0) 

# Initialize SIFT detector
feature_detector = cv2.xfeatures2d.SIFT_create()

# Extract keypoints and descriptors
keypoints_ref, descriptors_ref = feature_detector.detectAndCompute(reference_image,None)
keypoints_target, descriptors_target = feature_detector.detectAndCompute(target_image,None)

# Configure FLANN matcher
KD_TREE_INDEX = 0
index_config = dict(algorithm = KD_TREE_INDEX, trees = 5)
search_config = dict(checks = 50)
matcher = cv2.FlannBasedMatcher(index_config, search_config)
matches_found = matcher.knnMatch(descriptors_ref,descriptors_target,k=2)

# Filter quality matches using Lowe's ratio test
good_matches = []
for match, neighbor in matches_found:
    if match.distance < 0.7*neighbor.distance:
        good_matches.append(match)

if len(good_matches) > THRESHOLD_MATCHES:
    # Extract coordinate points
    source_points = np.float32([ keypoints_ref[m.queryIdx].pt for m in good_matches ]).reshape(-1,1,2)
    destination_points = np.float32([ keypoints_target[m.trainIdx].pt for m in good_matches ]).reshape(-1,1,2)
    
    # Calculate transformation matrix
    transform_matrix, inlier_mask = cv2.findHomography(source_points, destination_points, cv2.RANSAC, 5.0)
    mask_values = inlier_mask.ravel().tolist()
    img_height, img_width = reference_image.shape
    
    # Define corner coordinates
    corners = np.float32([ [0,0],[0,img_height-1],[img_width-1,img_height-1],[img_width-1,0] ]).reshape(-1,1,2)
    transformed_corners = cv2.perspectiveTransform(corners,transform_matrix)
    cv2.polylines(target_image,[np.int32(transformed_corners)],True,0,2, cv2.LINE_AA)
else:
    print( "Insufficient matches detected - %d/%d" % (len(good_matches),THRESHOLD_MATCHES))
    mask_values = None

draw_settings = dict(matchColor=(0,255,0), 
                   singlePointColor=None,
                   matchesMask=mask_values, 
                   flags=2)

final_result = cv2.drawMatches(reference_image, keypoints_ref, target_image, keypoints_target, good_matches, None, **draw_settings)
cv2.imshow("result_output", final_result)
cv2.imshow("processed_target", target_image)
cv2.waitKey()

Tags: OpenCV python Ubuntu Computer Vision Template Matching

Posted on Thu, 03 Sep 2026 16:20:31 +0000 by witold