OpenCV Tutorial

Image Processing

Feature Detection and Description

Drawing Functions

Video Processing

Applications and Projects

Draw a triangle with centroid in OpenCV

Drawing a triangle and its centroid in OpenCV involves a few steps. First, you need to define the vertices of the triangle. Then, draw the triangle using the polylines function. Finally, compute the centroid of the triangle using the average of its vertices and mark it on the image.

Here's a step-by-step tutorial:

Drawing a Triangle with Centroid using OpenCV

  1. Setup: Install OpenCV if you haven't already:

    pip install opencv-python
    
  2. Code to Draw Triangle and Centroid:

    import cv2
    import numpy as np
    
    # Create an empty black image
    img = np.zeros((500, 500, 3), dtype=np.uint8)
    
    # Define vertices of the triangle
    pts = np.array([[100, 250], [400, 250], [250, 50]], np.int32)
    pts = pts.reshape((-1, 1, 2))
    
    # Draw the triangle
    cv2.polylines(img, [pts], isClosed=True, color=(0, 255, 0), thickness=2)
    
    # Compute the centroid of the triangle
    centroid = np.mean(pts, axis=0).astype(int)[0]
    cv2.circle(img, tuple(centroid), 5, (0, 0, 255), -1)  # Draw a red dot for the centroid
    
    # Display the image
    cv2.imshow('Triangle with Centroid', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
  3. Run the Code: When you run the code, you should see a triangle drawn on a black image with its centroid marked by a red dot.

Explanation:

  • We use a black canvas of size 500x500 to draw the triangle.

  • We define the triangle vertices in the pts array. Note that the vertices should be defined in a specific format suitable for the polylines function.

  • The polylines function helps us draw the triangle on the image.

  • The centroid of the triangle is computed using the mean of its vertices.

  • We use the circle function to mark the centroid on the image.

This tutorial provides a straightforward way to draw a triangle and its centroid using OpenCV functions.

  1. Creating a triangle and its centroid in Python with OpenCV:

    • This involves using OpenCV to draw a triangle and calculate its centroid.
    import cv2
    import numpy as np
    
    # Create an image canvas
    img = np.zeros((400, 400, 3), dtype=np.uint8)
    
    # Define triangle vertices
    pts = np.array([[100, 300], [300, 300], [200, 100]], np.int32)
    
    # Reshape to a 3x1 matrix
    pts = pts.reshape((-1, 1, 2))
    
    # Draw the triangle on the image
    cv2.polylines(img, [pts], isClosed=True, color=(255, 255, 255), thickness=2)
    
    # Calculate centroid
    M = cv2.moments(pts)
    cx = int(M['m10'] / M['m00'])
    cy = int(M['m01'] / M['m00'])
    
    # Draw the centroid
    cv2.circle(img, (cx, cy), 5, (0, 0, 255), -1)
    
    # Display the image
    cv2.imshow('Triangle with Centroid', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
  2. Enhancing triangle drawing with colors and styles in OpenCV:

    • This involves adding color and style variations to the drawn shapes, making them more visually appealing.
    # Draw a filled triangle with a green outline
    cv2.fillPoly(img, [pts], color=(0, 255, 0))
    cv2.polylines(img, [pts], isClosed=True, color=(0, 255, 0), thickness=2)