Matlab convert DCM to BMP - matlab

How to convert .dcm(dicom) image to .bmp format.
I have tried this, but its not working for me.
000000.dcm is a medical lung image (size 6 MB)
dcmimg = dicomread('000000.dcm');
>> imshow(dcmimg);
DCM image read in dcmimg
imwrite(dcmimg, 'myfile.bmp');
Getting error:
Error using writebmp (line 15)
Expected X to be one of these types:
logical, uint8, single, double
Instead its type was uint16.
Error in imwrite (line 477)
feval(fmt_s.write, data, map, filename, paramPairs{:});
I am not understanding these errors, What's going wrong. Help me out, Thank You.

Dicom uses unsigned 16 bit integers. The function that writes .bmp files expects a different data type, as explained by the error message. This suggests that the following should work:
imwrite(double(dcmimg), 'myFile.bmp');
By explicitly converting the data type to double you should get rid of the error. Actually single might be enough... It too can represent 16 bit integers without loss of precision.

Related

Error: The compressed pixel data is missing item delimiters.?

I am working with few Dicom files and when i try to use dicomread('filename.dcm') in MATLAB it gives the following error:
Error using dicomread>processOffsetTable (line 943)
The compressed pixel data is missing item delimiters.
Error in dicomread>processEncapsulatedPixels (line 858)
[offsetTable, offset] = processOffsetTable(metadata);
Error in dicomread>newDicomread (line 232)
X = processEncapsulatedPixels(metadata, frames);
Error in dicomread (line 86)
[X, map, alpha, overlays] = newDicomread(msgname, frames, useVRHeuristic);
I can view this same file in dicom viewing Softwares like onis, di com viewer, Sante Dicom etc.., but when i use dicomread I cannot see see them and get this error
I have so many images of this same format and cannot start from the beginning again, Is there any way I can use this file and view it.
Refer this online help.
It is common in DICOM world that not all data sets fully comply with DICOM. Most applications (you mentioned in your question) handle the non-compliant part with assumptions and workarounds based on experience and imagination.
Try setting TF to false to read these files.
Also note the list of supported transfer syntax:
Little-endian, implicit VR, uncompressed
Little-endian, explicit VR, uncompressed
Big-endian, explicit VR, uncompressed
JPEG (lossy or lossless)
JPEG2000 (lossy or lossless)
Run-length Encoding (RLE)
GE implicit VR, LE with uncompressed BE pixels (1.2.840.113619.5.2)
Check your input image is compressed with one of the above.

Using Matlab to read binary data from file

I'm currently trying to read data from a .surf file using Matlab. (I realise there will probably be quite a lot of other questions similar to this, but each is specific to its own problem, and I wasn't able to find a duplicate of what I'm about to ask; if someone else does and marks this as duplicate... apologies!)
This is what the readme for the data files says:
All of the files consist of 1-byte values, except for the
surface (surf) files, which consist of 4-byte floating-point
values. The 1-byte phase values denote the phase scaled and
quantized to the range 0-255. The 1-byte correlation and
mask values denote the weights 0-1 scaled and quantized to
the range 0-255.
I have been able to read a .phase file without any problems using
fn = 'spiral.257x257.phase';
f = fopen(fn);
fin = fread(f, [257, 257], 'uint8');
fclose(f);
However, I'm unable to read the surf file. I have tried single, float32 and real*4, the three options under 'Floating-point numbers' in the Matlab documentation for fread, as well as uint, uint32, int, int32 and long, which are the other options for 4-byte data. None of these give the correct solution at all - not even just the wrong scaling; they're completely off.
I'm pretty stuck for ideas; any advice (including general advice) would be most appreciated.

MSER features detection not working in matlab

