OpenCV-3之draw_shape

1、cv2.line

import cv2
img = cv2.imread(r"img/1.jpg")

cv2.line(img, pt1=(100, 30), pt2=(210, 180), color=(0, 0, 255), thickness=2) 
cv2.imshow("pic show", img)
cv2.waitKey(0)

2、cv2.circle:thickness=-1,内部填充

import cv2
img = cv2.imread(r"img/1.jpg")

cv2.circle(img, center=(50, 50), radius=30, color=(0, 0, 255), thickness=2) 
cv2.imshow("pic show", img)
cv2.waitKey(0)

3、cv2.rectangle:thickness=-1,内部填充

cv2.rectangle(img, pt1=(100, 30), pt2=(210, 180), color=(0, 0, 255), thickness=2)

4、cv2.ellipse:thickness=-1,内部填充

cv2.ellipse(img, center=(100, 100), axes=(100, 50), angle=0, startAngle=0, endAngle=360, color=(255, 0, 0), thickness=-1)

5、cv2.polylines:[pts],否则点不连城线,thickness不能取负数

pts = np.array([[10, 5], [50, 10], [70, 20], [20, 30]], np.int32)
pts = pts.reshape((-1, 1, 2))  # shape[4, 1, 2]
cv2.polylines(img, [pts], isClosed=True, color=(0, 0, 255), thickness=2)

6、cv2.putText

# org:Bottom-left corner of the text string in the image
# lineType=cv2.LINE_AA:抗锯齿
cv2.putText(img, text='beautiful girl', org=(10, 30), fontFace=cv2.FONT_HERSHEY_SIMPLEX, 
            fontScale=1, color=(0, 0, 255), thickness=1, lineType=cv2.LINE_AA)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
OpenCV provides a function called `cv2.findContours()` that can be used to find the contours of a shape in an image. To find a specific shape model, you can first create a template of the shape you want to find. This can be done by drawing the shape on a blank image and saving it. Then, you can use the `cv2.matchTemplate()` function to match the template to the image and find the location of the shape. Here is an example code snippet for finding a circle shape model in an image: ```python import cv2 # Load the image img = cv2.imread('image.jpg') # Create a template of the circle shape template = cv2.imread('circle_template.jpg', cv2.IMREAD_GRAYSCALE) # Find the contours of the image _, contours, _ = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Loop through the contours and match the template for contour in contours: # Get the bounding box of the contour x, y, w, h = cv2.boundingRect(contour) # Crop the image to the bounding box roi = img[y:y+h, x:x+w] # Match the template to the ROI result = cv2.matchTemplate(roi, template, cv2.TM_CCOEFF_NORMED) # Set a threshold for the match result threshold = 0.7 # Find the location of the shape locations = np.where(result >= threshold) # Draw a rectangle around the shape for loc in zip(*locations[::-1]): cv2.rectangle(img, (x+loc[0], y+loc[1]), (x+loc[0]+w, y+loc[1]+h), (0, 0, 255), 2) # Display the image with the detected shape cv2.imshow('Detected shape', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` Note that this is just one way to find a shape model in an image using OpenCV. Depending on the complexity of the shape, you may need to use more advanced techniques such as feature detection or machine learning.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值