【问题标题】:How can I count number of stomata in the microscopic image of leaf using Python and Opencv?如何使用 Python 和 Opencv 计算叶片显微图像中的气孔数量?
【发布时间】:2021-08-17 20:51:31
【问题描述】:

我想计算叶片显微图像中的气孔数量。下面附上其中一个样本。气孔的特点是一般呈椭圆形,中间有厚厚的黑色衬里。由于我有很多图像,我想自动化这个过程。我熟悉 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()

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

【问题讨论】:

    标签: python python-3.x opencv computer-vision opencv-python


    【解决方案1】:

    我希望我明白你所说的气孔是什么意思 :)

    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) 之类的内容。

    【讨论】:

    • 谢谢。这确实让我更接近解决方案。您能否指出哪个参数指的是气孔的数量,即左图中的绿色矩形或右图中的黑色矩形的数量?
    • 您好 :) 您可以在最后一行添加类似这样的内容: print("检测到的气孔数:", len(cnts-1))
    • cnts-1 是因为整个图像也被称为轮廓之一。
    • 我认为应该是 len(cnts)-1。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    相关资源
    最近更新 更多