Display gray color only from grayscale image

Hi all
Apology for my ignorance as this is quite new to me.
I'm currently working on MRI images and have tried numerous times using some thresholding methods to display gray area of MRI image/s but to no avail.
Any pixel with grayscale value (0-255) of less than 5 & more than 250 will be eliminated and thus only showing pixels within 6-249.
Any helps are much appreciated.
Regards Demon

2 comentarios

Walter Roberson
Walter Roberson el 17 de En. de 2013
The places where those pixels were: do you need those places to be transparent?
Demons
Demons el 17 de En. de 2013
transparent or black if that matter.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 17 de En. de 2013
You can alter the image to mask out (set to zero) those pixels with gray levels outside 5 and 250 inclusive:
darkPixels = grayImage <= 5;
lightPixels = grayImage >= 250;
pixelsToExclude = darkPixels | lightPixels;
grayImage(pixelsToExclude) = 0;
Is that what you want to do?

10 comentarios

Demons
Demons el 17 de En. de 2013
hi bud
thanks man it worked. any idea to fill in the hole within gray area to get it's previous value (black or white) for image analysis?
Image Analyst
Image Analyst el 17 de En. de 2013
Not exactly sure what that means, but possibilities are using roifill() or imfill(). If you explain more I can give you a better answer.
If you fill the hole with the previous value, then would the result not be the original image?
Or are you processing the filtered image, and then later want to put the pixels outside the ROI back to what they used to be? If so then keep the grayImage around, and:
displayImage = filteredImage;
displayImage(pixelsToExclude) = grayImage(pixelsToExclude);
Demons
Demons el 18 de En. de 2013
Walter Roberson
Walter Roberson el 18 de En. de 2013
That almost seems to be suggesting that you don't really want everything to be thresholded.
Image Analyst
Image Analyst el 18 de En. de 2013
I'm still not really sure what is wanted at every step of the way. Please try your best to give a really detailed description. And there should be at least 3 images: the original, the filtered, and the final image where you "fill in the hole within gray area to get it's previous value (black or white) for image analysis" And please define "gray area" a lot better.
Demons
Demons el 18 de En. de 2013
the original image above is input image, where the gray area represents muscles, outer white area - adipose tissue and outer gray/black lining - skin. by using your code above, i'm able to remove adipose tissue and inner bone section (which i don't want it to be removed) as can be seen in filtered image above.
here's the output image i intended it to be projected: http://www.imagesup.net/?di=6135854242712
this can be done manually but a really time consuming process especially when involving thousands of images
Here, run this demo and see what it does. I think you'll like it. I believe it does what you asked me for. Copy, paste, change the filename and folder, then run.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
format compact
% Read in a demo image.
folder = 'C:\Users\Mark\Documents\Temporary';
baseFileName = 'bodyslice.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage)
if numberOfColorBands > 1
% Convert to grayscale.
grayImage = rgb2gray(grayImage);
end
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
% Suppress the lowest bin
pixelCount(1) = 0;
subplot(2, 3, 2);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Threshold the image.
binaryImage = grayImage >= 200;
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Get their areas
labeledImage = bwlabel(binaryImage);
blobMeasurements = regionprops(binaryImage, 'Area');
% Now I'll demonstrate how to select certain blobs based using the ismember function.
% Let's say that we wanted to find only thh largest blob.
allBlobAreas = [blobMeasurements.Area];
% Get a list of the blobs that meet our criteria and we need to keep.
[areaOfLargestBlob, labelOfLargestBlob] = max(allBlobAreas)
% Extract only those blobs that meet our criteria, and
% eliminate those blobs that don't meet our criteria.
% Note how we use ismember() to do this.
keeperBlobsImage = ismember(labeledImage, labelOfLargestBlob);
% Display the image.
subplot(2, 3, 4);
imshow(keeperBlobsImage, []);
title('Largest Blob in Binary Image', 'FontSize', fontSize);
% Let's fill interior holes.
binaryImage = imfill(keeperBlobsImage, 'holes');
% Display the image.
subplot(2, 3, 5);
imshow(binaryImage, []);
title('Binary Image, filled and dilated', 'FontSize', fontSize);
% Get rid of everything outside the largest blob
% by taking the inverse of the convex hull
convexHull = bwconvhull(binaryImage);
% We'll hang on to this. We will use it later on the gray scale image.
% Let's enlarge it a little.
binaryImage = imdilate(binaryImage, true(31));
% Display the image.
subplot(2, 3, 5);
imshow(binaryImage, []);
title('Binary Image, filled and dilated', 'FontSize', fontSize);
% Combine convex hull and largest blob to get the pixels to erase.
pixelsToErase = ~convexHull | binaryImage;
% Display the image.
subplot(2, 3, 6);
imshow(pixelsToErase, []);
title('Pixels to Erase', 'FontSize', fontSize);
promptMessage = sprintf('Now we will erase these pixels in the lower right from the original image.');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'OK', 'Cancel', 'OK');
if strcmpi(button, 'Cancel')
return;
end
% Use this binary image to mask out the original image.
grayImage(pixelsToErase) = 0;
% Display the image.
subplot(2, 3, 6);
imshow(grayImage, []);
title('Gray Image, no Adipose', 'FontSize', fontSize);
uiwait(helpdlg('Done with demo! Thanks Image Analyst!'));
Demons
Demons el 18 de En. de 2013
One word man - WOW!
This will save at least a good few weeks of my life. Really appreciate your effort.
If you happen to drop in Manchester someday, let me know.
Image Analyst
Image Analyst el 19 de En. de 2013
You're welcome. The closest I get is Newcastle. But good luck with your project.

Iniciar sesión para comentar.

Más respuestas (2)

Amith Kamath
Amith Kamath el 17 de En. de 2013
For most tasks of analysing pixel values and thresholding, I've found http://www.mathworks.com/matlabcentral/fileexchange/6770 very useful.
You could use:
[levelLow bwLow] = thresh_tool(IM);
[levelHi bwHi] = thresh_tool(IM);
%to get the mask for each of the lower and upper level in bw.
depending on the datatype of your image, you can then just do an and operation between your image and the bw images.

1 comentario

Demons
Demons el 17 de En. de 2013
Hi buddy
Thanks for your response. Appreciate that.
But gosh that was huge! Have you any idea of a much simpler one?

Iniciar sesión para comentar.

Jurgen
Jurgen el 17 de En. de 2013
imshow(I,[6 249])
if it's a 2D image slice you want to visualize.

1 comentario

Demons
Demons el 17 de En. de 2013
hi bud
i've tried that before and nothing changed.

Iniciar sesión para comentar.

Categorías

Más información sobre Images en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 17 de En. de 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by