can I convert multiple png images to bmp images in matlab without effect on the quality of the images?

조회 수: 19 (최근 30일)
I used the following code and the ouput of some images are completely black
d=dir('*.png');
for i=1:length(d)
fname=d(i).name;
png=imread(fname);
fname = [fname(1:end-4),'.bmp'];
imwrite(png,fname,'bmp');
end

답변 (1개)

Guillaume
Guillaume 2018년 2월 19일
편집: Guillaume 2018년 2월 19일
That depends on the png images. The png format supports a lot more image formats than bmp. A png image can be up to 16 bits per channel whereas bmp can only go up to 8 bits. I suspect you'd be getting an error when trying to save the image in that case, though.
The png format also supports transparency (which you're not reading, if it exists) whereas bmp doesn't.
However, for a basic 8 bit-per-channel, no transparency png image, there'll be no difference if saved as bmp, other than the bmp using a lot more disk space.
Note that overall png is a much better format than bmp, so unless you have very good reasons, I wouldn't bother with the conversion.
  댓글 수: 2
Eliza
Eliza 2018년 2월 20일
Actually I am working on my system which I built it from the beginning according to database of bmp images and all the development stages was working on bmp images. Now I have to test it on other database but I have to convert it to bmp to evaluate the results. There is no error occur during conversion but the output is not desirable at all :( I uploaded samples of the images.
Guillaume
Guillaume 2018년 2월 20일
The problem in your case is that the png image is indexed but you're not reading the colour map associated with the indices. The following code will work with rgb, greyscale and indexed images:
dircontent = dir('*.png');
for fileidx = 1: numel(dircontent);
filename = dircontent(fileidx).name;
[img, map, trans] = imread(filename);
if ~isempty(trans)
warning('transparency information of %s discarded', filename);
end
[~, basename] = fileparts(filename);
outname = [basename, .bmp'];
if isempty(map)
%greyscale and colour image
imwrite(img, outname, 'bmp');
else
imwrite(img, map, outname, 'bmp');
end
end
Note that this convert an indexed png into an indexed bmp. If your processing code is not designed to cope with indexed image, you may be better off converting indexed images to colour. In that case, replace the above from if isempty(map) by
if ~isempty(map)
img = ind2rgb(img, map);
end
imwrite(img, outname, 'bmp');
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by