내가 보려고 만든 블로그

OpenCV를 사용하여 이미지에 주석 달기(python) 본문

OpenCV/python

OpenCV를 사용하여 이미지에 주석 달기(python)

hjh1023 2024. 6. 7. 20:20
반응형

 

# Import dependencies
import cv2
# Read Images
img = cv2.imread('Lenna.png')

# Print error message if image is null
if img is None:
    print('Could not read image')

# Draw line on image
imageLine = img.copy()

#Draw the image from point A to B
pointA = (200,80)
pointB = (450,80)
cv2.line(imageLine, pointA, pointB, (255, 255, 0), thickness=3, lineType=cv2.LINE_AA)

# Draw circle
imageCircle = img.copy()
# define the center of circle
circle_center = (300,280)
# define the radius of the circle
radius =100
#  Draw a circle using the circle() Function
cv2.circle(imageCircle, circle_center, radius, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)

#Draw Filled Circle
imageFilledCircle = img.copy()
# draw the filled circle on input image
cv2.circle(imageFilledCircle, circle_center, radius, (255, 0, 0), thickness=-1, lineType=cv2.LINE_AA)

#Draw Rectangle
imageRectangle = img.copy()
# define the starting and end points of the rectangle
start_point =(120,100)
end_point =(430,405)
# draw the rectangle
cv2.rectangle(imageRectangle, start_point, end_point, (0, 0, 255), thickness= 3, lineType=cv2.LINE_8)

#Draw an Ellipse
imageEllipse = img.copy()
# define the center point of ellipse
ellipse_center = (300,280)
# define the major and minor axes of the ellipse
axis1 = (100,50)
axis2 = (125,50)
# draw the ellipse
#Horizontal
cv2.ellipse(imageEllipse, ellipse_center, axis1, 0, 0, 360, (255, 0, 0), thickness=3)
#Vertical
cv2.ellipse(imageEllipse, ellipse_center, axis2, 90, 0, 360, (0, 0, 255), thickness=3)

#Draw a Half-Ellipse
halfEllipse = img.copy()
# define the axis point
axis1 = (100,50)
# draw the Incomplete/Open ellipse, just a outline
cv2.ellipse(halfEllipse, ellipse_center, axis1, 0, 180, 360, (255, 0, 0), thickness=3)
# if you want to draw a Filled ellipse, use this line of code
cv2.ellipse(halfEllipse, ellipse_center, axis1, 0, 0, 180, (0, 0, 255), thickness=-2)

#Adding Text
imageText = img.copy()
#let's write the text you want to put on the image
text = 'I am Lenna!'
#org: Where you want to put the text
org = (120,400)
# write the text on the input image


cv2.putText(imageText, text, org, fontFace = cv2.FONT_HERSHEY_COMPLEX, fontScale = 1.5, color = (250,225,100))


# Display Image
cv2.imshow('Original Image',img)
cv2.imshow('Image Line', imageLine)
cv2.imshow("Image Circle",imageCircle)
cv2.imshow('Image with Filled Circle',imageFilledCircle)
cv2.imshow('imageRectangle', imageRectangle)
cv2.imshow('ellipse Image',imageEllipse)
cv2.imshow('halfEllipse',halfEllipse)
cv2.imshow("Image Text",imageText)


cv2.waitKey(0)
반응형