How to set the display range for this medical DICOM images - image

Here is the code that i used to display the DICOM image, When i give the full display range it shows burred image like left, and when i increase the lower display range the image looks more clear.
[img, map] = dicomread('D:\Work 2017\Mercy\trial\trial4\Export0000\MG0001.dcm');
info = dicominfo('D:\Work 2017\Mercy\trial\trial4\Export0000\MG0001.dcm' );
mini = min(img(:));
maxi = max(img(:));
figure,
subplot(131), imshow(img, [mini maxi]); title('Full display range')
subplot(132), imshow(img, [maxi*0.7 maxi]); title('70% and above display range')
subplot(133), imshow(img, [maxi*0.8 maxi]); title('80% and above display range')
I want to always see an image something similar to right side image without giving the display range that I used in the above code

Typically a DICOM will have WindowCenter and WindowWidth tags that specify the recommended window/level settings. You can convert these to color limits in the following way
% Get the DICOM header which contains the WindowCenter and WindowWidth tags
dcm = dicominfo(filename);
% Compute the lower and upper ranges for display
lims = [dcm.WindowCenter - (dcm.WindowWidth / 2), ...
dcm.WindowCenter + (dcm.WindowWidth / 2)];
% Load in the actual image data
img = dicomread(dcm);
% Display with the limits computed above
imshow(img, lims);
Or more briefly
lims = dcm.WindowCenter + [-0.5 0.5] * dcm.WindowWidth;
If those values aren't acceptable, then it's likely best to provide a user-adjustable window/level (such as the contrast tool in imtool) as there is unlikely any way to reliably get "acceptable" contrast since it is subjective.

Related

Auto-brightening images

I found this code for auto-brightening images to an optimum level.
% AUTOBRIGHTNESS
% -->Automatically adjusts brightness of images to optimum level.
% e.g. autobrightness('Sunset.jpg','Output.jpg')
function autobrightness(input_img,output_img)
my_limit = 0.5;
input_image=imread(input_img);
if size(input_image,3)==3
a=rgb2ntsc(input_image);
else
a=double(input_image)./255;
end
mean_adjustment = my_limit-mean(mean(a(:,:,1)));
a(:,:,1) = a(:,:,1) + mean_adjustment*(1-a(:,:,1));
if size(input_image,3)==3
a=ntsc2rgb(a);
end
imwrite(uint8(a.*255),output_img);
I want to ask, why the value of my_limit is 0.5?
How we determine that value?
Why use the 'ntsc' colorspace instead of another colorspace like hsv, lab or yCbCr?
I want to ask, why the value of my_limit is 0.5? How we determine that
value?
The color space NTSC ranges from 0 to 1 for each of its channel. So essentially 0.5 is the center. This is equivalent of choosing 127 for RGB space
Why use the 'ntsc' colorspace instead of another colorspace like hsv,
lab or yCbCr?
I believe ntsc provides 100% coverage of the color space and so the author of the code choose it. However most modern systems wont display in this color space and hence we use standard RGB for display. I used this website to come to this conclusion NTSC color space
Also, as pointed by Cris in this wikipedia page. NTSC stores Luminance and Chrominance and the author of the code is adjusting the Lumiance(brightness). I am including a modified script I used to come to these conclusions
input_img='lena_std.tif'
output_img='lena2.tif'
my_limit = 0.5;
input_image=imread(input_img);
if size(input_image,3)==3
a=rgb2ntsc(input_image);
k=rgb2ntsc(input_image);
else
a=double(input_image)./255;
end
mean_adjustment = my_limit-mean(mean(a(:,:,1)));
a(:,:,1) = a(:,:,1) + mean_adjustment*(1-a(:,:,1));
if size(input_image,3)==3
a=ntsc2rgb(a);
end
imwrite(uint8(a.*255),output_img);
output=uint8(a.*255);
imwrite(uint8(k.*255),'test.tif');
ntscoutput=uint8(k.*255);

What is the syntax of ImageResize()

