Python粒子图像分析:检测过多微小区域的问题排查
粒子图像分析问题排查求助
背景与需求
- 正在从ImageJ转向使用Python处理粒子图像(涵盖台面上的静态粒子或下落中的动态粒子),已编写首版代码实现以下功能:
- 读取粒子图像
- 通过阈值分割与降噪生成二值图像
- 应用分水岭算法分离重叠粒子
- 检测区域并测量,获取粒径分布信息(如面积、周长、轴长等)
- 当前测试样本为台面上粒径0.5-1mm的粒子图像;后续还需处理粒径0.25-0.5mm、需要使用分水岭算法的粒子图像
问题现象
- 运行Python代码后得到的粒子面积分布中,0.0-0.5mm²区间存在大量实际不存在的微小区域
- 使用ImageJ处理同一样本则无此问题
已尝试措施
调整阈值分割、降噪、形态学操作及区域测量的相关参数,问题始终未解决
代码实现
import cv2 import numpy as np import pandas as pd import os from skimage.segmentation import watershed, clear_border from skimage import measure, color, io, morphology # Define the folder path containing the images image = "C:/..." # Set scale pixels/mm (either based on ImageJ or by reference image, or calculations) px_per_mm = 222 mm_per_px = 1 / px_per_mm img = cv2.imread(image) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #Threshold and remove noise thresholded_img = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] kernel = np.ones((5, 5),np.uint8) image = cv2.morphologyEx(thresholded_img, cv2.MORPH_OPEN, kernel) image = clear_border(image) # Prepare for Watershed sure_bg = cv2.dilate(image,kernel, iterations=4) dist_transform = cv2.distanceTransform(image, cv2.DIST_L2, 0) ret2, sure_fg = cv2.threshold(dist_transform, 0.4 * dist_transform.max(), 255, 0) sure_fg = np.uint8(sure_fg) unknown = cv2.subtract(sure_bg, sure_fg) ret3, markers = cv2.connectedComponents(sure_fg, connectivity=8) markers = markers + 10 markers[unknown == 255] = 0 # Now we are ready for watershed filling. markers = cv2.watershed(img, markers) #Measure properties label_image = measure.label(markers,background=255, connectivity=None) props = measure.regionprops_table(label_image, image, properties=['label', 'Area']) # Scale properties from pixels to mm props['Area'] = props['Area'] * mm_per_px ** 2 # Remove the image frame that is usually identified as a region max_area_threshold = 700 props_df = pd.DataFrame(props) filtered_props = props_df[props_df['Area'] <= max_area_threshold] # Convert the filtered properties to a DataFrame particle_analysis = pd.DataFrame(filtered_props)
内容的提问来源于stack exchange,提问作者Ant_C
相关产品推荐
相关产品推荐

