Hi, in the source code for selectAndRefineChessboardCorners I don’t understand line 161 and 170 here: opencv/modules/objdetect/src/aruco/charuco_detector.cpp at 5.x · opencv/opencv · GitHub
Before cornerSubPix (0.5, 0.5) is subtracted from the corner, and afterwards (0.5, 0.5) is added back. The line is commented “// adjust sub-pixel coordinates for cornerSubPix”
Can anyone explain this comment and why the pixel shift is necessary?
It probably has to do with a difference in the origin (where 0,0 is) for a pixel. For example, if cornersubpix operates on pixels and treats the top left of a pixel as the 0,0 reference, but other parts of the processing treat the center of a pixel as the 0,0 reference, then a half pixel shift is needed to transform between those two coordinate systems.
In much (most?) of openCV, the center of the pixel is considered the origin. So if you call cv::projectPoints, for example, and get a result of (0.0,0.0) that means the projected points lands at the center of the upper-left pixel, not the top/left of the upper-left pixel. This has implications when warping images to be displayed in a program due to the difference in pixel origin treatment. (I usually build the half pixel into the warp/remap step because it makes it easier to handle on the UI side, but the specific context matters, of course.)