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

19 vues (au cours des 30 derniers jours)
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

Réponses (1)

Guillaume
Guillaume le 19 Fév 2018
Modifié(e) : Guillaume le 19 Fév 2018
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 commentaires
Eliza
Eliza le 20 Fév 2018
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 le 20 Fév 2018
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Convert Image Type dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by