[Python] Identifying outliers from openCV contours based on curvature?

Discussão em 'Python' iniciado por Stack, Julho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm processing a photo of the Earth from the Moon, as seen here. [​IMG]

    What I want to do is get the contour points that correspond to the edge of the Earth that is not in shadow. (e.g., the areas that should form an ellipsoid). I want the physical points on the edge of globe itself, and I'm trying to avoid using Gaussian blurring to avoid data destruction.

    I have this code that produces contours.

    import numpy as np
    import cv2 as cv

    # Read in Image, convert to grayscale
    image_path = r"moon_check.png"
    im = cv.imread(image_path)
    imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)

    # Threshold, then find contours
    ret, thresh = cv.threshold(imgray, 0, 1, cv.THRESH_BINARY + cv.THRESH_OTSU)
    contours, hierarchy = cv.findContours(thresh, cv.RETR_EXTERNAL , cv.CHAIN_APPROX_NONE)
    contours = sorted(contours, key=cv.contourArea, reverse=True)
    test = im
    cnt = contours[0]


    The resulting contour is pretty good:

    [​IMG]

    ... but I want to reduce the Contour elements to a non-contiguous one that excludes the shadows below the terminator, leaving me with only the curved elements near the top.

    I've thus far tried the following with poor results:


    1. Manual curvature sampling does work to a certain degree, but I've not found a good 2nd Order derivative check to exclude the regions at the bottom beyond some really manual tuning.


    2. OpenCV minEnclosingCircle doesn't fit very well, possibly due to the oblate spheroid nature of the Earth. Plus, going from the fit to keeping the points closest to that fit is fairly inelegant. I guess I could apply it iteratively and filter by min-distance to the enclosing circle, but that doesn't seem particularly efficient.


    3. I tried the OpenCV Hough's Circle, but it didn't work well using the grayscale image-- probably due to my avoidance of blurring the image etc.


    4. The shadow contours make a classic circle fit method like Kasa's or Coope's not work well.


    5. Convex matching should work for most of it, but many elements on the bottom are also convex.

    Any suggestions on methods to explore? Should I be changing how I approach the initial contour creation in the first place?

    Continue reading...

Compartilhe esta Página