用Python-OpenCV测量微粒的feret直径

2024-09-28 16:19:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python OpenCV2测量沉积在玻璃上的微观粒子的费雷特直径。目前,我有近150张图像,这一过程需要自动化。为了进行测量,我编写了一个Python脚本,如下所示:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import io, color, measure

##step-1 reading the image
img = cv2.imread('1.tif', 0)
pixel_2_micron = 1.75 #1 pixel is equal too 1.75 microns
#img = color.rgb2gray(io.imread('1.tif', 0))

##step-2 selecting required region if necessary
cropped_img = img[0:1422,:]
#plt.hist(img.flat, bins=100, range=(0,255))
ret, thresh = cv2.threshold(cropped_img, 162, 217, cv2.THRESH_BINARY)

#Step-3
kernel = np.ones((3,3),np.uint8)
eroded = cv2.erode(thresh, kernel, iterations = 1)
dilated = cv2.dilate(eroded, kernel, iterations = 1)
#cv2.imshow("Original Image", img)
#cv2.imshow("Threshold Image", thresh)
#cv2.imshow("Eroded Image", eroded)
#cv2.imshow("Dilated Image", dilated)
#cv2.waitKey(0)

#step-4

mask = thresh == 217
io.imshow(mask) #show the masked image 

请协助我测量遮罩区域的尺寸。特别是所有遮罩区域的feret直径

我附上了遮罩粒子的图像。Binary image consisting of particles that have been masked


Tags: io图像imageimportimgasstepnp