Image Processing
Cross Detection
Computer Vision
Pattern Recognition
Image Analysis

Cross detection in image

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Detecting a cross in an image is not one single algorithm. The best method depends on how constrained the problem is: whether the cross has a known size, fixed orientation, high contrast, or appears in cluttered scenes. In practice, most solutions start with preprocessing and then choose either template matching or line-intersection analysis.

Start With The Problem Constraints

A cross shape is basically two line segments that intersect at roughly right angles. But the difficulty changes a lot depending on the data:

  • fixed-size printed marker
  • rotated cross in a noisy scene
  • thick cross versus thin lines
  • one cross versus many candidates

If the cross has a known appearance, template matching is often simplest. If the cross can vary in scale or orientation, detecting lines and verifying their intersection is more robust.

Basic Preprocessing

Before detecting the shape, reduce the image to a cleaner representation.

Typical steps are:

  • convert to grayscale
  • blur slightly to suppress noise
  • threshold or edge-detect the image
python
1import cv2
2
3image = cv2.imread("input.png")
4gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
5blurred = cv2.GaussianBlur(gray, (5, 5), 0)
6_, binary = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

This gives you a stable input for later geometry checks.

Approach 1: Template Matching

If the cross size and orientation are mostly fixed, template matching is direct.

python
1import cv2
2import numpy as np
3
4image = cv2.imread("input.png", cv2.IMREAD_GRAYSCALE)
5template = cv2.imread("cross_template.png", cv2.IMREAD_GRAYSCALE)
6
7result = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
8_, max_val, _, max_loc = cv2.minMaxLoc(result)
9
10if max_val > 0.8:
11    h, w = template.shape
12    top_left = max_loc
13    bottom_right = (top_left[0] + w, top_left[1] + h)
14    print("Cross found at", top_left, bottom_right)
15else:
16    print("No strong cross match")

This works well for controlled images such as calibration markers, scanned forms, or UI screenshots. It is weaker when the cross rotates or changes scale.

Approach 2: Detect Two Perpendicular Lines

For more flexible scenes, detect line segments first and then search for pairs that:

  • intersect near the same point
  • form an angle close to 90 degrees
  • have reasonable length and thickness

A simple OpenCV strategy is Canny plus Hough transform.

python
1import cv2
2import numpy as np
3
4image = cv2.imread("input.png")
5gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
6edges = cv2.Canny(gray, 50, 150)
7
8lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=50,
9                        minLineLength=20, maxLineGap=5)
10
11if lines is not None:
12    for line in lines[:10]:
13        x1, y1, x2, y2 = line[0]
14        print((x1, y1), (x2, y2))

This is only the first half. After line detection, you still need to compute intersections and angles.

Verifying A Cross Geometrically

Suppose you have two line segments. You can estimate their directions and reject any pair whose angle is too far from perpendicular.

python
1import math
2
3
4def angle_degrees(line):
5    x1, y1, x2, y2 = line
6    return math.degrees(math.atan2(y2 - y1, x2 - x1))
7
8
9def near_perpendicular(line1, line2, tolerance=10):
10    a1 = angle_degrees(line1)
11    a2 = angle_degrees(line2)
12    diff = abs(a1 - a2) % 180
13    diff = min(diff, 180 - diff)
14    return abs(diff - 90) <= tolerance

Combine this with an intersection test and you have a practical cross detector for many industrial and robotics-style images.

Which Method To Choose

Use template matching when:

  • the cross is visually stable
  • rotation is limited
  • scale is predictable

Use line-based detection when:

  • orientation changes
  • size varies
  • there may be multiple candidate crosses
  • you need geometric validation rather than pure visual similarity

In harder cases, a learned detector or feature-based approach may be worth it, but classical image processing is often enough.

Common Pitfalls

A common mistake is skipping preprocessing and feeding a noisy image directly into line detection. That usually produces many false positives from unrelated edges.

Another issue is assuming every line intersection is a cross. A real cross needs more than just crossing lines. It usually also needs angle, length, and local shape checks.

Template matching is also easy to misuse. If the cross rotates or scales, a single template can fail badly even when the target is visually obvious to a human.

Finally, do not forget thickness and polarity. A dark cross on a light background and a light cross on a dark background may require different thresholding or inversion.

Summary

  • Cross detection usually starts with preprocessing, then either template matching or line-intersection analysis.
  • Template matching is easiest when the cross shape is fixed.
  • Hough-line-based methods work better when size or orientation varies.
  • A valid cross usually needs intersection and near-perpendicular angle checks.
  • Good preprocessing often matters more than the final detector choice.

Course illustration
Course illustration

All Rights Reserved.