我正在用Python学习图像分析,我只是一个初学者。我能够编写代码(我在下面分享)来检测此纳米颗粒图像中的斑点(纳米颗粒):
我可以使用以下方法检测到有 10 个纳米粒子cv2.connectedComponents
,但现在我需要:
用数字标记每个纳米颗粒以生成最终图像。
计算组成每个纳米粒子的像素数,以便我可以确定它们的大小。
我尝试四处研究,但找不到任何适合我的东西。有谁愿意帮助我吗?如果你能提出一个代码那就太好了,如果你也能解释它,那就太棒了!
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()