How to increase precision of image processing functions?
Show older comments
I have a 4K video which I stabilize with OpenCV and get a perfect result. Since I need to work with some stabilized frames in Matlab, I saved every transformation Matrix (10 significant digits) for every frame calculated by OpenCV in a CSV file. Then I read the CSV file in Matlab and apply the transformation to the frame I want. Minimum Code example I use:
affineTrafo = affine2d(transpose(transformMat));
[transformedFrame, transformedRef] = imwarp(frame, affineTrafo);
[xIntrinsic, yIntrinsic] = worldToIntrinsic(transformedRef, 0, 0);
padSize = ceil(max(abs(xIntrinsic), abs(yIntrinsic))) + 1;
paddedFrame = padarray(transformedFrame, padSize, 'both');
croppedFrame = imcrop(paddedFrame, [xInstrinsic + padSize, yIntrinsic + padSize, 4095, 2159]);
result = croppedFrame;
The problem is now, while my result generated with OpenCV produces a perfect stabilized video, Matlab produces with the exact same data some kind of 'swimming'. The reference pixels aren't holding their position but move 1-2 pixels all the time, thus making the result useless.
I thought maybe the non integer numbers in the cropping are the problem so I changed my code to this:
flooredX = floor(xIntrinsic);
flooredY = floor(yIntrinsic);
fracX = xIntrinsic - flooredX;
fracY = yIntrinsic - flooredY;
fracTranslation = affine2d([1, 0 , 0; 0, 1, 0; fracX, fracY, 1]);
translatedFrame = imwarp(transformedFrame, fracTranslation);
padSize = 100;
paddedFrame = padarray(translatedFrame, padSize, 'both');
croppedFrame = imcrop(paddedFrame, [flooredX + padSize, flooredY + padSize, 4095, 2159]);
result = croppedFrame;
but the results are exactly the same.
It seems that this is an precision problem, but I can't see where Matlab could make numeric errors in this process. Does someone has a clue why the result differs from OpenCV?
Answers (0)
Categories
Find more on OpenCV Support in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!