在我的研究專案中,我試圖區分水螅植物(較大的看起來像橙子的變形蟲)和它們的豐年蝦飼料(較小的橙點),以便我們可以使用移液機自動清潔培養皿。來自培養皿機器的快照影像示例如下所示:

到目前為止,我已經應用了一個圓形蒙版和一個橙色空間蒙版來創建一個清理過的影像,因此它主要是蝦和九頭蛇。

過濾后的影像中留下了一些殘留的光偽影,但我必須付出代價,否則我會失去非常薄的九頭蛇的解析度,例如原始影像左上角的解析度。
I was hoping to box and label the larger hydra plants but couldn't find much applicable literature for differentiating between large and small objects of similar attributes in an image, to achieve my goal.
I don't want to approach this using ML because I don't have the manpower or a large enough dataset to make a good training set, so I would truly appreciate some easier vision processing tools. I can afford to lose out on the skinny hydra, just if I can know of a simpler way to identify the more turgid, healthy hydra from the already cleaned up image that would be great.
I have seen some content about using openCV findCountours? Am I on the right track?
Attached is the code I have so you know what datatypes I'm working with.
import cv2
import os
import numpy as np
import PIL
#abspath = "/Users/johannpally/Documents/GitHub/HydraBot/vis_processing/hydra_sample_imgs/00049.jpg"
#note we are in the vis_processing folder already
#PIL.Image.open(path)
path = os.getcwd() "/hydra_sample_imgs/00054.jpg"
img = cv2.imread(path)
c_img = cv2.imread(path)
#==============GEOMETRY MASKS===================
# start result mask with circle mask
ww, hh = img.shape[:2]
r = 173
xc = hh // 2
yc = ww // 2
cv2.circle(c_img, (xc - 10, yc 2), r, (255, 255, 255), -1)
hsv_cir = cv2.cvtColor(c_img, cv2.COLOR_BGR2HSV)
l_w = np.array([0,0,0])
h_w = np.array([0,0,255])
result_mask = cv2.inRange(hsv_cir, l_w, h_w)
#===============COLOR MASKS====================
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#(hMin = 7 , sMin = 66, vMin = 124), (hMax = 19 , sMax = 255, vMax = 237)
# Threshold of orange in HSV space output from the HSV picker tool
l_orange = np.array([7, 66, 125])
h_orange = np.array([19, 255, 240])
orange_mask = cv2.inRange(hsv_img, l_orange, h_orange)
orange_res = cv2.bitwise_and(img, img, mask = orange_mask)
#===============COMBINE MASKS====================
for i in range(len(result_mask)):
for j in range(len(result_mask[i])):
if result_mask[i][j] == 255 & orange_mask[i][j] == 255:
result_mask[i][j] = 255
else:
result_mask[i][j] = 0
c_o_res = cv2.bitwise_and(img, img, mask=result_mask)
cv2.imshow('res', c_o_res)
cv2.waitKey(0)
cv2.destroyAllWindows()
uj5u.com熱心網友回復:
你在正確的軌道上,但我必須誠實。沒有深度學習,你會得到很好的結果,但并不完美。
這就是我設法使用輪廓得到的:
代碼:
import cv2
import os
import numpy as np
import PIL
#abspath = "/Users/johannpally/Documents/GitHub/HydraBot/vis_processing/hydra_sample_imgs/00049.jpg"
#note we are in the vis_processing folder already
#PIL.Image.open(path)
path = os.getcwd() "/hydra_sample_imgs/00054.jpg"
img = cv2.imread(path)
c_img = cv2.imread(path)
#==============GEOMETRY MASKS===================
# start result mask with circle mask
ww, hh = img.shape[:2]
r = 173
xc = hh // 2
yc = ww // 2
cv2.circle(c_img, (xc - 10, yc 2), r, (255, 255, 255), -1)
hsv_cir = cv2.cvtColor(c_img, cv2.COLOR_BGR2HSV)
l_w = np.array([0,0,0])
h_w = np.array([0,0,255])
result_mask = cv2.inRange(hsv_cir, l_w, h_w)
#===============COLOR MASKS====================
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#(hMin = 7 , sMin = 66, vMin = 124), (hMax = 19 , sMax = 255, vMax = 237)
# Threshold of orange in HSV space output from the HSV picker tool
l_orange = np.array([7, 66, 125])
h_orange = np.array([19, 255, 240])
orange_mask = cv2.inRange(hsv_img, l_orange, h_orange)
orange_res = cv2.bitwise_and(img, img, mask = orange_mask)
#===============COMBINE MASKS====================
for i in range(len(result_mask)):
for j in range(len(result_mask[i])):
if result_mask[i][j] == 255 & orange_mask[i][j] == 255:
result_mask[i][j] = 255
else:
result_mask[i][j] = 0
c_o_res = cv2.bitwise_and(img, img, mask=result_mask)
# We have to use gray image (1 Channel) to use cv2.findContours
gray = cv2.cvtColor(c_o_res, cv2.COLOR_RGB2GRAY)
contours, _ = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
minAreaSize = 150
for contour in contours:
if cv2.contourArea(contour) > minAreaSize:
# -------- UPDATE 1 CODE --------
# Rectangle Bounding box Drawing Option
# rect = cv2.boundingRect(contour)
# x, y, w, h = rect
# cv2.rectangle(img, (x, y), (x w, y h), (0, 255, 0), 2)
# FINDING CONTOURS CENTERS
M = cv2.moments(contour)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
# DRAW CENTERS
cv2.circle(img, (cX, cY), radius=0, color=(255, 0, 255), thickness=5)
# -------- END OF UPDATE 1 CODE --------
# DRAW
cv2.drawContours(img, contour, -1, (0, 255, 0), 1)
cv2.imshow('FinallyResult', img)
cv2.imshow('res', c_o_res)
cv2.waitKey(0)
cv2.destroyAllWindows()
更新 1:
要找到輪廓的中心,我們可以使用 cv2.moments。代碼# -------- UPDATE 1 CODE --------在 for 回圈中使用注釋進行了編輯。正如我之前提到的,這不是完美的方法,也許有一種方法可以改進我的答案,在沒有深度學習的情況下找到九頭蛇的中心。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313199.html
