How to convert medical images in mat format into jpg without losing information?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I have a stack of medical images. I saved all images in the stack into a folder (i.e., converting the 3D matrix into 2D mat files). When I tried the following code that I have got from the Internet, there are blank images. What do you suggest? Your help is appreciated.
dirpath='C:\Users\Sara\Desktop\Data_Preparation\Database\';
dirpath1='C:\Users\Sara\Desktop\Data_Preparation\Database_converted';
type='mat';
type1='jpg';
oldvar = '';
for j=1:length(dirpath)
infile = fullfile(dirpath, sprintf('image%03d.mat', j));
outfile = fullfile(dirpath1, sprintf('HK%d.jpg', j));
datastruct = load(infile);
fn = fieldnames(datastruct);
firstvar = fn{1};
data = datastruct.(firstvar);
imwrite( data, outfile );
if ~strcmp(oldvar, firstvar)
fprintf('loading from variable %s as of file %d\n', firstvar);
end
end
1 commentaire
Image Analyst
le 29 Déc 2016
Normally jpg does a lossy compressions so that's why I suggested jp2. It's still JPEG, jst a new version that allows lossless compression. Are you absolutely 100% sure that you need jpg format and not jp2 format even though they are both "jpeg"? If so, you can try JPG with a quality factor of 100% and see if that works. If you recall the image and it's different then it can't be saved without loss in jpg format and you'll have to use jp2 or PNG (like much/most of the world does).
As far as it not processing one of your files, I suspect that file does not exist. You are using the code chunk in the FAQ that builds names with sprintf. But unlike the robust code in the FAQ, you are not checking if the file exists before you try to process it. There is a reason why it calls the exist() function (I ought to know - I wrote that FAQ entry), so you should use it.
However, you should probably use the code chunk in the FAQ that gets the filenames with the dir() function so the files are guaranteed to exist. http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
Réponses (2)
Image Analyst
le 29 Déc 2016
You'll have to use lossless JPEG2000:
fullOutputFileName = fullfile(dirpath1, sprintf('HK%d.jp2', j)); % Note: jp2 extension, not jpg.
imwrite(data, fullOutputFileName, 'Mode', 'lossless'); % Note: lossless mode.
3 commentaires
Image Analyst
le 29 Déc 2016
Normally jpg does a lossy compressions so that's why I suggested jp2. It's still JPEG, jst a new version that allows lossless compression. Are you absolutely 100% sure that you need jpg format and not jp2 format even though they are both "jpeg"? If so, you can try JPG with a quality factor of 100% and see if that works. If you recall the image and it's different then it can't be saved without loss in jpg format and you'll have to use jp2 or PNG (like much/most of the world does).
As far as it not processing one of your files, I suspect that file does not exist. You are using the code chunk in the FAQ that builds names with sprintf. But unlike the robust code in the FAQ, you are not checking if the file exists before you try to process it. There is a reason why it calls the exist() function (I ought to know - I wrote that FAQ entry), so you should use it.
However, you should probably use the code chunk in the FAQ that gets the filenames with the dir() function so the files are guaranteed to exist. http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
Andre Brandao
le 5 Juil 2019
Hi, I need help with a bench of xray images. I wanna change them from jpeg to png, how do I do it in matlab? Could you help me?
I'm getting the error below:
Error using trainNetwork (line 165)
Unexpected image size: All images must have the same size.
Error in chestXray (line 39)
trainedNet = trainNetwork(imdsTrain,layers,options);
0 commentaires
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!