MATLAB 图像处理工具箱 + 细胞计数

4 阅读2分钟

我直接给你一套 **MATLAB 图像处理工具箱 + 细胞计数 **,适配 MATLAB R2025a,带详细注释。

一、细胞计数完整代码(直接运行)

%% 细胞计数 - MATLAB 图像处理完整流程
clear; clc; close all;

% 1. 读取图像(换成你的细胞图片路径)
img = imread('cells.jpg');  % 支持 jpg、png、tif 等
figure; imshow(img); title('原图');

% 2. 转灰度图
gray = rgb2gray(img);
figure; imshow(gray); title('灰度图');

% 3. 去噪(高斯滤波,让细胞边界更清晰)
gray = imgaussfilt(gray, 1.5);

% 4. 自适应阈值二值化(适合细胞这种光照不均的图)
bw = imbinarize(gray, 'adaptive', 'ForegroundPolarity', 'dark');
figure; imshow(bw); title('二值化图像');

% 5. 形态学处理:去除小噪点,填充细胞内部小孔
se = strel('disk', 2);    % 结构元素
bw = imopen(bw, se);      % 开运算:去噪点
bw = imclose(bw, se);     % 闭运算:填小孔
figure; imshow(bw); title('形态学处理后');

% 6. 细胞分割:分离粘连细胞(分水岭算法)
D = bwdist(~bw);         % 距离变换
D = -D;
L = watershed(D);        % 分水岭分割
bw(L == 0) = 0;          % 去掉分割线
figure; imshow(bw); title('分水岭分割后');

% 7. 统计细胞数量 & 标记
[labeled, num_cells] = bwconncomp(bw);  % 连通域分析
cells = label2rgb(labeled, 'jet', 'k'); % 彩色标记

% 8. 显示结果
figure; imshow(cells);
title(['细胞总数:', num2str(num_cells), ' 个']);

% 9. 绘制细胞中心点(可选)
stats = regionprops(labeled, 'Centroid');
hold on;
for i = 1:num_cells
    c = stats(i).Centroid;
    plot(c(1), c(2), 'r+', 'MarkerSize', 10, 'LineWidth', 2);
end
hold off;

二、使用步骤(超简单)

  1. 把你的细胞照片放到 MATLAB 当前文件夹
  2. 把代码里的 cells.jpg 改成你的图片名
  3. 直接运行
  4. 自动输出:细胞总数 + 彩色标记图 + 中心点标记

三、MATLAB Copilot 细胞计数提示词(一键生成代码)

直接复制到 Copilot:

使用MATLAB图像处理工具箱写细胞计数代码:
1. 读取图像
2. 灰度化、去噪
3. 自适应二值化
4. 形态学处理
5. 分水岭分割解决粘连细胞
6. 连通域计数
7. 显示结果并标记细胞位置
代码带注释,可直接运行

四、关键函数说明(一看就懂)

函数作用
rgb2gray彩色转灰度
imgaussfilt去噪
imbinarize二值化(分出细胞和背景)
imopen/imclose去噪点、填小孔
watershed分割粘连细胞
bwconncomp统计细胞数量(核心)
regionprops获取细胞中心坐标