SamiRa SaǮed
SamiRa SaǮed

Reputation: 11

Matlab: Removing white cells from image

I would like to only remove white blood cells and keep red blood cells from the image below. What is the best way to do this in 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 ');

that is i did but it not work for all images, there is any way to solve it ?

Upvotes: 1

Views: 1201

Answers (2)

rayryeng
rayryeng

Reputation: 104504

You wish to remove out the deep purple cell in the middle of the image. It's a very simple task in looking at the colour distribution in the HSV colour space. I wouldn't look at the hue in this case because the colour distribution of the image will most likely have similar hues. The saturation is what I would aim for.

Let's read in the image from StackOverflow, convert the image to HSV and look at the saturation component:

im = imread('https://i.sstatic.net/OQUKj.jpg');
hsv = rgb2hsv(im2double(im));
imshow(hsv(:,:,2))

We get this image:

enter image description here

You can clearly see that the "red" blood cell has a higher saturation than the background, and so we can do some simple thresholding. A saturation of 0.4 seems to work for me:

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

We get this:

enter image description here

There are some spurious pixels, so we can remove this with a bwareaopen operation. Any pixels whose areas are below 300 I remove:

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

We get this:

enter image description here

Now, there are holes in this remaining cell. We can remedy this by filling these holes using imfill and selecting the holes option:

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

We get this:

enter image description here

I'm going to dilate this mask slightly to make sure we get rid of the rest of the purple surroundings:

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

The last thing to do is to use this mask and mask the original image, then produce a final image with the white blood cell remove. Simply invert the mask and multiply this with the original image, then fill in the missing information with white:

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

We get this:

enter image description here

Upvotes: 4

Pieter12345
Pieter12345

Reputation: 1789

You can import the image by dragging it into the workspace or clicking the 'import' button in Matlab. This will give you a width x height x 3 matrix which contains the RGB values per pixel. By thresholding the red, green a blue values, you can select which pixels to edit. Using imview() and imsave(), you can view and store the result.

To edit multiple images, you can use (replace png with the extension you have):

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

Upvotes: -2

Related Questions