优化OpenCV查找轮廓的准确性

3

我正在处理一个简单的应用程序,该应用程序在Petri培养皿的照片中计算细菌菌落的数量。我主要使用Python和cv2库。

我正在使用上述代码:

#reading image (reading is fixed for tests) and putting Opening morphological transformation to improve edge visibility
img = cv2.imread("image1.jpg",1)
img = cv2.resize(img,(500,500))
kernel = py.ones((7,7),py.uint8)
open = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

#converting colors to GRAY scale, setting threshold and contours. Setting a copy for result comprasion
img_gray = cv2.cvtColor(open, cv2.COLOR_BGR2GRAY)
copy = img.copy()
ret,thresh = cv2.threshold(img_gray,190,255,cv2.THRESH_BINARY)
im2,contours2,hierarchies = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

#drawing and counting countours (colonies)
visible_colonies = 0
for contour in (contours2):
  (x,y),radius = cv2.minEnclosingCircle(contour)
  center = (int(x),int(y))
  radius = int(radius)
  if(radius>2 and radius<25):
    cv2.circle(img,center,radius,(255,0,255),2)
    visible_colonies += 1
#showing result
plt.imshow(img)
plt.show()
print(visible_colonies)
plt.imshow(copy)
plt.show()

尽管如此,结果并不够准确。提供一个示例:原始图像和轮廓图像 我可以清楚地看到有许多未被圈出的轮廓(菌落)。同时,有一些轮廓指向无物。
我尝试过什么:
  1. 处理噪声(使用开运算)
  2. 调整图像大小进行标准化
  3. 更改开运算核、阈值、查找轮廓和圆的半径设置。
  4. 使用自适应阈值
我怀疑的是什么:
  1. 这只是一个试错过程,需要大量的阈值/核/查找轮廓设置,我无法跟上
  2. 图像质量太低了
  3. 图像上有太多噪音
最后我的问题在于 - 如何尽可能地提高轮廓检测的准确性?我不想使用任何简化,我希望这尽可能准确。
1个回答

6

这里一个好的方法可能是使用cv2.inRange()进行颜色阈值分割。思路是将图像转换为HSV格式,然后使用上下限颜色阈值来分割菌落。我们在蒙版上绘制检测到的菌落,然后在蒙版上找到轮廓。


绘制在蒙版上的颜色分割的菌落

enter image description here

结果

enter image description here

我们还可以跟踪菌落数量

244

如果想要仅检测中等/大菌落,则可使用cv2.contourArea()和最小阈值面积等其他过滤器进行优化。可能需要提供更高分辨率的图像以获得更精确的结果。

import numpy as np
import cv2

image = cv2.imread('1.png')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

lower = np.array([0, 71, 0], dtype="uint8")
upper = np.array([179, 255, 255], dtype="uint8")
mask = cv2.inRange(hsv, lower, upper)

cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

colonies = []

for c in cnts:
    cv2.drawContours(image, [c], -1, (36, 255, 12), 2)
    colonies.append(c)

print(len(colonies))
cv2.imshow('mask', mask)
cv2.imshow('image', image)
cv2.imwrite('mask.png', mask)
cv2.imwrite('image.png', image)
cv2.waitKey()

可以使用此脚本找到下限和上限。

import cv2
import sys
import numpy as np

def nothing(x):
    pass

useCamera=False

# Check if filename is passed
if (len(sys.argv) <= 1) :
    print("'Usage: python hsvThresholder.py <ImageFilePath>' to ignore camera and use a local image.")
    useCamera = True

# Create a window
cv2.namedWindow('image')

# create trackbars for color change
cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv
cv2.createTrackbar('SMin','image',0,255,nothing)
cv2.createTrackbar('VMin','image',0,255,nothing)
cv2.createTrackbar('HMax','image',0,179,nothing)
cv2.createTrackbar('SMax','image',0,255,nothing)
cv2.createTrackbar('VMax','image',0,255,nothing)

# Set default value for MAX HSV trackbars.
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)

# Initialize to check if HSV min/max value changes
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0

# Output Image to display
if useCamera:
    cap = cv2.VideoCapture(0)
    # Wait longer to prevent freeze for videos.
    waitTime = 330
else:
    img = cv2.imread(sys.argv[1])
    output = img
    waitTime = 33

while(1):

    if useCamera:
        # Capture frame-by-frame
        ret, img = cap.read()
        output = img

    # get current positions of all trackbars
    hMin = cv2.getTrackbarPos('HMin','image')
    sMin = cv2.getTrackbarPos('SMin','image')
    vMin = cv2.getTrackbarPos('VMin','image')

    hMax = cv2.getTrackbarPos('HMax','image')
    sMax = cv2.getTrackbarPos('SMax','image')
    vMax = cv2.getTrackbarPos('VMax','image')

    # Set minimum and max HSV values to display
    lower = np.array([hMin, sMin, vMin])
    upper = np.array([hMax, sMax, vMax])

    # Create HSV Image and threshold into a range.
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, lower, upper)
    output = cv2.bitwise_and(img,img, mask= mask)

    # Print if there is a change in HSV value
    if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
        print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
        phMin = hMin
        psMin = sMin
        pvMin = vMin
        phMax = hMax
        psMax = sMax
        pvMax = vMax

    # Display output image
    cv2.imshow('image',output)

    # Wait longer to prevent freeze for videos.
    if cv2.waitKey(waitTime) & 0xFF == ord('q'):
        break

# Release resources
if useCamera:
    cap.release()
cv2.destroyAllWindows()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接