Resolution of a MATLAB-created JPG

4 views (last 30 days)
Sarah
Sarah on 9 Mar 2015
Commented: DGM on 12 Jul 2023
I am using some previously developed image processing code in order to calculate some area moments of inertia. In order to use the previously developed code, I have to plot my cross section, save it as a jpg, then upload that file and run the code on it. One portion of the code looks for the resolution (mm/pixel) of the cross sections. Is there a way to calculate the resolution from just the iminfo and the known size in mm of the cross section?

Accepted Answer

Michael Haderlein
Michael Haderlein on 9 Mar 2015
In case you include the information about the resolution, you can get it via imfinfo (see e.g. http://www.mathworks.com/matlabcentral/answers/53909-how-can-i-find-the-resolution-of-an-image)
BUT why do you first save and then load the image? Also jpg is not a good format for graphs (you know these blurry artefacts close to hard edges such as lines). Whatever you do (except of investigating these artefacts, maybe) will be easier if you simply use the data you already have instead of saving it as image, loading the image and processing the data finally.
  5 Comments
Michael Haderlein
Michael Haderlein on 9 Mar 2015
If you really do this save-load-processing workaround 10 million times, it will take quite some time. I don't know the image, but if you really want to do it such often, I would think about how to do it directly. If I remember correctly, the calculation of the area moment of inertia was just some integration of the distance of each point from the center of mass over all points, right? To me, this sounds like a rather simple operation including some summation, averaging and multiplication. Of course, if it's more complicated and you have some third-party function which needs a file name as input, then it might be easier to go this way instead of going all the way through this function to modify it.
DGM
DGM on 12 Jul 2023
Don't presume JPG gives good compression.
If your images are binarized, PNG will not only be lossless and save the image in the original class and scale, it will typically produce a smaller file. Depending on the distribution of pixels, a JPG may be twice the size. For heavily detailed images, it may be 50-100x larger than a PNG. Even for roughly binary images (e.g. an antialiased mask), JPG will typically produce a larger file.
% generate a large binary image containing one blob
inpict = imread('peppers.png');
inpict = imresize(inpict,2);
inpict = im2gray(inpict)>128;
inpict = bwareafilt(inpict,1);
% write it to a PNG, read it back
tic
imwrite(inpict,'a.png')
a = imread('a.png');
ta = toc; % the time taken
Sa = imfinfo('a.png');
% write it to a JPG, read it back
tic
imwrite(inpict,'b.jpg')
b = imread('b.jpg');
b = b>128; % image needs to be rebinarized
tb = toc; % the time taken
Sb = imfinfo('b.jpg');
% time ratio (JPG-based workflow is slower)
tb/ta
ans = 2.8338
% size ratio (JPG files are significantly larger)
Sb.FileSize/Sa.FileSize
ans = 8.6771
In all likelihood, you're spending more time and using more space by using JPG -- and all for no benefit.

Sign in to comment.

More Answers (0)

Categories

Find more on Images 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!