如何使用 Python 和 Opencv 计算叶子显微图像中的气孔数?

How can I count number of stomata in the microscopic image of leaf using Python and Opencv?

我想数一数叶子显微图像中气孔的数量。下面附上其中一个样本。气孔的特点是一般呈椭圆形,中间有厚厚的黑色衬里。因为我有很多图像,所以我想自动化这个过程。我对 Python 很熟悉,但对计算机视觉还是个新手。

通过一些代码,我能够计算图像中的汽车数量。但是,它不适用于我的叶子图像。示例代码如下:

import cv2
import numpy as np
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
from numpy.lib.polynomial import poly

image = cv2.imread("leaf1.jpg")
box, label, count = cv.detect_common_objects(image)
output = draw_bbox(image, box, label, count)
plt.imshow(output)
plt.show()

我得到的结果如下,没有检测到图像中的任何物体。是否可以像这样计算叶子图像中的气孔数量?

我希望我理解你所说的 stomata :)

import sys
import cv2
import numpy as np

# Load image
pth = sys.path[0]
im = cv2.imread(pth+'/stomata.jpg')
im = cv2.resize(im, (im.shape[1]//2, im.shape[0]//2))
h, w = im.shape[:2]

# Remove noise and make grayscale
blr = cv2.medianBlur(im, 11)
blr = cv2.cvtColor(blr, cv2.COLOR_BGR2GRAY)

# Remove noise again
bw = cv2.medianBlur(blr, 45)  # 51
bw = cv2.erode(bw, np.ones((5, 5), np.uint8))

# Create mask
bw = cv2.threshold(bw, 110, 255, cv2.THRESH_BINARY)[1]

# Draw white border around mask to detect stomatas near borders
cv2.rectangle(bw, (0, 0), (w, h), 255, 5)

# Find contours and sort them by position
cnts, _ = cv2.findContours(bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnts.sort(key=lambda x: cv2.boundingRect(x)[0])

# Find and draw blocks
for cnt in cnts:
    x, y, w, h = cv2.boundingRect(cnt)
    cv2.rectangle(im, (x, y), (x+w, y+h), (0, 255, 0), 5)
    cv2.rectangle(bw, (x, y), (x+w, y+h), 127, 5)

# Save final output
bw = cv2.cvtColor(bw, cv2.COLOR_GRAY2BGR)
cv2.imwrite(pth+'/stack.jpg', np.hstack((im, bw)))

这是一个开始;不完整,检测有误。您可能需要更多图像才能获得更好的结果。你需要花时间来获得想要的结果。我不确定下一句,但您稍后可能需要使用类似 CNN(ConvNet) 的内容。