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

조회 수: 3 (최근 30일)
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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by