Python 如何标记和测量水滴的大小?

Python 如何标记和测量水滴的大小?,python,opencv,image-processing,Python,Opencv,Image Processing,我正在用Python学习图像分析,我只是一个初学者。我能够编写一个代码(我在下面分享)来检测这个纳米颗粒图像中的斑点(纳米颗粒): 我可以使用cv2.connectedComponents检测到10个纳米颗粒,但现在我需要: 用数字标记每个纳米颗粒以生成最终图像 计算组成每个纳米颗粒的像素数,这样我就可以确定它们的大小 我试着四处调查,但找不到任何适合我的。有人愿意帮助我吗?如果你能提出一个代码,这将是伟大的,如果你也能解释它,这将是超级 import numpy as np impo

我正在用Python学习图像分析,我只是一个初学者。我能够编写一个代码(我在下面分享)来检测这个纳米颗粒图像中的斑点(纳米颗粒):

我可以使用cv2.connectedComponents检测到10个纳米颗粒,但现在我需要:

  • 用数字标记每个纳米颗粒以生成最终图像

  • 计算组成每个纳米颗粒的像素数,这样我就可以确定它们的大小

  • 我试着四处调查,但找不到任何适合我的。有人愿意帮助我吗?如果你能提出一个代码,这将是伟大的,如果你也能解释它,这将是超级

    import numpy as np
        import cv2
        from matplotlib import pyplot as plt
        img = cv2.imread('Izzie -  - 0002.tif')
    
        #show figure using matplotlib
        plt.figure(1)
        plt.subplot(2, 2, 1) # Figure 1 has subplots 2 raws, 2 columns, and this is plot 1
        plt.gca().set_title('Original')
        plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # , cmap='gray'
    
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        plt.figure(1)
        plt.subplot(2, 2, 2) # Figure 1 has subplots 2 raw, 2 columns, and this is plot 2
        plt.gca().set_title('Gray')
        plt.imshow(cv2.cvtColor(gray, cv2.COLOR_BGR2RGB)) # , cmap='gray'
    
    
    # In global thresholding (normal methods), we used an arbitrary chosen value as a threshold
        # In contrast, Otsu's method
        # avoids having to choose a value and determines it automatically.
        #The method returns two outputs. The first is the threshold that was used and the secon
        # output is the thresholded image.
    
    ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    
    print('Ret = ', ret) # Applies an arbitrary threshold of 128
    
    plt.figure(1)
        plt.subplot(2, 2, 3)
        plt.gca().set_title('Threshold')
        plt.imshow(cv2.cvtColor(thresh, cv2.COLOR_BGR2RGB))
    
    
    #-------------------------------------------------------------------------------------------
        # MORPHOLOGICAL TRANSFORMATION
        # noise removal using morphological trasnformations
        # For more info see: https://opencv-python
    tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html
    
        # Set up the kernel - structuring element
        kernel = np.ones((3,3), np.uint8) # 3x3 array of 1s of datatype 8-bytes
    
        # Remove noise using Opening (erosion followed by dilation)
        opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 4)
        plt.figure(2)
        plt.subplot(2, 2, 1)
        plt.gca().set_title('Noise rem')
        plt.imshow(cv2.cvtColor(opening, cv2.COLOR_BGR2RGB))
    
    
        # sure background area
        # dilation operation
        sure_bg = cv2.dilate(opening,kernel,iterations=3)
    
        plt.figure(2)
        plt.subplot(2, 2, 2)
        plt.gca().set_title('Dilated img')
        plt.imshow(cv2.cvtColor(sure_bg, cv2.COLOR_BGR2RGB))
    
    
    
        # Apply a distance transformation to transform the image into a gradient of B&W pixels and detect possible connected objects
        dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
    
        plt.figure(2)
        plt.subplot(2, 2, 3) 
        plt.gca().set_title('Dist_transform')
        plt.imshow(cv2.cvtColor(dist_transform, cv2.COLOR_BGR2RGB))
    
    
    
        # Apply a threshold to go back to binary B&W image
        ret, sure_fg = cv2.threshold(dist_transform, 0.7 * dist_transform.max(),255,0)
        print('Ret treshold: ', ret)
    
        plt.figure(2)
        plt.subplot(2, 2, 4) 
        plt.gca().set_title('Threshold')
        plt.imshow(cv2.cvtColor(sure_fg, cv2.COLOR_BGR2RGB))
    
    
        # Finding unknown region
        sure_fg = np.uint8(sure_fg) # creates an 8-bit unsigned matrix
    
        plt.figure(3)
        plt.subplot(1, 2, 1) 
        plt.gca().set_title('Sure_fg')
        plt.imshow(cv2.cvtColor(sure_fg, cv2.COLOR_BGR2RGB))
    
    
        unknown = cv2.subtract(sure_bg,sure_fg)
    
        plt.figure(3)
        plt.subplot(1, 2, 2) 
        plt.gca().set_title('Unknown')
        plt.imshow(cv2.cvtColor(unknown, cv2.COLOR_BGR2RGB))
    
    
        #----------------------------------------------------------------------------------------------------------------------#
    
        # Marker labelling
        # Connected components counts all black objects in the image. For explaination see: https://www.youtube.com/watch?v=hMIrQdX4BkE
        # It gives 2 objects in return, the number of objects and a picture with labelled objects.
    
    n_objects, markers = cv2.connectedComponents(sure_fg)
    
        plt.figure(4)
        plt.subplot(2, 1, 1) 
        plt.gca().set_title('markers')
        plt.imshow(markers) 
    
    
        # Add one to all labels so that sure background is not 0, but 1
        markers = markers+1
    
        # Now, mark the region of unknown with zero
        markers[unknown==255] = 0
    
    
        markers = cv2.watershed(img, markers)
        img[markers == 8] = [255, 0, 0] # Overlay red circles (-1 val) to img. 2, 3, 4 are all the different objects detected in the image
    
        plt.figure(4)
        plt.subplot(2, 1, 2)
        plt.gca().set_title('markers')
        plt.imshow(img)
    
    
    
        print('Number of particles detected: ', n_objects-2)
    
    
        plt.show()
    
    如果你的粒子是(几乎)黑色的,不要使用大津的阈值,而是使用一个固定的来遮罩(几乎)黑色的像素。在反向二值化图像上,然后可以应用形态学关闭(以获得整个粒子)和打开(以消除背景噪声),请参见。然后,找到所有轮廓以获得粒子和比例,请参见。我们确定所有轮廓的边界矩形,以便在输入图像中的粒子上放置一些标签,并通过将粒子边界框的宽度/高度除以比例边界框的宽度来计算粒子的水平和垂直直径

    在我的代码中,我省略了一些内容,包括Matplotlib输出。(在写作时,我注意到,您提供了更多的代码;我没有看到滚动条……我没有看到,也没有合并这些代码。)

    导入cv2
    从matplotlib导入pyplot作为plt
    从skimage import io(仅用于网络抓取图像),使用cv2.imread(用于本地图像)
    #从网络上读取图像;注意:已经是RGB了
    img=io.imread('https://i.stack.imgur.com/J46nA.jpg')
    #转换为灰度;注意:来源是网络抓取的RGB
    灰色=cv2.CVT颜色(img,cv2.COLOR\u RGB2GRAY)
    #使用固定阈值遮罩黑色区域
    _,thresh=cv2.阈值(灰色,30255,cv2.thresh\u二进制\u INV)
    #形态闭合,得到完整的颗粒;打开以消除噪音
    img_mop=cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7)))
    img_mop=cv2.morphologyEx(img_mop,cv2.morp_OPEN,cv2.getStructuringElement(cv2.morp_ELLIPSE,(15,15)))
    #寻找轮廓
    碳纳米管,碳纳米管=cv2.已找到碳纳米管(img\u mop,碳纳米管2.RETR\u外部,碳纳米管2.链约无)
    #获取缩放和粒子的边界矩形
    thr_尺寸=2000
    尺度=[cv2.contourArea(cnt)>thr_尺寸时,cnt中cnt的cv2.boundingRect(cnt)]
    粒子=[cv2.如果cv2.轮廓面积(cnt)
    具有固定阈值的
    thresh
    图像:

    应用形态学运算后的
    img_mop
    图像(注意:刻度仍然存在,因此我们可以使用它进行大小近似):

    最后,带有相应标签的输入/输出图像
    ìmg
    (由于图像大小限制,此处必须使用JPG):

    最后,但并非最不重要的是,
    打印
    输出:

    颗粒0 |水平直径:20.83 nm,垂直直径:23.03 nm
    颗粒1 |水平直径:20.83 nm,垂直直径:20.83 nm
    颗粒2 |水平直径:19.74 nm,垂直直径:17.54 nm
    颗粒3 |水平直径:23.03 nm,垂直直径:23.03 nm
    颗粒4 |水平直径:24.12 nm,垂直直径:24.12 nm
    颗粒5 |水平直径:21.93 nm,垂直直径:20.83 nm
    颗粒6 |水平直径:24.12 nm,垂直直径:23.03 nm
    颗粒7 |水平直径:21.93 nm,垂直直径:23.03 nm
    颗粒8 |水平直径:19.74 nm,垂直直径:21.93 nm
    颗粒9 |水平直径:19.74 nm,垂直直径:19.74 nm
    

    希望有帮助

    太棒了,非常感谢!现在让我研究一下你的代码!:)