How to get the image pixel at real locations in opencv?

前端 未结 4 2033
臣服心动
臣服心动 2020-12-08 15:49

I want to retrieve the rgb of a pixel in the image. But the location is not integer location but real values (x,y). I want a bilinear interpolated value. How could

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 16:05

    Unfortunately I don't have enough points to post this as a comment on the accepted answer... I adjusted the code to suit my own problem which requires interpolation on a single channel matrix of floats.

    I thought I'd like some intuition of which of the approaches are the fastest.

    I implemented the 3 methods from Andrey Kamaev's answer as well as a simple nearest neighbour (basically just rounding off the co-ordinates).

    I ran an experiment with a matrix A(100x100) which I just filled with garbage. I then made a matrix B(400x400) which is filled with values interpolated from a such that: B(i,j) = A(i/4, j/4).

    Each run was done 1000 times and here are the average times:

    • Nearest Neighbour: 2.173 ms
    • getRectSubPix: 26.506 ms
    • remap: 114.265 ms
    • manual: 5.086 ms
    • manual without borderInterpolate: 3.842 ms

    So nearest neighbour for super speed if you don't really care about the actual interpolation too much and just need a value - particularly if your data varies very smoothly. For anything else just I'd go with the manual bilinear interpolation as it seems consistently faster than the other methods. (OpenCV 2.4.9 - Ubuntu 15.10 Repo - Feb 2016).

    If you know all 4 your contributing pixels are within the bounds of your matrix, then your can make it basically equivalent in time to Nearest Neighbour - although the difference is pretty negligible anyway.

提交回复
热议问题