# Position coordinates on geometric image

**URL:** <https://forum.opencv.org/t/position-coordinates-on-geometric-image/7488>\
**Category:** Python\
**Created:** [February 9, 2022, 12:02pm UTC](https://forum.opencv.org/t/position-coordinates-on-geometric-image/7488 "2022-02-09T12:02:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![YorelNation](https://avatars.discourse-cdn.com/v4/letter/y/49beb7/32.png) [@YorelNation](https://forum.opencv.org/u/YorelNation)\
**Post date:** [February 9, 2022, 12:02pm UTC](https://forum.opencv.org/t/position-coordinates-on-geometric-image/7488/1 "2022-02-09T12:02:12Z")

</div>

Hi,

It’s the first time I post in this forum and to be honest the first time I post to ask help on a forum for a data science project at all !

So here is my issue. I want to position 6 points on an image of a cylindrical shape to apply a transformation to unbend it (I should be able to do this once I’ve found all my pixel point positions). The image looks like this after a few pre-processing steps:

```auto
img = cv2.imread('./img/wine2.jpg')

gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 21, 4)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours]

cnt = max(contour_sizes, key=lambda x: x[0])[1]

# Create a mask of the label
mask = np.zeros(img.shape,np.uint8)
cntr = cv2.drawContours(mask, [cnt],0,255,-1)
edges = cv2.Canny(cntr ,140, 210)

```

 ![](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/ae2f83be5ee4c6dc977753fd8add868b05ca3f55.png)

Ideally I want to position my points like this:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/3/3bd19b163f56b741c740dc6bfcaf931f24b33a54.png)

The problem is that I struggle to find a way to do this.  
As an example, I’ve tried to detect the left edge to then apply simple loops to get the first pixel at the top and the first pixel at the bottom with some for loops like so:

```auto
ddepth = cv2.CV_8U
borderType=cv2.BORDER_DEFAULT
left = cv2.Sobel(mask,ddepth,1,0,ksize=1,scale=1,delta=0,borderType=borderType)
right = cv2.Sobel(mask,ddepth,1,0,ksize=1,scale=-1,delta=0, borderType=borderType)
top = cv2.Sobel(mask,ddepth,0,1,ksize=1,scale=1,delta=0,borderType=borderType)
bottom = cv2.Sobel(mask,ddepth,0,1,ksize=1,scale=-1,delta=0,borderType=borderType)

# Remove noise from borders
kernel = np.ones((2,2),np.uint8)
left_border = cv2.erode(left,kernel,iterations = 1)
right_border = cv2.erode(right,kernel,iterations = 1)
top_border = cv2.erode(top,kernel,iterations = 1)
bottom_border = cv2.erode(bottom,kernel,iterations = 1)

xa = 0
ya = 0

for row in range(left_border.shape[0]):
    if left_border[row,:].sum() > 0:
        ya = row
        break

for col in range(left_border.shape[1]):
    
    if left_border[:,col].sum() > 0:
        xa = col
        break

# Getting F point

xf = 0
yf = 0

for row in reversed(range(left_border.shape[0])):
    if left_border[row,:].sum() > 0:
        yf = row
        break

```

 ![](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/1/17438f78a861c39fd014fd1e2bab6551a4199cfd.png)

The problem is that even if it worked for the A point, the left border obtained earlier with cv2.erode (hand draw in green) has some noise on top and bottom and as a result when i try to get the yf coordinate by “scanning” my image from the bottom to the top It stops on a little bottom pixel rather than detecting the first bottom pixel of my left geometric border.

So I need your help, should I try to find a way to simplify the representation of my borders ? (smoothing? And if so how ?), or is there a beter method to find a way to automatically position these wanted coordinates ?

I hope I’ve been clear enough, I wish you all to have a nice day !

---

<div class="post-metadata">

**Author:** ![Steve\_in\_Denver](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Steve\_in\_Denver](https://forum.opencv.org/u/Steve_in_Denver)\
**Post date:** [February 9, 2022, 5:22pm UTC](https://forum.opencv.org/t/position-coordinates-on-geometric-image/7488/2 "2022-02-09T17:22:14Z")

</div>

If your shape more or less looks the same (straight lines left/right, curved lines top/bottom) from run to run, I’d consider fitting a line to the left/right edges and an ellipse to the top / bottom edges (assuming an ellipse models it correctly) and then computing the intersection of the lines with the ellipses to get your corner points. Once you have those you could use cv::cornerSubpix() on those points to get a more accurate location.

---

<div class="post-metadata">

**Author:** ![YorelNation](https://avatars.discourse-cdn.com/v4/letter/y/49beb7/32.png) [@YorelNation](https://forum.opencv.org/u/YorelNation)\
**Post date:** [February 9, 2022, 5:43pm UTC](https://forum.opencv.org/t/position-coordinates-on-geometric-image/7488/3 "2022-02-09T17:43:17Z")

</div>

Thanks for your awnser that’s actually a solution I considered but I wouldn’t know ho to start fitting regression on a ndarray and still the noise would be present and thus could bend the fit.

---

<div class="post-metadata">

**Author:** ![Steve\_in\_Denver](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Steve\_in\_Denver](https://forum.opencv.org/u/Steve_in_Denver)\
**Post date:** [February 9, 2022, 8:06pm UTC](https://forum.opencv.org/t/position-coordinates-on-geometric-image/7488/4 "2022-02-09T20:06:19Z")

</div>

It’s been a while, but I did something similar (2D curved surface fit) with (I think) the Gnu Scientific Library (GSL). I don’t remember the details, but I think I used a function called QRSolve() (a QR decomposition method) to find the coefficients to a polynomial. I’m not at all saying this is the best way to approach the problem, but it worked well for me.

As far as your outliers, you could fit your curve, filter the bad outliers, and fit the curve to the inliers only (iterating a few times)…or maybe a RANSAC approach.

It seems workable to me.

---

<div class="post-metadata">

**Author:** ![YorelNation](https://avatars.discourse-cdn.com/v4/letter/y/49beb7/32.png) [@YorelNation](https://forum.opencv.org/u/YorelNation)\
**Post date:** [February 9, 2022, 9:11pm UTC](https://forum.opencv.org/t/position-coordinates-on-geometric-image/7488/5 "2022-02-09T21:11:08Z")

</div>

Thanks for your insights !

---

<div class="post-metadata">

**Author:** ![bren3232](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@bren3232](https://forum.opencv.org/u/bren3232)\
**Post date:** [February 9, 2022, 10:30pm UTC](https://forum.opencv.org/t/position-coordinates-on-geometric-image/7488/6 "2022-02-09T22:30:07Z")

</div>

math.hypot through all contours from top right of image, or somewhere in that quadrant. Where the hypot length is the longest is where that corner is.

---

<div class="post-metadata">

**Author:** ![bren3232](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@bren3232](https://forum.opencv.org/u/bren3232)\
**Post date:** [February 9, 2022, 11:48pm UTC](https://forum.opencv.org/t/position-coordinates-on-geometric-image/7488/7 "2022-02-09T23:48:50Z")

</div>

Or math.hypot from somewhere above where you drew the “F(o” on the image, and take the shortest length.

---

<div class="post-metadata">

**Author:** ![YorelNation](https://avatars.discourse-cdn.com/v4/letter/y/49beb7/32.png) [@YorelNation](https://forum.opencv.org/u/YorelNation)\
**Post date:** [February 10, 2022, 11:12am UTC](https://forum.opencv.org/t/position-coordinates-on-geometric-image/7488/8 "2022-02-10T11:12:42Z")

</div>

Thanks for your ideas Bren, I will try to look into this !  
Actually I’ve been going back a bit in my process because now it seems that I’m rarely capable of extracting the blue geometric pattern from all my samples images.

I saw a post on this forum where they recommend to train a U-net with manually labeling masks so the neural net is able to recognize the pattern.

---

<div class="post-metadata">

**Author:** ![1117.jx.100510](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/1117.jx.100510/32/11064_2.png) [@1117.jx.100510](https://forum.opencv.org/u/1117.jx.100510)\
**Post date:** [September 5, 2024, 9:40am UTC](https://forum.opencv.org/t/position-coordinates-on-geometric-image/7488/9 "2024-09-05T09:40:28Z")

</div>

You solve this problem inside？Availability use unet get good outcome
