gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:30 25 4
gpt4 key购买 nike

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

尽管如此,结果还不够准确。提供 sample : orginal orginal image, and image with contours

我可以清楚地看到有很多轮廓(菌落)没有被圈起来。同时,还有一些轮廓什么也没有。

我尝试做的事情:

  1. 处理噪声(使用开变换)
  2. 调整图片大小以实现标准化
  3. 更改开核、阈值、寻找轮廓和圆半径的设置。
  4. 使用自适应阈值

我怀疑的是:

  1. 这只是阈值/内核/查找轮廓设置的试错,我跟不上
  2. 图像质量太差
  3. 图像上有太多噪点

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

最佳答案

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

关于python - 使用 OpenCV 提高/优化查找轮廓的准确性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57125006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com