Matlab:从图像

时间:2015-06-20 23:42:38

标签: matlab image-processing

我想只去除白细胞并保留下面图像中的红细胞。在Matlab中执行此操作的最佳方法是什么? blood cells

format longg;
format compact;
fontSize = 16;
rgbImage = imread('E:\GP_Final\DS\CL_13-09_image2.jpg');
[rows columns numberOfColorBands] = size(rgbImage);
% imshow(rgbImage, []);

% title('Original Color Image', 'FontSize', fontSize);
hsv = rgb2hsv(rgbImage);
% figure(2),imshow(hsv);
% Display the color channels.
hueImage = hsv(:, :, 1);
saturationImage = hsv(:, :, 2);
valueImage = hsv(:, :, 3);
subplot(2, 2, 2);
imshow(hueImage, []);
title('Hue Channel', 'FontSize', fontSize);
subplot(2, 2, 3);
imshow(saturationImage, []); 
title('Saturation Channel', 'FontSize', fontSize);
subplot(2, 2, 4);
imshow(valueImage, [])
title('Value Channel', 'FontSize', fontSize);
[pixelCounts values] = hist(hueImage, 500);
 figure;
 bar(values, pixelCounts);
title('Histogram of Hue Channel', 'FontSize', fontSize);
redPixels = hueImage > 0.3 & hueImage >0.8 & valueImage <= 0.9;
% figure(10);
% imshow(redPixels);
% title('Map of red Pixels', 'FontSize', fontSize);
saturationImage(redPixels) = saturationImage(redPixels) *3.5;
% figure(7),imshow(saturationImage);
% title('New Saturation Channel', 'FontSize', fontSize);

% Combine back to form new hsv image
hsvImage = cat(3, hueImage, saturationImage, valueImage);
% Convert back to RGB color space.
rgbImage = hsv2rgb(hsvImage);
figure(8), imshow(rgbImage);
title('RGB Image with Enhanced red', 'FontSize', fontSize);

se1 = strel('disk',1);
 erodedBW = imerode(redPixels,se1);
se2 = strel('disk',2);
 dilatedBW2 = imdilate(erodedBW,se2);
se3 = strel('disk',1);
 openedBW = imopen(dilatedBW2,se3);

 filledBW=imfill(openedBW,'holes');
 figure(3), imshow(filledBW);title('after fill holes  ');


bw3=bwareaopen(filledBW,80);
    figure(5), imshow(bw3);title('after remove small objects ');

这是我做的但它不适用于所有图像,有什么方法可以解决它吗?

2 个答案:

答案 0 :(得分:4)

您希望移除图像中间的深紫色单元格。查看HSV colour space中的颜色分布是一项非常简单的任务。在这种情况下我不会看色调,因为图像的颜色分布很可能具有相似的色调。饱和度是我的目标。

让我们从StackOverflow中读取图像,将图像转换为HSV并查看饱和度分量:

im = imread('http://i.stack.imgur.com/OQUKj.jpg');
hsv = rgb2hsv(im2double(im));
imshow(hsv(:,:,2))

我们得到这张图片:

enter image description here

您可以清楚地看到“红色”血细胞的饱和度高于背景,因此我们可以进行一些简单的阈值处理。饱和度0.4似乎对我有用:

mask = hsv(:,:,2) > 0.4;
imshow(mask);

我们得到了这个:

enter image description here

有一些虚假像素,因此我们可以通过bwareaopen操作删除它。任何面积低于300的像素我都会删除:

mask_remove = bwareaopen(mask, 300);
imshow(mask_remove);

我们得到了这个:

enter image description here

现在,剩下的细胞中有洞。我们可以使用imfill填写这些漏洞并选择holes选项来解决此问题:

mask_fill = imfill(mask_remove, 'holes');
imshow(mask_fill);

我们得到了这个:

enter image description here

我要稍微扩大这个面具,以确保我们摆脱其余的紫色环境:

se = strel('square', 7);
mask_final = imdilate(mask_fill, se);

最后要做的是使用此蒙版并遮盖原始图像,然后生成最终图像并移除白细胞。只需将掩模反转并将其与原始图像相乘,然后用白色填充缺失的信息:

mask_final = repmat(mask_final, [1 1 3]);
out = im .* uint8(~mask_final);
out(mask_final) = 255;
imshow(out);

我们得到了这个:

enter image description here

答案 1 :(得分:-2)

您可以通过将图像拖入工作区或单击Matlab中的“导入”按钮来导入图像。这将为您提供宽度x高度x 3矩阵,其中包含每像素的RGB值。通过将红色,绿色和蓝色值阈值化,您可以选择要编辑的像素。使用imview()imsave(),您可以查看和存储结果。

要编辑多个图像,您可以使用(将png替换为您拥有的扩展名):

fileNames = [cellstr('name1'), cellstr('name2')]; % The images to process without extension.
for i = 1:length(fileNames)
  img = imread([char(fileNames(i)), '.png']);
  % Process the image here.
  % ...
  imwrite([char(fileNames(i)), '-edited.png'], 'png');
end
相关问题