python - 使用python和OpenCV计数图像上的单元格

标签 python image opencv counting

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

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

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/

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

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

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

    在此先感谢您的帮助。

    最佳答案

    删除不够圆整的的点的一种方法是,将打开操作(您正在使用的)与所需的最小点大小的圆形结构元素一起使用(每个)小于结构元素的点将被删除)。在以下图像中,有一个与您的问题类似的示例,其中以这种方式删除了小点(来源:https://homepages.inf.ed.ac.uk/rbf/HIPR2/open.htm)。另外,OpenCV具有此操作的实现(https://docs.opencv.org/trunk/d9/d61/tutorial_py_morphological_ops.html)。

    enter image description here

    实际上,您已经使用了此操作,但是我认为以下行中定义的结构元素似乎太小:

    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
    

    另外,由于您显然使用的是不同分辨率的图像,因此可以根据图像的分辨率调整结构元素的大小。

    关于python - 使用python和OpenCV计数图像上的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60957257/

    相关文章:

    python - 从每行的开头删除制表符和空格

    python - 使用 seaborn 绘制时间序列数据

    image - 如何通过向量表示图像或音频以获得余弦相似度?

    python - PIL : IOError: [Errno 13] Permission denied: [picturename. jpg]

    python-3.x - opencv 中的 bin/example_reg_map_test 错误

    python - 全局名称 'x' 未定义

    python - 如何存储执行函数的结果并在以后重新使用?

    html - 为高分辨率显示器(主要是 iOS 视网膜显示器)处理高分辨率网络图像的正确方法是什么?

    PHP & SQL 加载 URL 并插入 IMG 类

    image-processing - 如何去隔行相机流?