How to save the compressed file in singular value decomposition

1 view (last 30 days)
I am using the built-in function of svds for compressing image the problem is that i want to save the compressed file before reconstructing image the code is as the following
function [im] = imcompr (infile,singvals,outfile)
% Example of use:
% out=imcompr('input_image.dcm',20,'output_image.dcm');
% and [n,m]=size(input_image)
if (exist(infile)==2)
a = dicomread(infile);
a = mat2gray(a);
figure('Name','Input image');
imshow(a);
end
if ndims(a)==2
dvalue=double(a);
end
[u,s,v] = svds(dvalue, singvals);
if isa(a,'uint8')
im = uint8(u * s * transpose(v));
end
if isa(a,'uint16')
im = uint16(u * s * transpose(v));
end
if isa(a,'double')
im = (u * s * transpose(v));
end
dicomwrite(im, outfile);
figure('Name','Output image');
imshow(im);
return;
end
  4 Comments
haithem abdelghany
haithem abdelghany on 29 Jun 2017
I want to save the variable which contain the compressed data before decompression but when i save s or the vector [u,s,v] it give me file which is more in value than the original image

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Jun 2017
Remember that dlmwrite() writes as text, and text is often about 3 times larger than binary. In order for the output text file to be smaller than the original binary file, the values would have to be really fortunate (e.g., 1 digit integers plus a comma could be smaller than the binary representation of the values), or else your output array would have to have fewer elements than the original to compensate for text taking up more room.
If you want to store s in a file in a way that does not expand like I describe above, you need to store it in binary, and you need to store as few bits for each element as you can get away with and still distinguish all the possible outputs.
For example, if you could happen to prove that the s array consists entirely of integers in the range -32768 to +32767 then you could store them as int16, and that might happen to be less than whatever was used to store them in the .dcm file. But I suspect you will find that you need at least single precision, 4 bytes per location, and that the result will be larger than the original matrix, which is probably int16 or uint16, 2 bytes per location. (Not every proposed compression technique works, and some compression techniques are only suitable for lossy compression situations.)
  7 Comments
Walter Roberson
Walter Roberson on 30 Jun 2017
I wrote "It is a lossy compressor like I guessed." and you said "No", even though you have not seen the code yourself. I already posted a link to a paper that describes the theory of SVD compression and I could post additional links, but the only way that I could prove to you that they are in fact doing lossy compression would be for one of us to buy the code.
Notice the table near the end. For any lossless compression technique, MSE (Mean Squared Error) would be exactly 0.0, so any entry there with non-zero MSE is a lossy compression technique.
Anyhow, I've told you above what you should do:
"If you want to store s in a file in a way that does not expand like I describe above, you need to store it in binary, and you need to store as few bits for each element as you can get away with and still distinguish all the possible outputs."

Sign in to comment.

More Answers (0)

Categories

Find more on Denoising and Compression in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!