How to convert medical images in mat format into jpg without losing information?

4 ビュー (過去 30 日間)
Sara Salimi
Sara Salimi 2016 年 12 月 29 日
回答済み: Andre Brandao 2019 年 7 月 5 日
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 件のコメント
Image Analyst
Image Analyst 2016 年 12 月 29 日
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

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2016 年 12 月 29 日
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 件のコメント
Walter Roberson
Walter Roberson 2016 年 12 月 29 日
LMDB does not require any particular binary format.
Image Analyst
Image Analyst 2016 年 12 月 29 日
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
Andre Brandao 2019 年 7 月 5 日
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);

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by