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

问题描述 投票:0回答:2

我正在尝试处理一个简单的应用程序,该应用程序可以计算培养皿照片上的菌落数量。我主要使用 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. 图像上的噪点太多

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

python image opencv image-processing computer-vision
2个回答
6
投票

这里一个好的方法可能是使用

cv2.inRange()
进行颜色阈值处理。这个想法是将图像转换为 HSV 格式,并使用较低/较高的颜色阈值来分割菌落。我们将检测到的菌落绘制到掩模上,然后在掩模上找到轮廓。


将颜色分段的菌落绘制到掩模上

结果

我们还可以跟踪菌落数量

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()

0
投票

如果我有不同颜色的菌落怎么办?我怎样才能检测到它们?

© www.soinside.com 2019 - 2024. All rights reserved.