gpt4 book ai didi

python - 如何使用 OpenCV 在图像上标记数字并绘制圆圈

转载 作者:行者123 更新时间:2023-12-02 18:32:33 27 4
gpt4 key购买 nike

我有代码来计算图像中管道的数量。它基本上显示图像中管道的总数。这是代码

# import Libraries
import cv2
import numpy as np
import matplotlib.pyplot as plt

#load image
image=cv2.imread("enhanced.sample3.png")
#cv2.imshow("figure",image)
cv2.waitKey(0)
#create blob detector
detector=cv2.SimpleBlobDetector_create()
#keypoints
keypoints=detector.detect(image)
#create a blank file which is a numpy array to hold our circle that we will detect and mark during the process
blank = np.zeros((1,1))
blobs = cv2.drawKeypoints(image, keypoints, blank, (0,0,255),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

number_of_blobs = len(keypoints)
text = "Total Number of Blobss: " + str(len(keypoints))
cv2.putText(blobs, text, (2, 55), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)

# Display image with blob keypoints
#cv2.imshow("Blobs using default parameters", blobs)
cv2.waitKey(0)
#another function to create blobs
params=cv2.SimpleBlobDetector_Params()
#AREA
params.filterByArea=True
params.minArea=100
#CIRCULARITY
params.filterByCircularity=True
params.minCircularity=0.4
#CONVEXITY
params.filterByConvexity=True
params.minConvexity=0.3
#INERTIA
params.filterByInertia=True
params.minInertiaRatio=0.01

detector=cv2.SimpleBlobDetector_create(params)
keypoints=detector.detect(image)
blank=np.zeros((1,1))
number_of_blobs = len(keypoints)

#for i in range(number_of_blobs):
print("Number of Circular Blobs: " + str(number_of_blobs))

blobs=cv2.drawKeypoints(image,keypoints,blank,(0,200,222),cv2.DRAW_MATCHES_FLAGS_DEFAULT)

#number_of_blobs = len(keypoints)
text = "Number of Circular Blobs: " + str(len(keypoints))

cv2.putText(blobs, text, (1, 55), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)


cv2.imshow("blobs",blobs)
cv2.waitKey(0)



cv2.destroyAllWindows()

显示的输出是这样的

Image

但我想要这样的东西,有更大的圆圈。

image2

我怎样才能实现这个目标?

最佳答案

假设您已经有了要在其上绘制或写入文本的圆圈的轮廓,您可以找到 centroid对于每个圆形轮廓并使用 cv2.putText() 写入轮廓编号。要实际绘制圆圈,您可以使用 cv2.minEnclosingCircle() + cv2.circle() 。由于您没有提供示例输入图像,因此这里有一个简单示例,其中轮廓数字为青色,绘制的圆圈为红色。

输入图像->结果

import cv2

# Load image, convert to grayscale, Otsu's threshold
image = cv2.imread('1.png')
result = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Find contours, sort by contour area from smallest to largest
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=lambda x: cv2.contourArea(x))

for count, c in enumerate(cnts):
# Find centroid for each contour and label contour
M = cv2.moments(c)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
cv2.putText(result, str(count), (cx-5, cy+5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (246,255,12), 3)

# Find circle radius and draw circle
((x, y), r) = cv2.minEnclosingCircle(c)
cv2.circle(result, (int(x), int(y)), int(r), (100, 100, 255), 2)

# Display
cv2.imshow('image', image)
cv2.imshow('result', result)
cv2.waitKey()

关于python - 如何使用 OpenCV 在图像上标记数字并绘制圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69249813/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com