gpt4 book ai didi

python - opencv python使用houghcircle函数检测不规则形状

转载 作者:太空狗 更新时间:2023-10-30 01:27:29 28 4
gpt4 key购买 nike

我目前正在对像这样的图像进行圆形检测,但是一些水滴合并并形成一些不规则形状(原始图像中的红色标记)。我在 opencv 中使用 houghcircle 函数来检测圆圈。对于那些不规则形状,该函数只能将它们检测为几个小圆圈,但我真的希望程序将不规则形状视为一个完整的大形状,并像我在输出图像中绘制的那样得到一个大圆圈。

Original image

Output image

我的代码将检测所有圆并获取它们的直径。

这是我的代码:

def circles(filename, p1, p2, minR, maxR):
# print(filename)
img = cv2.imread(filename, 0)
img = img[0:1000, 0:1360]
l = len(img)
w = len(img[1])

cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 25,
param1 = int(p1) ,param2 = int(p2), minRadius = int(minR), maxRadius = int(maxR))

diameter = open(filename[:-4] + "_diamater.txt", "w")
diameter.write("Diameters(um)\n")
for i in circles[0,:]:
diameter.write(str(i[2] * 1.29 * 2) + "\n")

count = 0
d = []
area = []
for i in circles[0,:]:
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
count += 1
d += [i[2]*2]
area += [i[2]*i[2]*pi*1.286*1.286]

f = filename.split("/")[-1]
cv2.imwrite(filename[:-4] + "_circle.jpg", cimg)

# cv2.imwrite("test3/edge.jpg", edges)
print "Number of Circles is %d" % count

diaM = []
for i in d:
diaM += [i*1.286]

bWidth = range(int(min(diaM)) - 10, int(max(diaM)) + 10, 2)

txt = '''
Sample name: %s
Average diameter(um): %f std: %f
Drop counts: %d
Average coverage per drop(um^2): %f std: %f
''' % (f, np.mean(diaM), np.std(diaM), count, np.mean(area), np.std(area))

fig = plt.figure()
fig.suptitle('Histogram of Diameters', fontsize=14, fontweight='bold')
ax1 = fig.add_axes((.1,.4,.8,.5))
ax1.hist(diaM, bins = bWidth)
ax1.set_xlabel('Diameter(um)')
ax1.set_ylabel('Frequency')
fig.text(.1,.1,txt)
plt.savefig(filename[:-4] + '_histogram.jpg')
plt.clf()

print "Total area is %d" % (w*l)
print "Total covered area is %d" % (np.sum(area))

rt = "Number of Circles is " + str(count) + "\n" + "Coverage percent is " + str(np.divide(np.sum(area), (w*l))) + "\n"
return rt

最佳答案

您可以使用 minEnclosingCircle为了这。找到图像的轮廓,然后应用该函数将形状检测为圆形。

下面是一个带有 C++ 代码的简单示例。在您的情况下,我觉得您应该结合使用 Hough-circles 和 minEnclosingCircle,因为图像中的一些圆圈彼此非常接近,它们有可能被检测为单个轮廓。

输入图像:

input

圈子:

output

Mat im = imread("circ.jpg");
Mat gr;
cvtColor(im, gr, CV_BGR2GRAY);
Mat bw;
threshold(gr, bw, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(bw, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
for(int idx = 0; idx >= 0; idx = hierarchy[idx][0])
{
Point2f center;
float radius;
minEnclosingCircle(contours[idx], center, radius);

circle(im, Point(center.x, center.y), radius, Scalar(0, 255, 255), 2);
}

关于python - opencv python使用houghcircle函数检测不规则形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38907219/

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