我很难找到模糊物体周围的轮廓。以下是我想要实现的一些示例:
如您所见,对象非常模糊。我试过计算平均背景值并使用一个简单的阈值。问题是,较小的粒子比较大的粒子亮得多。因此,当我使用一个简单的阈值时,它要么切断了太多的小颗粒,要么在较大的颗粒周围不够紧。我还尝试根据对象的最暗像素值设置阈值。虽然这对某些粒子有效,但一旦粒子具有暗区和亮区,它就会分解。
我还尝试了 skimage 熵过滤器,但没有成功。由于图像的模糊性质,Canny 边缘检测也是不可能的。
但由于我的眼睛能够在物体周围画出轮廓,我认为必须有一种使用 python 和打开的 cv 找到轮廓的简单方法。
提前致谢! 卢卡
最佳答案
一个简单的方法是使用 Otsu's threshold或 adaptive threshold自动确定阈值。思路是Gaussian blur , 获得二值图像的阈值,然后 find contours并使用 cv2.drawContours
突出显示轮廓.您可能需要调整参数以获得所需的输出。结果:
输入(截图)->
输出
import cv2
import numpy as np
# Load image, grayscale, Gaussian blur, Adaptive threshold
image = cv2.imread('2.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (9,9), 0)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,23,3)
# Find contours and filter using contour area
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)
if area > 10:
cv2.drawContours(image, [c], -1, (36,255,12), 1)
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()
关于python - 使用 Python OpenCV 在模糊对象周围找到紧密轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60642827/