How to save inversed DCT image - matlab

Here is the code:
imshow(idct2(CDCT),[0 255])
i=idct2(CDCT),[0 255];
imwrite(i,'fa.tif');
When I display the image, it works fine. But only white image with few black lines is saved (incorrect image). Please tell what I am doing wrong. :)

If the image data that you are writing to a file using imwrite is of type double or single (which yours is), then all values are expected to be between 0 and 1. Your values are mostly greater than 1 since your data is all between 0 and 255, so this is why the image is appearing as mostly white. You can easily normalize your data using mat2gray prior to calling imwrite.
imwrite(mat2gray(i), 'fa.tif');
Otherwise, if you pass uint8 values to imwrite, the values are expected to be within the range of 0 to 255 (as your data is). So you can simply cast your input data as a uint8 prior to saving
imwrite(uint8(i), 'fa.tif');

Related

Exchange phase of 2 image's fft and reconstruct [duplicate]

I'm using MATLAB for image processing and I came across a code with the instruction:
imshow(pixel_labels,[]);
when executed it give a binary image.
I have check the manual of the function on Mathworks.com, the most similar used mode is
imshow(I,[low,high]);
but they don't say a thing about the case where that array is empty ([])
I tried to remove it:
imshow(pixel_labels);
but all I see is a white board. I would like to know what is happening in the first use case (imshow(pixel_labels,[])), I hope from there I will understand why I get a white board in the last use case.
If I type help imshow in MATLAB, the first paragraph reads:
IMSHOW(I,[LOW HIGH]) displays the grayscale image I, specifying the
display
range for I in [LOW HIGH]. The value LOW (and any value less than LOW)
displays as black, the value HIGH (and any value greater than HIGH) displays
as white. Values in between are displayed as intermediate shades of gray,
using the default number of gray levels. If you use an empty matrix ([]) for
[LOW HIGH], IMSHOW uses [min(I(:)) max(I(:))]; that is, the minimum value in
I is displayed as black, and the maximum value is displayed as white.
so [] is simply shorthand for [min(pixel_labels(:)) max(pixel_labels(:))].

Using dicomwrite with color images

I am trying to write a sequence of color images to a dicom file in Matlab. Each image is of type uint16. The sequence is stored in a 4D matrix named output of size 200x360x3x360 (num of rows x num of cols x num of channels x num of images). When I execute dicomwrite(output,'outputfile.dcm'), it gives the following error:
It says data bit depth is 8 but I've ensured that each image is 16-bit. Not sure what's going wrong.
The documentation for dicomwrite says it can write color images as well. In fact dicomread can read color dicom images such that the size of the matrix which stores the read data is 200x360x3x360. So I guess it should be possible to write color images as well using dicomwrite. Any help in this regard is appreciated. There is a related post but it doesn't talk about color image sequence.
The comment by JohnnyQ is correct.
From this page down in section A.8.5.4, they list the Multi-frame True Color SC Image IOD Content Constraints (partial list quote):
In the Image Pixel Module, the following constraints apply:
Samples per Pixel (0028,0002) shall be 3
Bits Allocated (0028,0100) shall be 8
Bits Stored (0028,0101) shall be 8
High Bit (0028,0102) shall be 7
Pixel Representation (0028,0103) shall be 0
It seems matlab will not do the conversion for you, so you should down convert each 16-bit color channel to 8-bit for DICOM

How to re size a too big image into small by keeping original values

I have an gray scale image of size <2559x3105 uint16>. when I try to open this image, I get warning that it is too big. I have tried imresize() function to make it small<512x512 uint8> in size. When I plot the original image and re-sized image, the intensity gets decreased after re-sizing. I want to re-size original image without changing in its pixel values. Is there any solution?
If you would like to keep your final image as uint8, I think you would be needed to first convert the uint16 image to uint8 image using im2uint8 -
uint8_image = im2uint8(uint16_image);
Then you may apply imresize on uint8_image.
But, if you don't want your final image to be of uint8 type, you can directly use imresize and it would keep the datatype, i.e. the resized image would be of uint16 type.
Read the docs and use the nearest neighbor method. That is,
resized = imresize(original, scale, 'nearest')
This will not use interpolated values. The downside is of course that edges might be jagged.
It sounds like your 16-bit image uses linear codes while the resulting 8-bit image needs to be gamma corrected. If this is the case you can use imadjust with a gamma parameter of 1/2.2 to produce the brighter image.
Do you get the warning when you display it with imshow? Does it say something like "Image to large to fit the screen, resizing to xx%"? If so, then you can simply ignore the warning. Otherwise, you can can set the 'InitialMagnification' parameter of imshow to resize the figure, but not the image itself.

When I write the image it appears black

I have a program that returns a grayscale image. But, when I try to write the image, it appears totally black. Why is that? How can I write the image and get the expected result?
Thanks.
First check on the type of your data. You can cast the type of the data by example double() or uint16() etc. (Check the help for typecasting).
Here is an example how you rescale your values to the intensity-range of uint16, unsigned integers with ~65k possible different values. The casting of course leads to less precision of the intensity values.
new_img(:,:) = uint16((new_img(:,:)./max(max(new_img(:,:),[],1)))*65536);
Afterwards you should be able to write the data to your file.
Make sure that your grayscaled image is of the right class. Furthermore check the values in the generated image. If they're simply too low all will appear black. If you can provide more specific information it might be possible to give a more elaborate answer.
if you're working on a binary image(before being converted to gray) and you about to convert it to gray-scale, then you suddenly change the range of pixels from [0, 1] to [0, 255]. so the value '1' in binary image is totally white but in gray-scale image is almost black.
try this:
img = imread('image_name.jpg');
imshow(img*50)
it make you sure that you image is black or just its pixel-values aren't appropriate.

pixel values changing after doing imwrite in MATLAB

The imwrite function is behaving in a weird way. I have modified a single pixel value of an image. After performing imwrite, however, the pixel value is either getting changed to a entirely new value OR is remaining unchanged.
function imwriteCheck(input_image,output_image)
a=imread(input_image);
fprintf('\nBefore modification a(1,1,1)=%d\n',a(1,1,1));
a(1,1,1)=50;
fprintf('\nAfter modification a(1,1,1)=%d\n',a(1,1,1));
imwrite(a,output_image);
b=imread(output_image);
fprintf('\nValue at b(1,1,1)=%d\n',b(1,1,1));
end
I've tested this function with two images and the outputs are as follows:
>> imwriteCheck('MOM.jpg','MOMout.jpg')
Before modification a(1,1,1)=206
After modification a(1,1,1)=50
Value at b(1,1,1)=170
>> imwriteCheck('durga.jpg','durgaout.jpg')
Before modification a(1,1,1)=63
After modification a(1,1,1)=50
Value at b(1,1,1)=63
I cannot understand why this is happening. Thank you for your help.
if you write to a jpg file, pixel values get changed because of their lossy compression technique. you can write to a jpg file using lossless mode but then you wont be able to view the image anywhere else.
try writing to a bmp or png file, you'll see the pixel values are NOT changing.