I have data that change in size and want to display them in the same window. The command
void ImageResize( BasicImage im, Number num_dim, Number... )
seems like a potential fit, but the syntax is not clear at all.
Let's say I have 512x5 data set and now it needs to be 367x5.
The , Number...) indicates that this command takes a different number of parameters, all of them interpreted as number parameters. Commands which do this, usually use one of their other parameters to specify how many such parameters follow.
A typical example for this is also the SliceN command.
In this particular case, the command not only allows you to change the size of the dimensions in the image, but also the number of dimensions. It is a very useful command to f.e. change a 2D image into a 3D stack or the like.
The command ImageResize( BasicImage im, Number num_dim, Number... ) does several things:
It replaces im in-place, so the meta-data, display and window remains the same
It adjusts the dimension calibration when the dimension size is changed. Here, the assumption is, that the field-of-view before and
after the resize is the same. (The command can be used to easily scale
images as shown in the example below.)
All values of the image im are set to zero. ( If you need to keep the values, you need to act on an image clone!)
Example 1: Resizing image with bilinar interpolation
image before := GetFrontImage()
number sx, sy
before.GetSize(sx,sy)
number factor = 1.3
image after := before.ImageClone()
after.ImageResize( 2, factor*sx, factor*sy ) // Adjusts the empty container with meta-data
after = warp(before, icol/factor, irow/factor ) // interpolate data
after.ShowImage()
Example 2: Extend 2D image into 3D stack
number sx = 100
number sy = 100
image img := RealImage("2D",4,sx,sy)
img = iradius* Random()
img.ShowImage()
OKDialog("Now into a stack...")
number sz = 10
img.ImageResize(3,sx,sy,sz) // All values are zero now!
img = iradius * Random()

How to transfer the .mat file to image file without any change?

I have a image.mat of about 4MB.
The size of some image file can also be 4MB.
Can the image.mat be transferred to image file?
I tried this, but that doesn't do the trick:
load image.mat %load Iw
imshow(mat2gray(Iw))
imwrite(Iw,'image.png');
IwNew = imread('image.png');
isequal(Iw,IwNew)
The result is 0; am I misunderstanding something?
The number in Iw are very important, so Iw can not be changed.
Actually my real problem is how to store float numbers into an image?
But MATLAB does not support Tiff 6.0, so I'll have to find some workaround.
I am doing a blind watermarking,and the decimal fraction of a number in Iw is important because it involve the information about another image.So the Iw can not be changed.
Actually,Mathematica can store floating floating-point data:
But my programs are all in MATLAB.
According to Matlab documentation:
"If A is a grayscale or RGB color image of data type double or single, then imwrite assumes that the dynamic range is [0,1] and automatically scales the data by 255 before writing it to the file as 8-bit values."
In other words: imwrite performs automatic conversion from double to uint8.
if you wish to keep the values of Iw unchanged, save it as a mat file and not as an image.
If you do want to save it as an image - there is going to be some loss of information. In this case, there are two things which need to be done:
Change the dynamic range of the matrix to [0,1]. (in your case, the range is between -0.0035 to 255.0035. Also, the matrix contain inf values).
If you want to get an equality, scale IwNew by 255, and convert it to uint8.
Code:
load image.mat %load Iw
%step 1, change the dynamic range of the image to [0,1].
%One way to do it is by using mat2gray on each channel separately.
Iw(:,:,1) = mat2gray(Iw(:,:,1));
Iw(:,:,2) = mat2gray(Iw(:,:,2));
Iw(:,:,3) = mat2gray(Iw(:,:,3));
%write the image to file
imwrite(Iw,'image.png');
%read the image
IwNew=imread('image.png');
%scale it, and convert to uint 8
Iw2 = uint8(Iw*255);
%check equality
isequal(Iw2,IwNew)
Result:
ans =
1
Alternatively, if you want to convert IwNew to double, perform the following:
%conversion to double
Iw2 = double(IwNew)/255;
Notice that in this case, the matrices won't be equal to one another,
Due to the loss of information which happened during the imwrite process (conversion from double to uint8).
Instead, they will be epsilon-close to one another, where epsilon = 0.0001.
In order to test this, write the following:
%equality check
sum(abs(Iw2(:)-Iw(:))>0.0001)
Result:
ans =
0
My MATLAB (R2010a) with the image processing toolbox is perfectly capable of storing double-valued pixel values, and retrieve them without loss of data.
Here's a shameless copy of this answer:
% Some random, data of type double
A = 7.6*rand(10);
% Construct TIFF image...
t = Tiff('test.tif', 'w');
% ...with these custom parameters...
tagstruct = struct(...
'ImageLength' , size(A,1),...
'ImageWidth' , size(A,2),...
'Compression' , Tiff.Compression.None,...
'SampleFormat' , Tiff.SampleFormat.IEEEFP,... % floating point
'Photometric' , Tiff.Photometric.MinIsBlack,...
'BitsPerSample' , 64,... % 8 bytes / double
'SamplesPerPixel' , 1,...
'PlanarConfiguration', Tiff.PlanarConfiguration.Chunky);
t.setTag(tagstruct);
% ...and write it to disk.
t.write(A);
t.close();
% Read the data actually written, and check if all
% information was indeed preserved:
B = imread('test.tif');
isequal(A,B)
Result:
ans =
1
Adjust in obvious ways if you have more than 1 channel (RGB).

