I donβt know exactly what OpenCV is, but I would suggest the following:
The selected cluster of pixels has a maximum width of one point - w - so let's say that there are vertical columns of pixels in the area. Now I would weight the columns according to the number of pixels contained in the column, and use these columns to determine the horizontal center point.
The same algorithm can also work for the X-center.
source share