jpeg2000 compression of hyperspectral image data (imwrite not working)

I am trying to apply add noise to a hyperspectral image data matrix(m-n-layers) by applying jpeg2000 compression on each of the layers saparately. So we can't use it like a nomal .jgp or .png image. I tried the imwrite scheme in loop format (not actual code here),
a = image(:,:,1); % a can only be a gray scale or rgb or an indexed data as I remember
imwrite(a,'image1.jpg', 'Compression','jpeg','CompressionRatio',10);
% the upper line threw an error that "not valid arguments"
% neither the next line work
imwrite(a,'image1.jpg','Mode','lossy','CompressionRatio',10); %according to the documentation
%the syntax doesn't allow the output to be a data matrix like the input itself too
imwrite(a,imageNoise(:,:,1), 'Compression','jpeg','CompressionRatio',10);
, but apparantely it allows compression for HDF format only. I want to store the output in a data matrix format(m-n-layers) only (compression applied layerwise). Can you suggest me any alternate way to apply jpeg2000 compression without imwrite. Thanks in advance.

4 Comments

What in your code is forcing jpeg2000 rather than ordinary jpg?
If you want to add jpeg noise/artifacts to your image, you're going to have to use imwrite() to write the JPG image. Not sure about JPEG2000 (which I believe has a lossless mode like PNG), but a round trip imwrite/imread with lossy JPEG2000 or ordinary JPG should show some differences (not at every pixel though), even more so when the compression ratio is higher.
I didn't get what you mean by forcing jpeg2000 properly. But what i want to do is create different copies of the data matrix (m by n by layers) with different compression ratios say 10, 20, 30 etc and store them as data matrix only , not in any image format. So what should i do for getting a data matrix output file? Also when I run this code, it gives the output error stating that arguments are invalid . If there is another way to apply compression (using DCT or DWT techniques) please suggest a way to do it.
'Compression' as an option is available only for HDF4 and TIFF.
'CompressionRatio' as an option is available only for JPEG2000. To get JPEG2000 you need to use the 'jp2' option (before the keyword/value pairs), or you need to use 'jp2' or 'jpx' as the file extension. JPEG2000 options are not available for .jpeg file extension.
So I ran this code
a = img(:,:,190); %190th layer
a_8Bit = uint8(255 * mat2gray(a));
imwrite(a_8bit,'imageb.jp2','CompressionRatio',10)
%scaled the layer of image to uint8
It worked! the output file is created as imageb.jp2 file
I have to create the modiefied img data matrix. I can't just store .jp2 files. I'll have to run this imwrite function 190 times manually. Can you suggest me an efficient way so that i can run it in one loop for all the layers(applying same compression ratio to each layer) and store in one place, then I will read the images in data matrix format?
Thanks a lot

Sign in to comment.

Answers (0)

Asked:

on 7 Apr 2019

Edited:

on 8 Apr 2019

Community Treasure Hunt

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

Start Hunting!