使用OpenCV提高/优化寻找轮廓的准确性

时间:2019-07-20 13:05:42

标签: python image opencv image-processing computer-vision

我正在尝试处理一个简单的应用程序,该应用程序可以在培养皿的照片中对细菌菌落进行计数。我主要使用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()

但是,结果不够准确。提供样本: orginal orginal image, and image with contours

我可以清楚地看到有许多没有圈出的轮廓(殖民地)。同时,有些轮廓什么也没指向。

我尝试做的事情:

  1. 处理噪音(使用开放变换)
  2. 调整图像大小以进行标准化
  3. 更改开放核的设置,阈值,查找轮廓和圆的半径。
  4. 使用自适应阈值

我所怀疑的是:

  1. 这只是一个反复尝试的游戏,具有阈值/内核/查找轮廓设置,我无法赶上
  2. 图像质量低
  3. 图像上杂讯太多

最后我的问题是-如何尽可能地提高寻找轮廓的准确性?我不想在此使用任何简化,我希望它尽可能地准确。

1 个答案:

答案 0 :(得分:1)

这里的一个好方法可能是使用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()