如何正确选择液滴内的气泡

问题描述

这是Wendy.Image处理相关的问题。 我必须正确选择气泡以分析其大小并计算图像序列之间的速度。

原始图片
enter image description here

图像序列 enter image description here // enter image description here // enter image description here

图片中最大的形状是液滴,液滴内部包含许多微小的气泡。

到目前为止,问题是: 1.由于气泡的形状破裂,几乎没有检测到轮廓。同时,检测到许多边缘。如何将边缘填充为轮廓(像一枚硬币,我只想要每个气泡的轮廓,而又不想让气泡内的线条出现)

当前进度
enter image description here
enter image description here

代码(检测轮廓)

gray=cv2.imread("1000.tif")
blurred = cv2.GaussianBlur(gray,(3,3),0)
edged = cv2.Canny(blurred,5,60)
cv2.imshow('edged ',edged )
cv2.waitKey(0)
contours,hierarchy = cv2.findContours(edged,cv2.RETR_EXTERNAL,cv2.RETR_LIST)
img = gray.copy()
cv2.drawContours(img,contours,-1,(0,255),1)  
cv2.imwrite("contours.tif",img)
cv2.imshow("contours",img)  
cv2.waitKey(0)  

代码(填充孔)

im_floodfill = edged.copy()
h,w = edged.shape[:2]
mask = np.zeros((h+2,w+2),np.uint8)
cv2.floodFill(im_floodfill,mask,0),255)
im_floodfill_inv = cv2.bitwise_not(im_floodfill)
im_out = edged | im_floodfill_inv
cv2.imshow("Thresholded Image",edged)
cv2.imshow("Floodfilled Image",im_floodfill)
cv2.imshow("Inverted Floodfilled Image",im_floodfill_inv)
cv2.imshow("Foreground",im_out)
cv2.waitKey(0)
cv2.imwrite("fill.jpg",im_out)

结果将是
enter image description here

  1. 气泡的速度是我的目标。我试图将二进制图像序列放入Iamgej,并使用“ trackmate”(用于自动和半自动粒子跟踪的工具)。它具有固定的气泡大小设置以进行跟踪,但是,在我的情况下,气泡的大小完全不同,因此是否有任何方法可以跟踪气泡的运动?

如果这还不够清楚,请告诉我,我可以尝试更加具体。谢谢您的时间。

解决方法

这是Python / OpenCV中的一种方法。

  • 阅读输入内容
  • 转换为灰色
  • 阈值
  • 用形态填充孔
  • 获取轮廓和最大轮廓(尽管大概只有一个)
  • 在输入的副本上绘制轮廓
  • 保存结果

输入:

enter image description here

import cv2
import numpy as np

# read image
img = cv2.imread('vapor.jpg')

# convert to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# threshold
#thresh = cv2.threshold(gray,5,255,cv2.THRESH_BINARY)[1]
thresh = cv2.threshold(gray,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# apply close morphology to fill white circles
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(45,45))
thresh_cleaned = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel)

# get largest contour
contours = cv2.findContours(thresh_cleaned,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours,key=cv2.contourArea)

# draw contour
result = img.copy()
cv2.drawContours(result,[big_contour],(0,255),1) 

# save image with points drawn
cv2.imwrite('vapor_thresh.jpg',thresh)
cv2.imwrite('vapor_cleaned_thresh.jpg',thresh_cleaned)
cv2.imwrite('vapor_contour.jpg',result)

cv2.imshow("thresh",thresh)
cv2.imshow("thresh_cleaned",thresh_cleaned)
cv2.imshow("contour",result)
cv2.waitKey(0)
cv2.destroyAllWindows()

阈值图像:

enter image description here

形态填充图像:

enter image description here

轮廓图片:

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...