Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python OpenCV使用形态学运算计算重叠圆_Python_Image_Opencv_Image Processing_Image Morphology - Fatal编程技术网

Python OpenCV使用形态学运算计算重叠圆

Python OpenCV使用形态学运算计算重叠圆,python,image,opencv,image-processing,image-morphology,Python,Image,Opencv,Image Processing,Image Morphology,这是一张我试图从中得到圆圈的图片 我用灰度图像的差分和腐蚀来得到边界 img_path= 'input_data/coins.jpg' img = cv2.imread(img_path) rgb,gray=getColorSpaces(img) a,b=0,255 plt.figure(figsize=(12, 12)) erosion_se=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) erosion = cv2.erode(gra

这是一张我试图从中得到圆圈的图片

我用灰度图像的差分和腐蚀来得到边界

img_path= 'input_data/coins.jpg'
img = cv2.imread(img_path)
rgb,gray=getColorSpaces(img)
a,b=0,255
plt.figure(figsize=(12, 12))

erosion_se=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
erosion = cv2.erode(gray,erosion_se,iterations = 1)
boundary=gray-erosion
image, contours, hierarchy = cv2.findContours(boundary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)    
plt.imshow(boundary,'gray')
我可以得到边界相对清晰的大多数圆的圆。 我想做两件事

  • 获取重叠圆的计数

  • 找到与图像边界接触的圆。 我可以通过比较圆的半径和图像边界来确定。问题是有两个特定的水滴未检测为圆

下面是圆边界图像中HoughCircles之后的输出。突出的大绿色圆是不需要的。我不确定为什么对于某些重叠区域,没有检测到圆


使用一种简单的轮廓过滤方法,而不是使用要求圆为“完美”圆且在连接的斑点上不准确的HoughCircles。主要思想如下:

计算重叠圆的数量

  • 近似单个圆的轮廓面积,即
    ~375
  • 查找轮廓,遍历轮廓并使用轮廓区域进行过滤
  • 和重叠圆
为了找到与图像边界接触的圆,我们将检测区域限制在图像的外部10个像素。我们在这张新图像上找到轮廓,然后使用轮廓区域进行过滤,以确定接触圆


计算重叠圆的数量

在转换为灰度和阈值以获得二值图像之后,我们将单个斑点/圆的轮廓面积近似为
~375
。接下来,我们在图像上找到轮廓,并使用
cv2.contourArea()
进行过滤。为了确定是否有重叠,我们将每个轮廓的面积除以单个圆的面积,然后使用
math.ceil()
找到天花板。如果我们得到的上限值大于1,这意味着blob已连接,我们只需将上限值添加到计数器

这是检测到的重叠圆

重叠:213

查找与图像边界接触的圆

其想法是创建一个黑匣子来遮掩不在边界上的图像内部部分。我们可以使用
cv2.fillPoly()
实现这一点。从这里我们找到轮廓并使用轮廓面积进行过滤。这个想法是,如果水滴与某个阈值区域相比相对较大,则意味着水滴最有可能接触到边缘

这是填充的黑框和检测到的触摸圈

感人:10


,这真是太好了。这正是我要找的。
    circles = cv2.HoughCircles(boundary, cv2.HOUGH_GRADIENT, 1, 20,
                  param1=30,
                  param2=15,
                  minRadius=5,
                  maxRadius=20)

    if circles is not None: 
        circles = np.uint16(np.around(circles))
        for i in circles[0,:]:
            cv2.circle(img,(i[0],i[2]),i[3],(0,255,0),2)
            cv2.circle(img,(i[0],i[2]),2,(0,0,255),3)

        cv2.imshow('circles', img)

        k = cv2.waitKey(0)
        if k == 27:
            cv2.destroyAllWindows()
import cv2
import numpy as np
import math

image = cv2.imread('1.jpg')
black_box = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# Count overlapping circles
single_area = 375
overlapping = 0

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    blob_area = math.ceil(area/single_area)
    if blob_area > 1:
        overlapping += blob_area
        cv2.drawContours(image, [c], -1, (36,255,12), 2)

# Find circles touching image boundary
h, w, _ = image.shape
boundary = 10
touching = 0
box = np.array(([boundary,boundary], 
                      [w-boundary,boundary], 
                      [w-boundary, h-boundary], 
                      [boundary, h-boundary]))
cv2.fillPoly(black_box, [box], [0,0,0])

copy = black_box.copy()
copy = cv2.cvtColor(copy, cv2.COLOR_BGR2GRAY)
copy = cv2.threshold(copy, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

cnts = cv2.findContours(copy, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area > 100:
        touching += 1
        cv2.drawContours(black_box, [c], -1, (36,255,12), 2)

print('Overlapping:', overlapping)
print('Touching:', touching)
cv2.imshow('image', image)
cv2.imshow('black_box', black_box)
cv2.waitKey()