I have MATLAB code to read raw data and I can display it using 'uint16' like this:
But when I try to change 'uint16' to 'uint64' I get this:
I want to display my image like the 16bit image with using 64bit. Is there a way to do that? My code is here:
clear all;
I=fopen('data.raw');
A=fread(I,[2048 1536],'uint16');
imagesc(A.');
newmap = contrast(A);
colormap(newmap)
Ask yourself why do I want to use 64-bit ? A color is generally RGB888, 8-bits per components ... you can read the image in uint16 and then use this data in 64-bit floating point (double) format ... is that what you are trying to do ?
Related
I have a problem with imcontrast tool.
I read 2D dicom image, then convert it to 16bits(im2uint16) and perform filtration and windowing using imcontrast.
It shows me a warning message:
How Can I prevent then ?
How can I change data range in imtool ?
I would appreciate for any help please.
The [0,51156] is the range of the image data which change from image to image. [0, 65535] are the default color axis limits for uint16, which are outside the image data range, causing the re-adjust warning prompt. If you wish to silence the prompt you can manually set the color axis limits before calling imcontrast by:
caxis([double(min(min(Image))), double(max(max(Image)))]);
The conversion to double is to prevent a datatype mismatch. max() and min() return an uint16 value with uint16 data, which causes an another error down the line.
General problem description
I have 33 TIFF16 images and I want to do some processing on them using MATLAB. So reading them is the first step. After I download an image from the web and then try to read it using MATLAB's imread (as well as Tiff and read). I display the image using imshow. The image displayed by the Windows File Viewer and MATLAB is totally different. I cannot process them since I don't trust MATLAB has read them correctly. I give more specifics of the problem now.
EDIT: If it helps, the details of the TIFF16 images are: TIFF (16 bits per channel, ProPhoto RGB color space, lossless compression)
More details:
I download an image a0008-WP_CRW_3959.tif. Destination: Go to this link -> img0008 -> Expert B (In case somebody wants to try, otherwise I have screenshots below).
I read that image in MATLAB using: img=imread('imgFilename.tif','tiff'); imshow(img,[]); or
t = Tiff('imgFilename.tif','r');
imageData = read(t);
imshow(imageData);
Now, I display the snapshots of Windows file viewer:
Next, snapshot of what MATLAB shows me:
Now, I have a good reason to believe that Windows file viewer is correct. Go the same link as previous. Scroll down to img0008. Hover your mouse onto the leftmost img0008. A thumbnail view of Expert B will come which looks same as what Windows shows me.
Does anybody know how to make MATLAB read and show the tiff16 image correctly?
Thank you #MarkRansom for pointing me to the embedded color profile possibility. I believe the following solution is correct and produces the same output as Windows File Viewer.
First read the icc-color-profile using iccread command.
I_rgb = imread('a0008-WP_CRW_3959.tif');
outprof = iccread('sRGB.icm');
P = iccread('a0008-WP_CRW_3959.tif');
Then convert the image into sRGB profile using makecform and applycform:
C = makecform('icc',P,outprof);
I_cmyk = applycform(I_rgb,C);
imwrite(I_cmyk,'pep_cmyk.tif','tif')
info = imfinfo('pep_cmyk.tif');
imshow('pep_cmyk.tif');
The original image saved on disk and the new - pep_cmyk.tif - look exactly the same with Windows file viewer.
can anyone help me to save my resulted images by using imwrite
source = 'C:\Y\';
im_number=5;
for i=1:5
image{i}=im2double(imread([source,'Carbon_', num2str(i)],'tif'));
image{i}=double(image{i});
B{i}= Sftfun(image{i});
B{i}=uint32(B{i});
imwrite(B{i},[source,'face_', num2str(i)],'tif');
end
The problem with your code is that you are casting your image to uint32. If you are trying to save your image as a TIF file, you can only save it as 8-bit or 16-bit. Consulting the MATLAB documentation, you can only save with these two bit depths. 32-bit depths are not supported.
Consult the MATLAB documentation for more details: http://www.mathworks.com/help/matlab/ref/imwrite.html
As such, either cast the image as 8-bit or 16-bit (through im2uint8 or im2uint16), or normalize your image so that it goes from [0,1] (through im2double).
I also have some comments about your code that do need fixing for readability:
Do not save your images to a cell array called image. MATLAB has a built-in command called image which takes in a matrix and displays it to the screen for you as an image. Bear in mind this is not the same as imshow. By doing this assignment, you will shadow over the actual image command, and so any scripts that rely on this function will result in an error.
im_number seems to be an unused variable. I'm not sure what its purpose is, but I'd say it's safe to remove this statement as well.
Get rid of the following statement as you are already converting the image to a double type in the previous line:
image{i} = double(image{i});
Aside
It seems that you have asked a similar question here: save tif 32 bit images by using imwrite
This question has already been resolved in that you are not able to save 32-bit images using imwrite. However, someone in this thread has posted a workaround for you to use in MATLAB. Try using that instead of imwrite.
I have question about creating a binary / raw image file.
I've made an image in photoshop and now I want to load that in a C program.
I followed this tutorial http://www.nullterminator.net/gltexture.html but I don't know how to convert my own image to a .RAW file.
Can anyone help me out with this question?
Download ImageMagick, it is a command-line utility that can convert images into all kind of ways. And it supports many platforms.
So you could save your file in Photoshop to for example a PNG file, then run the following command to convert it to raw grayscale 8 bit:
convert MyImage.png -depth 8 gray:MyRawImage.raw
Try Save As and selecting RAW from the format drop-down. Humorously, it is listed under "P" for Photoshop RAW.
I don't know if it is the right kind of RAW which GLUT requires...
This worked for me, with that tutorial code:
Open your image in GIMP
Go to Layer/Transparency/Remove Alpha Channel. If it's already removed the option will be greyed out, which is fine. If you have
multiple layers, do this for all of them. (You MUST remove the alpha channel or else GIMP will write RGBA instead of RGB, and you'll just see a repeating lattice pattern instead of your image.)
File/Save As... and at the very bottom of the save popup, there's an option "Select File Type (By Extension)" with a (+). Expand it.
Select Raw image data
At the top of the save popup, manually give your file a .raw extension and save. Click OK to accept the default options.
Then you should be able to save it, move it to your program's directory, and read it in with the code from that tutorial.
Also, to save yourself another headache source, I suggest adding an error message if the file isn't found, like replace the line
if ( file == NULL ) return 0;
with
if ( file == NULL ){
printf("texture file not found.");
return 0;
}
I have an OpenCV matrix of double (CV_32F) values. I'd like to save it to the disk. I know, I could convert it to an 1-Channel 8-bit IplImage and save it. But that way, I loose precision. Is there a way to save it directly in the 32-bit format, without having to convert it first? It also would be nice, if the resulting file would have an image format, so I can view the result as an image.
You can always save any "object" (CvMat, IplImage, anything..) from OpenCV "as is" by using cvSave() and loading it back with cvLoad(). As to my experience, most floating-point image stuff does not work correctly, I usually save my floating point data this way.
However, you cannot directly view the stored data.
Another possibility we have used frequently is including an own built of OpenEXR. You can easily store full precision floating point images using this library and many third party applications are able to open EXR files. Note that OpenCV includes OpenEXR, if i am not mistaken, but the last time i've tried, saving/loading floating point images did not work correctly. However, you should first try to save an fp image as *.exr, maybe that already does the magic with recent versions.
You could always iterate over the matrix and write it out yourself. If you want it to be viewable as an image, you can use a variant of PPM. I'm not sure what programs would be able to natively read your image format if you use values out of the 0-255 range though.
This is old, but thought I'd throw in my two cents.
If you just want to save float images to disk, and you don't need to view them, you may want to look at Portable Float Map (PFM) image format. Very simple format, just saves floats to disk, no compression, minimal header. You can write your own read/write code for this very quickly. That's what I'm using for HDR research.
As the others pointed out, to "view" float images you need to ask yourself some questions about their contents and how to sensibly scale them back into an 8-bit range you can display on your monitor. You might consider Matlab's image viewer (imshow function) which offers some double scaling functionality.
You might also consider saving to either EXR or HDR format and using Photomatix's built-in HDR image viewer which gives you a little separate window that shows you a real-time tonemapped window around your current cursor position. It's a good way to navigate an HDR or floating point image to get a sense of "what's really there" without tonemapping the whole thing.