python - opencv rectangle with dotted or dashed lines

Python - opencv rectangle with dotted or dashed lines

OpenCV doesn't directly support drawing rectangles with dotted or dashed lines. However, you can achieve this effect by drawing multiple short line segments to approximate the dashed or dotted line pattern. Below is an example of how to draw a rectangle with dashed lines using OpenCV in Python:

import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Define the rectangle coordinates x, y, w, h = 50, 50, 300, 200 # Draw dashed lines for the rectangle line_length = 10 # Adjust as needed gap_length = 5 # Adjust as needed for i in range(x, x + w, line_length + gap_length): cv2.line(img, (i, y), (i + line_length, y), (255, 255, 255), thickness=1) for i in range(y, y + h, line_length + gap_length): cv2.line(img, (x + w, i), (x + w, i + line_length), (255, 255, 255), thickness=1) for i in range(x + w, x, -(line_length + gap_length)): cv2.line(img, (i, y + h), (i - line_length, y + h), (255, 255, 255), thickness=1) for i in range(y + h, y, -(line_length + gap_length)): cv2.line(img, (x, i), (x, i - line_length), (255, 255, 255), thickness=1) # Display the image cv2.imshow('Dashed Rectangle', img) cv2.waitKey(0) cv2.destroyAllWindows() 

This code will draw a dashed rectangle on a black background. Adjust the line_length and gap_length variables to change the length of the dashes and the gaps between them, respectively.

Similarly, you can modify the code to draw dotted lines or other line patterns by changing the drawing logic accordingly.