Change Range Of Displayed Pixel Values from Command Line?

I'm working with some MRI data in Matlab 2014b, but the data is formed of intensity values not RGB. To get around this I use the code below to form a movie out of the MRI frames (I'm working on dynamic data here)
My problem is that the images need to have altered display values for the pixels, as the default only displays between -Inf and Inf, and I need between 0 and 0.25 to get a sensible image out of my data.
Are there any ways to pass that change from the script in to the movie, and then to write to file? I can only seem to do this per image in implay, and I'd like an automated way to edit each image and then store as a frame for a movie..?
%Code for producing movie.
graymap = gray(256);
for i = 1:32
a(:,:,i) = cmunique(Reformed_Data_Colourmap(:,:,i));
end
for i = 1:32
b = im2frame(a(:,:,i),graymap);
a(:,:,1) = ((b.cdata));
image(a(:,:,1))
colormap 'gray'
%The change needs to be here, to display pixel values from 0 to 0.25, to allow for a sensible image from the MR data.
frames(1,i) = getframe;
end
movie(frames)
The solution is provided:
for i = 1:32
b = im2frame(a(:,:,i),graymap);
a(:,:,1) = ((b.cdata));
clims = [0 250];
%image(a(:,:,1),clims)
colormap 'gray'
imagesc(a(:,:,1),clims);
%set('window', [0 400])
frames(1,i) = getframe;
end
clims solves the issue.

Removing Noise From an image in MATLAB

I'm using Geometric mean filter to remove noise instead of median filter as image quality goes off in former case. code shown below is a part of m-file to remove noise.
fname = getappdata(0, 'fname');
[a, map] = imread(fname);
x = ind2rgb(a, map);
b = im2double(x);
w=fspecial('gaussian',[3,3]);
geom=exp(imfilter(log(b),ones(3,3),'replicate')).^(1/3/3);
fname=imfilter(b,w,'replicate');
axes(handles.axes1);
imshow(fname);
If i press push button which is named 'Remove Noise' above code get executed irrespective of image quality/property. In the sense even if no noise is present, image will be subjected to filter.
My question, is there any way to detect whether noise is present or not, so that the moment i pressed push button if no Noise in image it should display a message stating 'NO NOISE TO REMOVE' automatically.
There is no direct way to determine whether an image is noisy or not.
However, you can compare resulting image fname with the input image b in such a way that if their difference is lower than a certain threshold, then it can be decided that denoising operation has not changed image effectively and there is not much noise in the original image like:
threshold_ratio = 0.2; % to be decided experimentally
difference_ratio = sum(sum((fname - b).^2)) / sum(sum(b.^2));
if difference_ratio < threshold_ratio
disp('NO NOISE TO REMOVE');
end

Resources