is there a better way to recover 16UC bit values?
Hi guys, let's say that I have a CV_16UC1
matrix with 16bit unsigned short values which it needs to be transformed to CV_8UC1
for now no any further process in between and then back to CV_16UC1
. The way I am doing it at the moment is:
// img is the 16bit image
double minval,maxval;
minMaxLoc(img, &minval, &maxval, NULL, NULL);
img.convertTo(img, CV_8UC1, 255.0 / maxval);
img.convertTo(img, CV_16UC1, maxval / 255.0);
However, the values in the new 16bit matrix slightly differ from the original ones. My question is there a better way to recover the original values as it was (I guess no, but I need to ask :-p), or at least how I can decrease the difference between the original and new values to the minimum as possible?
I'm not sure to understand but may be like this
Mya be at the end an error about max-minval/255
You mean unsigned short, not floats, right? CV_16UC means 16 bit unsigned, which is a 16 bit integer.
@LBerger thanks for the response. Your solution did not change that much, I got more or less the same result. What do you mean by "Mya be at the end an error about max-minval/255"
@Pedro Batista yes you are right, my bad. (fixed)
I don't get something. Do you need to do some operation on the Mat before changing it back to the 16bit? Why don't you just convert the first 16bit image into a new Mat and keep a copy of both alive? (Because normally, converting and image to 8bit is used to display it).
@Pedro Batista in order to explain a bit better. I have this 16bit pixel values image (it is actually a depth image) with some blank spots (pixel value = 0) which I would like to smooth, something like this one. However, this demands to use
inpaint()
which only accepts 8-bit images as input. Therefore, I have to convert my 16-bit image to 8-bit image but afterwards I would like to recover my original 16-bit depth image values. I think what you and @Guyygarty propose, by keeping a copy of it and then just replace the blank values will do the trick. Thanks ;-).btw, there is an inpaint version in xphoto, which works with float or ushort input images
@berak many thanks I did not know about it. I will try it as soon as possible. Have a look also to this thread that I opened regarding
inpaint()
.Offtopic question: is that inpaint() function fast? Can you use it at real-time framerate to fix your raw data?
@Pedro Batista supposedly not. For that reason if you check also the link with the initial code they first scale down the image, apply
inpaint()
and then resize it back to the initial size. This is how they get real time framerate. For my case at the moment I am not concerned about speed since I have the data already, and I just need to process them. Later on, I will need to think about it as well.