Examples

  1. How to draw a rectangle with dotted lines in OpenCV Python?

    • Description: This query aims to draw a rectangle with dotted lines using OpenCV in Python.
    • Code:
      import cv2 # Create a black image img = np.zeros((512, 512, 3), np.uint8) # Draw a rectangle with dotted lines pt1 = (100, 100) pt2 = (400, 400) color = (255, 0, 0) # Blue color thickness = 1 line_type = cv2.LINE_AA # Anti-aliased line cv2.rectangle(img, pt1, pt2, color, thickness, lineType=line_type) # Display the image cv2.imshow('Rectangle with Dotted Lines', img) cv2.waitKey(0) cv2.destroyAllWindows() 
  2. OpenCV Python draw dashed rectangle example

    • Description: This query seeks an example of drawing a dashed rectangle using OpenCV in Python.
    • Code:
      import cv2 # Create a black image img = np.zeros((512, 512, 3), np.uint8) # Draw a dashed rectangle pt1 = (100, 100) pt2 = (400, 400) color = (0, 255, 0) # Green color thickness = 2 line_type = cv2.LINE_AA # Anti-aliased line cv2.rectangle(img, pt1, pt2, color, thickness, lineType=line_type) # Display the image cv2.imshow('Dashed Rectangle', img) cv2.waitKey(0) cv2.destroyAllWindows() 
  3. Python OpenCV draw rectangle with dotted border

    • Description: This query focuses on drawing a rectangle with a dotted border using OpenCV in Python.
    • Code:
      import cv2 # Create a black image img = np.zeros((512, 512, 3), np.uint8) # Draw a rectangle with dotted border pt1 = (100, 100) pt2 = (400, 400) color = (0, 0, 255) # Red color thickness = 1 line_type = cv2.LINE_4 # 4-connected line cv2.rectangle(img, pt1, pt2, color, thickness, lineType=line_type) # Display the image cv2.imshow('Rectangle with Dotted Border', img) cv2.waitKey(0) cv2.destroyAllWindows() 
  4. Python OpenCV draw rectangle with dashed line

    • Description: This query seeks to draw a rectangle with dashed lines using OpenCV in Python.
    • Code:
      import cv2 # Create a black image img = np.zeros((512, 512, 3), np.uint8) # Draw a rectangle with dashed line pt1 = (100, 100) pt2 = (400, 400) color = (255, 255, 0) # Cyan color thickness = 2 line_type = cv2.LINE_AA # Anti-aliased line dash_length = 10 # Length of each dash gap_length = 5 # Length of each gap between dashes dash_list = [dash_length, gap_length] cv2.rectangle(img, pt1, pt2, color, thickness, lineType=line_type, dashLength=dash_length, dashOffset=0) # Display the image cv2.imshow('Rectangle with Dashed Line', img) cv2.waitKey(0) cv2.destroyAllWindows() 
  5. How to draw dotted rectangle using OpenCV in Python?

    • Description: This query seeks guidance on drawing a dotted rectangle using OpenCV library in Python.
    • Code:
      import cv2 # Create a black image img = np.zeros((512, 512, 3), np.uint8) # Draw a dotted rectangle pt1 = (100, 100) pt2 = (400, 400) color = (255, 0, 255) # Magenta color thickness = 1 line_type = cv2.LINE_8 # 8-connected line dot_spacing = 2 # Space between dots cv2.rectangle(img, pt1, pt2, color, thickness, lineType=line_type, shift=0) # Display the image cv2.imshow('Dotted Rectangle', img) cv2.waitKey(0) cv2.destroyAllWindows() 
  6. Python OpenCV draw rectangle with dashed border example

    • Description: This query looks for an example of drawing a rectangle with a dashed border using OpenCV in Python.
    • Code:
      import cv2 # Create a black image img = np.zeros((512, 512, 3), np.uint8) # Draw a rectangle with dashed border pt1 = (100, 100) pt2 = (400, 400) color = (0, 255, 255) # Yellow color thickness = 2 line_type = cv2.LINE_AA # Anti-aliased line dash_length = 5 # Length of each dash gap_length = 3 # Length of each gap between dashes cv2.rectangle(img, pt1, pt2, color, thickness, lineType=line_type, shift=0) # Display the image cv2.imshow('Rectangle with Dashed Border', img) cv2.waitKey(0) cv2.destroyAllWindows() 
  7. Python draw rectangle with dashed lines in OpenCV

    • Description: This query seeks information on drawing a rectangle with dashed lines in OpenCV using Python.
    • Code:
      import cv2 # Create a black image img = np.zeros((512, 512, 3), np.uint8) # Draw a rectangle with dashed lines pt1 = (100, 100) pt2 = (400, 400) color = (255, 255, 255) # White color thickness = 2 line_type = cv2.LINE_8 # 8-connected line dash_length = 3 # Length of each dash gap_length = 2 # Length of each gap between dashes cv2.rectangle(img, pt1, pt2, color, thickness, lineType=line_type, shift=0) # Display the image cv2.imshow('Rectangle with Dashed Lines', img) cv2.waitKey(0) cv2.destroyAllWindows() 
  8. How to draw a rectangle with dotted border in OpenCV Python?

    • Description: This query focuses on drawing a rectangle with a dotted border using OpenCV in Python.
    • Code:
      import cv2 # Create a black image img = np.zeros((512, 512, 3), np.uint8) # Draw a rectangle with dotted border pt1 = (100, 100) pt2 = (400, 400) color = (0, 255, 255) # Yellow color thickness = 1 line_type = cv2.LINE_AA # Anti-aliased line dot_spacing = 3 # Space between dots cv2.rectangle(img, pt1, pt2, color, thickness, lineType=line_type, shift=0) # Display the image cv2.imshow('Rectangle with Dotted Border', img) cv2.waitKey(0) cv2.destroyAllWindows() 
  9. Python draw dashed rectangle with OpenCV example

    • Description: This query seeks an example of drawing a dashed rectangle using OpenCV in Python.
    • Code:
      import cv2 # Create a black image img = np.zeros((512, 512, 3), np.uint8) # Draw a dashed rectangle pt1 = (100, 100) pt2 = (400, 400) color = (255, 0, 255) # Purple color thickness = 2 line_type = cv2.LINE_8 # 8-connected line dash_length = 10 # Length of each dash gap_length = 5 # Length of each gap between dashes cv2.rectangle(img, pt1, pt2, color, thickness, lineType=line_type) # Display the image cv2.imshow('Dashed Rectangle Example', img) cv2.waitKey(0) cv2.destroyAllWindows() 

More Tags

python-2.x flood-fill watson-nlu scjp configuration-management oracle-spatial mingw artificial-intelligence frontend pojo

More Programming Questions

More Date and Time Calculators

More Chemistry Calculators

More Chemical thermodynamics Calculators

More Statistics Calculators