DICOM to grayscale in matlab - matlab

How can I convert a DICOM image into a grayscale image in matlab?
Thanks.

I'm not sure what specific DICOM image you are talking about that is not grayscale already. The following piece of code demonstrates how you read and display a DICOM image:
im = dicomread('sample.dcm'); % im will usually be of type uint16, already in grayscale
imshow(im, []);
If you want a uint8 grayscale image, use:
im2 = im2uint8(im);
However to keep as much of the precision as possible, it's best to do:
im2 = im2double(im);
To stretch the limits temporarily only when displaying the image, use:
imshow(im2, []);
To stretch the limits permanently (for visualization only not for analysis), use:
% im3 elements will be between 0 and 255 (uint8) or 0 and 1 (double)
im3 = imadjust(im2, stretchlim(im2, 0), []);
imshow(im3);
To write the grayscale image as jpg or png, use:
imwrite(im3, 'sample.png');
UPDATE
If your version of Matlab does not have im2uint8 or im2double, assuming that your DICOM image is always uin16 a quick workaround to convert the DICOM image to a more manageable format would be:
% convert uint16 values to double values
% im = double(im);
% keep the current relative intensity levels for analysis
% involving real values of intensity
im2 = im/2^16
% if displaying, create a new image with limits stretched to maximum
% for visualization purposes. do not use this image for
% calculations involving intensity value.
im3 = im/(max(im(:));

Related

Combine 2 images as a single planar image

I have two separate gray scale images im1 (Fig1) and im2 (Fig2) each of size 50 by 50 that are displayed here by color coding them. When I combine them together using cat() command and then display the result of concatenated images, they get displayed side by side (Fig3). However, if I create a third image by replicating either the first of the second image and then display the concatenation of the 3 images I get a single image (Fig4). I don't understand how the merging for the RGB (3 dimensions) could be possible whereas for the conversion to grayscale the merging did not happen. How can I get a single image using the two images im1 and im2 merged or overlayed whichever is legally possible and not side by side? Si how do I overlay im1 and im2 to get a single image and display it by color coding?
imgGray = cat(2,im1,im2);
imshow(imgGray)
imgGray = cat(2,im1,im2);
imshow(imgGray)
imagesc(imgGray)
im3=im1;
imgColor = cat(3,im1,im2,im3);
imagesc(imgColor)
You can also just add them to each other (after normalizing them) and have a single colormap represent it all
imagesc(I1+I2);
or if you want to set transparency according to the color and intensity you can add
alpha color
alpha scaled
You can to do it "manually":
Use ind2rgb for converting each Grayscale image to RGB with "color coding".
Compute average of two RGB images for getting "fused image".
Here is a code sample:
% Use cameraman as first image, and resized moon for the second image (just for example).
I1 = imread('cameraman.tif'); % I1 is uint8 Grayscale
I2 = imresize(imread('moon.tif'), size(I1)); % I2 is uint8 Grayscale
% Convert images to RGB using parula color map (you may choose any color map).
J1 = ind2rgb(I1, parula(256)); % J1 is double RGB (three color planes in range [0, 1]).
J2 = ind2rgb(I2, parula(256)); % J2 is double RGB (three color planes in range [0, 1]).
% Fuse J1 and J2 "manually".
alpah = 0.5; % Using alpah=0.5, gives average of J1 and J2.
K = J1*alpah + J2*(1-alpah); %K is is double RGB.
K = im2uint8(K); % Convert K to uint8 (im2uint8 multiplies pixels by 255, and convert to uint8).
%Display result
figure;imshow(K);
Result:

Contrast Stretching for colore images with MATLAB

i'm working in matlab and i wanted to apply the Contrast Stretching for grey scale image and also RGB image ,
so for the grey scale i've tried this one and it worked
clear all;
clc;
itemp = imread('cameraman.tif'); %read the image
i = itemp(:,:,1);
rtemp = min(i); % find the min. value of pixels in all the
columns (row vector)
rmin = min(rtemp); % find the min. value of pixel in the image
rtemp = max(i); % find the max. value of pixels in all the
columns (row vector)
rmax = max(rtemp); % find the max. value of pixel in the image
m = 255/(rmax - rmin); % find the slope of line joining point
(0,255) to (rmin,rmax)
c = 255 - m*rmax; % find the intercept of the straight line
with the axis
i_new = m*i + c; % transform the image according to new slope
figure,imshow(i); % display original image
figure,imshow(i_new); % display transformed image
this is for greyscale image ,
the problem is that that i don't know how to do for the RGB image
any idea? how to implement that?
thank you :)
Could the function stretchlim (reference) be useful for your purpose?
Find limits to contrast stretch image.
Low_High = stretchlim(RGB,Tol) returns Low_High, a two-element vector
of pixel values that specify lower and upper limits that can be used
for contrast stretching truecolor image RGB.
img = imread('myimg.png');
lohi = stretchlim(img,[0.2 0.8]);
If you write
rmin = min(i(:));
Then it computes the minimum over all values in i. This will work for RGB images also, which simply are 3D matrices with 3 values along the 3rd dimension.
The rest of your code also applies directly to such images.

what is the right way to compute the measures for images with different color properties

