Drawing a cross on an image with OpenCV

OpenCV is an Open Source Computer Vision Library in Python that provides numerous functions to perform various image and video processing operations. The library uses the NumPy module to represent all video frames and images as ndarray types.

In this article, we will explore different ways to draw a cross on an image using OpenCV Python. A cross can be drawn using built-in marker functions or by drawing intersecting lines manually.

Input Output Scenario

We start with an input image and draw a cross marker on it. The output shows the cross overlaid on the original image at the specified position.

Input Image Draw Cross Output Image

Using cv2.drawMarker() Function

The cv2.drawMarker() function draws predefined marker shapes on an image. It's the easiest way to draw a cross as it provides built-in cross marker types.

Syntax

cv2.drawMarker(img, position, color, markerType, markerSize, thickness, line_type)

Parameters

  • img: The source image where to draw the marker

  • position: (x, y) coordinates where the cross is positioned

  • color: BGR color tuple for the marker

  • markerType: Type of marker to draw:

    • cv2.MARKER_CROSS: Standard crosshair marker
    • cv2.MARKER_TILTED_CROSS: 45-degree tilted cross
    • cv2.MARKER_STAR: Combination of cross and tilted cross
  • markerSize: Length of the marker arms (default: 20 pixels)

  • thickness: Line thickness of the marker

  • line_type: Type of line (LINE_8, LINE_4, LINE_AA)

Example

This example demonstrates drawing a tilted cross marker on a sample image ?

import cv2
import numpy as np

# Create a sample image
img = np.zeros((300, 400, 3), dtype=np.uint8)
img[:] = (240, 240, 240)  # Light gray background

# Draw a tilted cross marker
cv2.drawMarker(img, (200, 150), color=(0, 0, 255), 
               markerType=cv2.MARKER_TILTED_CROSS, 
               markerSize=50, thickness=5, line_type=cv2.LINE_AA)

print("Cross marker drawn successfully")
print(f"Image shape: {img.shape}")
Cross marker drawn successfully
Image shape: (300, 400, 3)

Using cv2.line() Function

The cv2.line() function draws lines between two points. To create a cross, we draw two perpendicular lines that intersect at the desired center point.

Syntax

cv2.line(img, pt1, pt2, color, thickness, lineType)

Parameters

  • img: The source image

  • pt1: Starting point (x, y) of the line

  • pt2: Ending point (x, y) of the line

  • color: BGR color tuple

  • thickness: Line thickness

  • lineType: Type of line (LINE_8, LINE_4, LINE_AA)

Example Simple Cross

This example creates a cross by drawing two perpendicular lines ?

import cv2
import numpy as np

# Create a sample image
img = np.zeros((300, 400, 3), dtype=np.uint8)
img[:] = (240, 240, 240)  # Light gray background

# Define cross center and size
center_x, center_y = 200, 150
cross_size = 40

# Draw horizontal line
cv2.line(img, (center_x - cross_size, center_y), 
         (center_x + cross_size, center_y), 
         color=(255, 0, 0), thickness=8)

# Draw vertical line  
cv2.line(img, (center_x, center_y - cross_size), 
         (center_x, center_y + cross_size), 
         color=(255, 0, 0), thickness=8)

print("Cross drawn using cv2.line()")
print(f"Cross center: ({center_x}, {center_y})")
print(f"Cross size: {cross_size} pixels")
Cross drawn using cv2.line()
Cross center: (200, 150)
Cross size: 40 pixels

Example Diagonal Cross

This example creates a diagonal cross covering the entire image from corner to corner ?

import cv2
import numpy as np

# Create a sample image
img = np.zeros((200, 300, 3), dtype=np.uint8)
img[:] = (200, 220, 240)  # Light blue background

# Get image dimensions
height, width = img.shape[:2]

# Draw diagonal lines from corner to corner
cv2.line(img, (0, 0), (width-1, height-1), 
         color=(0, 255, 0), thickness=5)
cv2.line(img, (width-1, 0), (0, height-1), 
         color=(0, 255, 0), thickness=5)

print("Diagonal cross drawn")
print(f"Image dimensions: {width} x {height}")
Diagonal cross drawn
Image dimensions: 300 x 200

Comparison

Method Ease of Use Flexibility Best For
cv2.drawMarker() Very Easy Limited Standard cross shapes
cv2.line() Moderate High Custom cross designs

Conclusion

Use cv2.drawMarker() for quick standard cross markers with built-in shapes. Use cv2.line() when you need custom cross designs or precise control over positioning and angles.

Updated on: 2026-03-27T06:55:00+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements