2015-06-20 148 views
1

我只想從下面的圖像中刪除白細胞並保留紅細胞。在Matlab中做到這一點的最好方法是什麼? blood cellsMatlab:從圖像中刪除白細胞

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

您可以通過將圖像拖入工作區或單擊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 
+0

我有一個以上的圖像,我不t想要手動執行,我想輸入圖像,輸出將進入另一個處理步驟和計數,你能幫助我嗎? –

+0

我添加了允許您自動加載並保存多個圖像的代碼。 – Pieter12345

+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

+0

對不起,但白血細胞不是背景它的圖像中間有奇怪的顏色,它具有不同的顏色我在此標記鏈接https://fbcdn-sphotos-ha.akamaihd.net/hphotos-ak-xpt1/v/t1.0-9/p720x720/11351304_670217563112162_6449690187812091844_n.jpg?oh=55c18366748434df0d5a6e3885ba38d0&oe=56293B2C&__gda__=1444965642_f3e46cf56b64d0fab2e7809a511811a6 –

+0

@SamiRaSaǮed現在就來試試 – rayryeng

+0

感謝你的答案,它只適用於這個圖像,但對不起,我有一些圖像,我想輸入任何圖像,只提取紅細胞進入另一個處理階段作爲計數。 他們的圖像。 我想要一個任何血液圖像的代碼。 http://imgur.com/a/deJra –