imwrite error saying 'TransparentColor' is not a recognized parameter

3 vues (au cours des 30 derniers jours)
Isa L
Isa L le 8 Jan 2020
Commenté : Isa L le 9 Jan 2020
I am trying to remove the black background created when using imwarp and read that it can be done when exporting the images. the error I get is:
Error using writetif>parse_param_value_pairs (line 161)
'TransparentColor' is not a recognized parameter. For a list of valid name-value pair arguments, see the documentation for this function.
However when using
doc imwrite
it exist as a Name-Value pair argument (imwrite) with the example 'TransparentColor', 20. So therefore I added 20 jut to try it out (black might be another colour).
This is part of the code I am trying to get to work, where movingReg is a uint8 object. Why is it giving me this error?
imwrite(movingReg ,fullfile('C:\Users\X\Documents\Matlab\multiplex\55D-A',string(['moved',num2str(number(num)),'.TIF'])),'TransparentColor',20);

Réponse acceptée

Guillaume
Guillaume le 8 Jan 2020
Yes, the documentation list 'TransparentColor' as a parameter but only for GIF images. GIF is a very limited format, completely outdated, you shouldn't be using it.
As far as I remember, TIF supports transparency (as an alpha channel) but matlab's imwrite doesn't support it for tiff. You may be able to work around it by using the Tiff class.
On the other hand, if you're not restricted to TIF, you could write the image as PNG, in my opinion a better format anyway. With PNG, you can designate a single colour as transparent (with the 'Transparency' parameter) or any amount of transparency for each pixel individually (with the 'Alpha' parameter). E.g
%the following assumes image format is double RGB, so img is MxNx3
%img: a MxNx3 matrix of class double
imwrite(img, 'somename.png', 'Transparency', [0 0 0]); %set pure black as transparent, wherever it is in the image
%or
%alpha = all(img == 0, 3); %set pure black pixels to alpha = 0 == fully transparent
alpha = 1 - 0.75 * all(img == 0, 3); %set all black pixels to alpha = 0.25, mostly transparent
imwrite(img, 'somename.png', 'Alpha', alpha); %alpha is a 2D matrix with the same number of rows and columns as the image

Plus de réponses (1)

Steven Lord
Steven Lord le 8 Jan 2020
That argument appears in a section on the documentation page describing functionality available for the GIF format, not for the TIFF format. Some of the options are supported for several of the formats (WriteMode is supported for both GIF and TIFF, for example) while others are specific to just one of the formats (like TransparentColor to GIF.)

Community Treasure Hunt

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

Start Hunting!

Translated by