【问题标题】:How to merge bounding boxes to detect group of spots?如何合并边界框以检测一组点?
【发布时间】:2021-06-19 06:59:26
【问题描述】:

我有一个检测太阳黑子的代码,但我想计算太阳黑子组的数量,而不是像我在此处 1 的当前输出那样计算单个点的数量。

import os
import cv2 # opencv library
import numpy as np
import matplotlib.pyplot as plt

"""Make the pwd implementation"""
cwd = os.getcwd()
file = "/sunspot1.jpg"
path = cwd + file
image = cv2.imread(path,0)

image_1 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
#plt.show()

#plot the image in graycolor
#gray = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
#plt.imshow(gray)
#plt.show()


# perform image thresholding
ret, thresh = cv2.threshold(image, 90, 255, cv2.THRESH_BINARY)
#plt.imshow(thresh, cmap = 'gray')
#plt.show()

#circle = cv2.circle(thresh, (249,249),(238),(0, 255, 0),1)
# plt.imshow(circle)
# plt.show()

# find taches contours
contours, hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
valid_cntrs = []

for i,cntr in enumerate(contours):
    x,y,w,h = cv2.boundingRect(cntr)
    #print("x = ",x,"y = ",y,"w = ",w,"h = ",h)
    if ((x-249)**2 + (y-249)**2)<= 238**2:
        valid_cntrs.append(cntr)
"""implement image size detection for the contour LINE 36"""
        
#count the taches number
taches= len(valid_cntrs);
#sunspot= 1*(10*groups+taches);

# count the number of dicovered sunspots
print("The number of taches is: ",taches)

if taches == 0:
    plt.imshow(image_1)
    plt.show()

else:

    contour_sizes = [(cv2.contourArea(contour), contour) for contour in valid_cntrs]

    for i in range(len(valid_cntrs)):
        x,y,w,h = cv2.boundingRect(contour_sizes[i][1])
        prevtaches = cv2.rectangle(image_1,(x,y),(x+w,y+h),(0,255,0),1)
plt.imshow(prevtaches)
plt.show()

这是我的代码。如何让我的输出对太阳黑子进行分组并看起来像这样2?谢谢。

【问题讨论】:

    标签: python opencv image-processing cv2


    【解决方案1】:

    你可以做的是在阈值化之后(并且在检测到单个轮廓之前),你可以执行像dilation这样的形态学操作,使白色区域更宽,这样附近的区域就可以连接起来,形成一个大轮廓。为此,您可以调整 kernel 大小以最佳方式满足您的需求,也可以使用 iterations 参数。

    您可以参考this

    之后,您可以绘制轮廓。

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 2020-01-23
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      相关资源
      最近更新 更多