[使用python和OpenCV计数图像上的细胞

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

我正在尝试编写一种算法来对图像上的点(单元)进行计数。

这里是我到目前为止编写的脚本:

import numpy as np
import cv2
import os

for dirname in os.listdir("images/"):

    for filename in os.listdir("images/" + dirname + "/"):

        # Image read
        img = cv2.imread("images/" + dirname + "/" + filename, 0)

        # Denoising
        denoisedImg = cv2.fastNlMeansDenoising(img);

        # Threshold (binary image)
        # thresh – threshold value.
        # maxval – maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.
        # type – thresholding type
        th, threshedImg = cv2.threshold(denoisedImg, 200, 255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU) # src, thresh, maxval, type

        # Perform morphological transformations using an erosion and dilation as basic operations
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
        morphImg = cv2.morphologyEx(threshedImg, cv2.MORPH_OPEN, kernel)

        # Find and draw contours
        contours, hierarchy = cv2.findContours(morphImg, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        contoursImg = cv2.cvtColor(morphImg, cv2.COLOR_GRAY2RGB)
        cv2.drawContours(contoursImg, contours, -1, (255,100,0), 3)

        cv2.imwrite("results/" + dirname + "/" + filename + "_result.tif", contoursImg)
        textFile = open("results/results.txt","a")
        textFile.write(filename + " Dots number: {}".format(len(contours)) + "\n")
        textFile.close()

这是我的输入图片:Input

这是我的结果:Result

目前,此脚本在该输入中效果很好,但是当我切换到其他类似输入时:Input

我得到非常糟糕的结果:

Result

我只希望保留点是:

  • 四舍五入

或:

  • 就亮度而言,在其他点的前10%中>]
  • 哪个“足够大”(相对于图像,所以说消除表面上底部10%的点)。
  • 我阅读了有关创建“ is_contour_bad”函数的内容,可以用来确定轮廓是否不良并应将其删除。

https://www.pyimagesearch.com/2015/02/09/removing-contours-image-using-python-opencv/

我尝试实现它,但是没有得到任何结果。不过,这个主意对我来说似乎很好。

我也根据图像调整阈值和腐蚀/膨胀,但实际上最好的办法是能够对之前列举的每个参数进行操作。仍然,如果您有想法自动找到图像的有用属性以在图像上应用正确的滤镜,它可能会很有趣。

如果您有任何想法或一段代码,甚至很小,可以帮助我实现该目标,那将是非常棒的。

感谢您的帮助。

我正在尝试编写一种算法来对图像上的点(单元)进行计数。这是到目前为止我做过的脚本:导入numpy作为np import cv2导入os.listdir(“ images /”)中目录名的os:for ...

python image opencv counting
1个回答
0
投票

去除未圆整

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