Clear Filters
Clear Filters

How do I encode a jpg to a set quality level in order to calculate it's ela without first writing it to a temp file?

1 view (last 30 days)
I have tried looking at using a encoder some exist in the File Exchange, but are not written to allow adjustable quality levels or are faithful adaptation of the original jpeg encoder!
I'm now using a virtual ram disk to hold the temp file, which is then deleted!
function [] = generate_ela(filename,quality)
if nargin > 2 | nargin < 2
error('usage: generate_ela(filename,quality)');
end;
img_orig = imread(filename);
tempfile = 'z:\temp.jpg';
imwrite(img_orig,tempfile,'jpeg','Quality',quality);
temp = imread(tempfile);
delete(tempfile);
idiff = uint8(abs(double(img_orig)-double(temp))*30);
me = max(max(max(idiff)));
idiff = uint8(double(idiff)*255/double(me));
%imwrite(idiff,'ela.jpg');
figure; imshow(idiff);
end
  3 Comments
Thomas
Thomas on 14 Jun 2013
Edited: Thomas on 15 Jun 2013
I was asking how to convert the code so that it does not need to actually write to disk.
Image Analyst
Image Analyst on 15 Jun 2013
I don't believe there is a function to generate the jpeg artifacts other than by calling imwrite followed by imread.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 14 Jun 2013
Edited: Walter Roberson on 14 Jun 2013
Writing to file is by far the easiest method. If you had a RAMDISK or solid state drive then the writing (and reading) would be quite fast.
Otherwise...
Have a look at this which shows how to do JPEG compression in Java. There is a point where the code creates an output stream associated with a disk file. I suspect that it is potentially possible to create an output stream into memory instead of into a file. Or you could probably connect the output stream from the compressor as the input stream to the decompresser. I have not worked with Java more than a little, so I am not certain that these can be done.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!