How to save DICOM image as JPEG without losing information - matlab

I have a dicom image that when I open it in MATLAB it is like this:
However when I see that via dicomviewer it is like this:
How can I save these dicom images without loosing their information in .jpeg format due to compression process?
I want to save the image so that I can retrieve the same information as I get from the respective dicom image.
Is it possible?

DICOM image data is typically stored as 16-bit unsigned integers, so you'll want to make sure that your image is stored within a uint16 matrix prior to saving so MATLAB knows to save it as such. Also, for some image formats, MATLAB requires that we explicitly state the bit depth.
% Save as a 16-bit Baseline JPEG with the highest quality
imwrite(uint16(data), 'image.jpg', 'Quality', 100, 'BitDepth', 16);
% Save as a 16-bit Lossless JPEG
imwrite(uint16(data), 'image.jpg', 'Mode', 'lossless', 'BitDepth', 16)
% Save as a 16-bit JPEG 2000 Image
imwrite(uint16(data), 'image.jp2', 'Mode', 'lossless')
If you don't need a JPEG for any particular reason, I would recommend a PNG (lossless).
% Save as 16-bit PNG
imwrite(uint16(data), 'image.png')
See the full list of available 16-bit formats here.
For visualization in MATLAB, you can specify the second input to imshow (or use imagesc) to automatically scale the displayed gray scale values to the data within the image
imshow(data, []) % or imagesc(data); axis image;

Related

How to read image data as a 2D grid using gnuplot

Gnuplot is a very powerful library that supports plotting of functions with numerous scientific operations. What my case is I want to read a single channel grayscale image just as we read in matlab or python using imread and store it into a 2D data grid using gnuPlot.
Basically I want to make contours of image gray scale intensities.To do that I am exporting the single channel luminance data of the image as a .dat file using matlab once it is exported I splot it using:
set contour base
splot 'greyScaleImagePixelByPixelData.dat' matrix
This works fine but in case I dont want to use Matlab to export the pixel by pixel data to surface plot the image what is the way around?
The example below has been tested with 8-bit and 16-bit grayscale png images (no alpha channel). If your particular images do not match this, please provide a more complete description of how they are encoded.
You haven't said exactly what you want to do with the pixel data after reading it in, so I show the obvious example of displaying it as an image (e.g. a regular array of pixels). If you want to do further manipulation of the values before plotting, please expand the question to give additional details.
[~/temp] file galaxy-16bitgrayscale.png
galaxy-16bitgrayscale.png: PNG image data, 580 x 363, 16-bit grayscale, non-interlaced
[~/temp] gnuplot
set autoscale noextend
plot 'galaxy-16bitgrayscale.png' binary filetype=png with rgbimage
Note that gnuplot does not have any separate storage mode for grayscale vs. RGB image data. In this case it loads 3 copies of each 16-bit grayscale value into parallel storage as if it were separate R/G/B pixel data.
[2nd edit: show both grayscale image and contour levels]
set autoscale noextend
set view map
set contour surface
set cntrparam levels discrete 100, 200
set cntrparam firstlinetype 1
set key outside title "Contour levels"
splot 'galaxy16bit.png' binary filetype=png with rgbimage notitle, \
'' binary filetype=png with lines nosurface title ""

Why do YCbCr channels saved as jpeg images,the pixels' values change?

I used the function rgb2ycbcr in matlab R2013a to change RGB to YCBCR color space. And I saved each channel of YCBCR as a jpeg image.Then I read the jpeg image,for example CB channel,but I found the pixel value is different in the jpeg image with the channel CB before saved. Why does this happen? Here is my code:
I = imread('pic.jpg'); % // 'pic.jpg' is an unin8 rgb image
YCBCR = rgb2ycbcr(I);
Y = YCBCR(:,:,1);
CB = YCBCR(:,:,2);
CR = YCBCR(:,:,3);
imwrite(Y,'F:\CASIA V1.0\Y.jpg','jpg');
imwrite(CB,'F:\CASIA V1.0\CB.jpg','jpg');
imwrite(CR,'F:\CASIA V1.0\CR.jpg','jpg');
Then I read the CB.jpg, I found the pixel value is different with those in YCBCR(:,:,2). Is anything wrong with my code? I will be very grateful if anybody can answer my question.
When you use imwrite to store an image as a jpg, it runs jpeg compression on the image before saving it to file. By default, the compression quality is set to 75% of the original. I'm guessing this is the reason behind some pixel values changing from the uncompressed images to the compressed ones.

How to store the images with colourmap in MATLAB

I am using HDF satellite data to retrieve bands from that I am concluding different vegetation indices. Every band in hdf data is in grey colour format, its a grey colour scale image. After HDF data processed I can convert into colour by using colour map (I am using jet for colourmap). My doubt is how to convert greyscale image into colourmaped while using imwrite. How to use colourmap within imwrite. I have tried many times, but the output is only in full blue colour, this spoil the output image. Please help me to do this.
Why use imwrite? You can use imshow.
Example:
imshow(im)
imshow(im,'Colormap',jet(255))
With reference: http://www.alecjacobson.com/weblog/?p=1655
Try using the ind2rgb function before using imwrite if you want to save to a format like .jpg, but if you are using an indexing image format (e.g. .png) you can just use imwrite directly as shown in the docs:
imwrite(X, map, filename)
where X is your greyscale image, map is your colourmap (i.e. jet) and filename is the is the name of the image you want to save ending in .png

Why dicom image displayed are inverse colour and lossy for output. (Matlab)

Why are the colours inversed when I read a DICOM file? Also, when I am try to stretch and adjust the image output the export files are lossy (TIFF):
origImg = dicomread(uigetfile('~/matlab/*.*', 'Select Dicom File'));
J=imadjust(origImg,stretchlim(origImg),[0 1]);
CExpo = adapthisteq(J,'Distribution','exponential','Alpha',3);
imwrite(CExpo,'finish.tif','Resolution',100);
Resource and compare file: https://www.mediafire.com/?2xcwpt4khw3qh06
Matlab's Tiff class inverts uint8 image values when PhotometricInterpretation is 'Separated'. The Tiff specification specifically forbids this, but Matlab does it anyway for reasons unknown to me. That probably answers why your images are inverted. Generally speaking, Tiff images are not a lossy format, so if you believe there is a loss of fidelity, I do not understand how that is happening. (Nor do I know what a dicom image is).

reading a image file with imread in matlab gives what kind of representation?

I am newbie on matlab and doing some little image processing tasks. I am using imread to read some images but I don't get after reading the image what kind of representation is given. Is it a binary representation of histogram of the image?
If someone might describe me I'll be appreciated :)
From imread:
The return value A is an array containing the image data. If the file contains a grayscale image, A is an M-by-N array. If the file contains a truecolor image, A is an M-by-N-by-3 array. For TIFF files containing color images that use the CMYK color space, A is an M-by-N-by-4 array. See TIFF in the Format-Specific Information section for more information.
So it's an array, as your image also is an array of pixels.
Btw: it doesn't hurt to try things out for yourself...