Why is that whenever I try to use detectSURFFeatures(img) with a binary image in matlab gives me proper points but whenever I use detectMSERFeatures(img) with the same binary image gives me error instead of pointing some valid regions?
ERROR:
Error using detectMSERFeatures
Expected input number 1, I, to be one of these types:
uint8, int16, uint16, single, double
Instead its type was logical.
Error in detectMSERFeatures>parseInputs (line 75)
validateattributes(I,{'uint8', 'int16', 'uint16', ...
Error in detectMSERFeatures (line 64)
[Iu8, params] = parseInputs(I,varargin{:});
Try this:
make image 2 double first by using img=im2double(img);
then feed it to MSER
detectMSERFeatures(img)
detectMSERFeatures does not accept logical inputs, as stated in the documentation and in the error you are receiving. detectSURFFeatures does. I don't know if there's a specific reason why, as I'm not familiar with the limitations of the different algorithms.
You could simply convert your binary image to one of the types listed, and run MSER on it:
detectMSERFeatures(double(img));

satellite images in matlab

I have a satellite image in the BSQ format. I initially processed it using ENVI. Now, I am trying to read the image using Matlab's multibandread function.
This is the data pertaining to the image I am trying to load
samples = 911
lines = 3191
bands = 196
header offset = 0
data type = 2
interleave = bsq
byte order = 0
This is the code I've written to load the image.
I = multibandread('QUAC.bsq',[3191,911,196], ...
'int16','0','bsq','ieee-le',{'Band','Direct',[29,23,16]});
I am getting the following errors:
Error using multibandread>parseInputs (line 311)
Unable to open QUAC.bsq for reading.
Error in multibandread (line 112)
info = parseInputs(filename, dims,...
I looked up the format for the multibandread function on this website
http://www.ehu.es/ccwintco/uploads/d/dc/LoadHypercubesMatlab.pdf
I checked the code for parseInputs on MATLAB, but I was unable to make any difference.
What mistake am I committing while trying to load the image?
The question is a bit old, but it may be useful for somebody else to know the answer.
This was most likely a wrong filename ('QUAC.bsq'), or that file was not located in the current MATLAB working directory.
There is another mistake in the list of arguments that, with a valid filename, returns this error in Matlab 2015b :
Error using multibandread>parseInputs (line 337)
Expected input to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64
Instead its type was char.
Error in multibandread (line 111)
info = parseInputs(filename, dims,...
Replacing '0' with 0 in multibandread arguments should fix the problem, worked for me. The correct syntax is:
I = multibandread('QUAC.bsq',[3191,911,196], ...
'int16',0,'bsq','ieee-le',{'Band','Direct',[29,23,16]});

reading and displaying USGS NED DEM

I'm trying to use USGS DEMs in MATLAB but after reading it in with geotiffread, mapshow produces an error. Based on the documentation, I tried
[A, R] = geotiffread('NED_10340081')
figure
mapshow(A, R);
but I get
Error using mapshow
Expected input number 1, I or X or RGB, to be one of these types:
uint8, uint16, double, logical
Instead its type was single.
Error in validateMapRasterData>parseImageInputs (line 109)
validateattributes(A, {'uint8', 'uint16', 'double', 'logical'}, ...
Error in validateMapRasterData>validateImageComponent (line 93)
[A, R] = parseImageInputs(mapfcnname, dataArgs{:}, cmap, rules );
Error in validateMapRasterData (line 27)
[Z, SpatialRef] = validateImageComponent(mapfcnname, ...
Error in maprastershow (line 127)
[Z, SpatialRef, displayType, HGpairs] = ...
Error in mapshow (line 231)
h = showFcn(varargin{:});
My matrix A is of type single...is that the problem? and how do I fix this?
Here is a download link for the DEM http://snowserver.colorado.edu/pub/fromDominik/NED_10340081.zip
Thanks
PS I crossposted this at http://www.mathworks.com/matlabcentral/answers/38255-display-usgs-dem-using-geotiffread-and-mapshow
Specifying DisplayType is necessary because Matlab distinguishes "images" from continuous raster data. Matlab has an entire Image Processing Toolbox as well as a Mapping Toolbox with distinct functions in each that are often complimentary but come from different development backgrounds. Matlab supports "images" in logical, uint8, uint16, and double class types, and raster (grid) data in single and double.
Also your solution to use 'DisplayType','surface' only worked in your case because you had 'single' data. If, for example, you had created a geotiff image in ArcGis and then tried to read it into Matlab it would come in as int16 and you would get the same error when trying to use mapshow or geoshow, but the 'DisplayType','surface' would not work in this case because that DisplayType does not accept int16. Your solution to convert to double is more general.
I finally figured it out.
geotiffread gives a matrix type 'single' but mapshow defaults to show an image which only accepts type 'double' (and some others). So the solution here is to either:
A=double(A);
or
mapshow(A,R,'DisplayType','surface');