Is this the expected behavior of imapplymatrix() with explicit output class parameter?

3 views (last 30 days)
I'm using imapplymatrix() to speed up some things and noticed that it was giving unexpected results when trying to use the output class parameter. Regarding this parameter, the synopsis and web documentation merely says:
% Y = IMAPPLYMATRIX(..., OUTPUT_TYPE) returns the result of the linear
% combination in an array of type OUTPUT_TYPE.
Which leads me to assume that it simply rescales and casts the output to match the standard data range for OUTPUT_TYPE instead of the input class. Instead, it merely seems to cast the output without scaling it accordingly. This almost universally ends up in a garbage image unless the input class is matched, or unless the multiplier and offset arrays are both prescaled to compensate. For example:
inpict=imread('sources/RGB16Million.png'); % uint8 input
% these are just test parameters
mat=[0.299,0.587,0.114];
offset=0;
outpict=imapplymatrix(mat,inpict,offset,'int16');
imrange(inpict) % 0 255 as uint8
imrange(outpict) % 0 255 as int16
In this case, the output datatype is wider than the input, so it's not destructive; it's just not scaled correctly. If the input class were wider than the output, the image data would be lost.
I guess my question is how is this even supposed to be used? Are we supposed to be working on the assumption that MAT and OFFSET are both scaled to compensate?
I tried looking at the helper files to see for sure what it's actually doing, but it looks like that's all done in mex.

Accepted Answer

DGM
DGM on 23 Apr 2021
Edited: DGM on 23 Apr 2021
Well, again, I guess I'll resolve my own question without actually answering the core curiosity. I'll just assume that the cumbersome and inconsistent behavior is intended. If the user is forced to wrap every call to imapplymatrix() in all the requisite class-dependent matrix and output rescaling, then they might as well formalize it in a function. I went ahead and wrote another passthrough/fallback tool for my Image Manipulation Toolbox (MIMT).
Said function (imappmat) takes normalized transformation matrices regardless of image class, and the output is always scaled correctly for its class. For speed, it uses imapplymatrix() when available; otherwise, it does the transformation internally.
imappmat() is part of the MIMT and depends on it:

More Answers (0)

Community Treasure Hunt

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

Start Hunting!