| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import cv2
- import numpy as np
- import math
- def draw_arc_on_image(
- image_path,
- output_path,
- params,
- thickness=2,
- color=(0, 0, 255)
- ):
- """
- Draw ellipse arc on an image using given parameters.
- Args:
- image_path (str): path to the input image.
- output_path (str): path to save the output image.
- params (list or tensor): [cx, cy, a, b, theta1, theta2, theta3]
- cx, cy: center of ellipse
- a, b: long and short axis (radius, not diameter)
- theta1, theta2, theta3: angle range (radians)
- thickness (int): thickness of the arc.
- color (tuple): BGR color.
- Notes:
- OpenCV ellipse uses:
- - center (int, int)
- - axis lengths (int, int) are *half lengths*
- - rotation angle in degrees
- - startAngle, endAngle in degrees
- """
- # Load image
- img = cv2.imread(image_path)
- if img is None:
- raise FileNotFoundError(f"Image not found: {image_path}")
- cx, cy, a, b, t1, t2, t3 = params
- # Convert to int for OpenCV
- cx = int(cx)
- cy = int(cy)
- a = int(a) # long axis (radius)
- b = int(b) # short axis (radius)
- # Convert radian to degree
- t1_deg = math.degrees(t1)
- t2_deg = math.degrees(t2)
- t3_deg = math.degrees(t3)
- # If you want full ellipse:
- # cv2.ellipse(img, (cx, cy), (a, b), 0, 0, 360, color, thickness)
- # Draw arc only (theta1 -> theta2)
- cv2.ellipse(
- img,
- (cx, cy),
- (a, b),
- 0, # rotation angle of ellipse
- t1_deg, # start angle
- t2_deg, # end angle
- color,
- thickness
- )
- # Optionally draw second arc ¦È2 ¡ú ¦È3
- cv2.ellipse(
- img,
- (cx, cy),
- (a, b),
- 0,
- t2_deg,
- t3_deg,
- (0, 255, 0), # different color for visualization
- thickness
- )
- # Save output
- cv2.imwrite(output_path, img)
- print(f"[Saved] Arc drawn on image -> {output_path}")
- # Example usage:
- if __name__ == "__main__":
- params = [651.5971, 491.6041, 4.5752, 4.3241, 1.1640, 3.0971, 3.1106]
- draw_arc_on_image(
- image_path=r"/home/zhaoyinghan/下载/imageData.png",
- output_path="output_arc.png",
- params=params
- )
|