I need a little help guys in Matlab in Matrix Dimensions,
I Have two images imported by imread function:
im1 = imread('1.jpg');
im2 = imread('2.jpg');
im1 is the reference image, while im2 is the Noisy image.
In the workspace window, Matlab shows the im2 Dimensions like this: 768x1024x3
while im2 displayed as: 768x1024
They are both RGB, there's no greyscale images,
In fact the second image is the a compressed image (performed compression algorithm on it ) while the first image is natural JPEG Image, untouched
and for calculating MSE/PNSR for both images, the matrix dimensions must be the same.
I Will need to transform im1 dimensions to be 3d like the first image (768x1024)
I tried this functions (squeeze, reshape) and with no success
You were on the right track with repmat. Here's the correct syntax:
im2 = repmat(im2, [1 1 3]);
This says you want 1 replicate along the first dimension, 1 replicate along the second dimension, and 3 replicates along the third dimension.
Are you sure that both are RGB images because im2 has only one channel and it looks grayscale but it can also be a colormap image in that case try
[im2, map] = imread('im2.jpg');
and see if anything is appearing in map variable. If the image is indeed colormap image, the map variable should be of size 256 X 3.
What donda has suggested is repeating the grayscale channel 3 times to make it of size 768x1024x3. Another possibility is that noisy image was created by converting RGB image to grayscale or by taking green channel of RGB image. Verify the source of the image in that case.
About PSNR computation I have a feeling that there is some problem with your code. I have given my code below use this and see if it works. Get back to me if you face any problem.
function [Psnr_DB] = psnr(I,I_out)
I = double(I);
I_out = double(I_out);
total_error = 0;
for iterz = 1:size(I,3)
for iterx = 1:size(I,1)
for itery = 1:size(I,2)
total_error = total_error + (I(iterx,itery,iterz)-I_out(iterx,itery,iterz))^2;
end
end
end
MSE = total_error/numel(I);
Psnr = (255^2)/MSE;
Psnr_DB = 10*log10(Psnr) %#ok<NOPRT>

convert any image type to RGB using MATLAB

I have an image I get it using this code
[path,user_cance]=imgetfile();
im=imread(path);
Now I don't know if this image is RGB or indexed or ...
How to convert im to RGB for example ?
If it is indexed image you can easily use ind2rgb function:
read the image:
[X,map] = imread('imagefile.tif');
Verify that the colormap, map, is not empty, and convert the data in X to RGB.
if ~isempty(map)
Im = ind2rgb(X,map);
end
finally you can View the size and class of X.
whos Im
ind2rgb converts the matrix X and corresponding colormap map to RGB (truecolor) format.
X can be of class uint8, uint16, single, or double. RGB is an m-by-n-by-3 array of class double.
here you can find the image formats that you can read using MATLAB.
According to Mathworks documentation (http://www.mathworks.fr/fr/help/matlab/ref/imread.html), imread can infer the data type from its content.
You could do test if the colormap exists or is empty :
[im, map] = imread(path_to_image);
if(isempty(map)) % image is RGB or grayscale
if(size(im, 3) == 1) % image is grayscale
im = cat(3, img, img, img);
end
else % image is indexed
im = ind2rgb(im, map);
end
% now 'im' is a RGB-image

problems with imshow() and rgb2gray() in MATLAB

Good afternoon,
when running the following code:
testImage = double(imread(testfile));
figure; imshow(testImage)
greyTestImage = rgb2gray(testImage);
figure; imshow(greyTestImage)
I get unclear and mostly blank Images, I was unable to fx it using the colormap. The following is teh original image and the two resulting figures:
1:
2:
3:
You have to know the format of the image you are trying to read. To make this sure, I always use the following piece of code in my programs when I want to convert user defined image from unknown format to uint8 grayscale :
% load image
[filename, pathname] = uigetfile({'*.*'},'image file');
fullFilename = [pathname filename];
% Get image info, read it accordingly
info = imfinfo(fullFilename);
if(strcmp('truecolor',info.ColorType))
I = imread(fullFilename);
Igray = uint8(rgb2gray(I));
clear I
elseif(strcmp('grayscale',info.ColorType))
Igray = uint8(imread(fullFilename));
elseif(strcmp('indexed',info.ColorType))
[I,map] = imread(fullFilename);
Igray = uint8(ind2gray(I,map));
clear I map
else
error('statPart:FormatImage','Image format error');
end
clear info
Also, this : testImage = double(imread(testfile)); won't work if you assume testfile is uint8 and want to convert it to double (in double intensity shall range from 0 to 1). You have to do testImage = double(imread(testfile)) / 255;
Hope this help.
Cheers
Have you tried converting to 8 bit unsigned integers?
testImage = double(imread('PEYXW.jpg'));
figure; imshow(uint8(testImage))
greyTestImage = rgb2gray(uint8(testImage));
figure; imshow(greyTestImage)
This is probably an issue with the image you are loading.
The input to the rgb2gray function needs to be a 3-channel color image (MxNx3 matrix for the R,G,B channels).
If you use a built-in Matlab sample image, you can effectively do:
imdata = imread('ngc6543a.jpg');
figure; imshow(imdata)
Then:
test = rgb2gray(imdata);
Actually, Matlab provides the function Im2double instead of double to convert images to double precison:
testImage = im2double(imread(testfile));
figure; imshow(testImage)
greyTestImage = rgb2gray(testImage);
figure; imshow(greyTestImage)
In double precision, image RGB values are between 0 and 1, for uint8 they range between 0 and 127. When you use the double function, all RGB values keep their value, but they are in double precision. This means that any RBG tiple with values >= 1 will result in a double >= 1 and thus produce a white space in your image.
im2doubleactually scales the values to the range [0 1].