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?

 Akzeptierte Antwort

Michael Haderlein
Michael Haderlein am 9 Mär. 2015

0 Stimmen

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 Kommentare

Sarah
Sarah am 9 Mär. 2015
I save then load the image because the code I have calculates area moment of inertia (AMOI) contribution of individual pixels for a given cross section, which is very organically shaped and cannot be accurately described using traditional AMOI estimation techniques. That and I cannot figure out how to analyze the AMOI from the MATLAB figure directly. If jpg is not the best format for this type of analysis in your opinion, what would you recommend?
Thank you for your help!
Michael Haderlein
Michael Haderlein am 9 Mär. 2015
Usually, I prefer png for saving images in Matlab, but it depends on the graphic itself.
Sarah
Sarah am 9 Mär. 2015
It's just a black and white image (cross section in black, white background). I'm hesitant to use png because of the file size, but I suppose I could look into it. I'm going to end up with close to 10,000,000 images, so the smaller the images without compromising too much quality, the better.
Michael Haderlein
Michael Haderlein am 9 Mär. 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.
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Gefragt:

am 9 Mär. 2015

Kommentiert:

DGM
am 12 Jul. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by