Search code examples
pythonlibraries

Plot digitizer for single datapoint Python


I need to analyse 900 of these frames, that is giving the angle of the little cardboard piece with respect to the polar. Is there any library that I can use to automate the process in python? I would like to feed the program with the folder directory and that it would give back a list with all the angles in each frame.enter image description here


Solution

  • OpenCV is a powerful tool for image manipulation.

    I've tried to come up with a solution for you problem, but I do not know how stable this solution is across the 900 images. In particular, the 0° - 180° axis must not be significantly tilted, as I am not correcting for that.

    enter image description here

    import cv2 as cv
    import numpy as np
    
    #load image
    img = cv.imread(r'path_to_your_image.jpg', cv.IMREAD_GRAYSCALE)
    
    #detect circle
    circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 100,
                                   param1=100, param2=30,
                                   minRadius=50, maxRadius=500)
    # get circle center (raise Exception if circle was not detected)
    try:
        center = (int(circles[0,0,0]), int(circles[0,0,1]))
    except IndexError:
        raise Exception("Unable to identify center.")
    # plot center of circle
    cv.circle(img, center, 1, (0, 100, 100), 3)
    
    # dilate and threshold image to only see the rectangle as small dot
    kernel = np.ones((35, 35), np.uint8)
    img_dilate = cv.dilate(img, kernel, iterations=1)
    _, img_dilate_thres = cv.threshold(img_dilate,120,255,cv.THRESH_BINARY)
    # get center position of remaining dot
    rect_x = np.argmin(img_dilate_thres.mean(axis=0))
    rect_y = np.argmin(img_dilate_thres.mean(axis=1))
    cv.circle(img, (rect_x, rect_y), 1, (0, 100, 100), 3)
    
    # get angle between circle center and patch
    angle = np.arctan2(center[1] - rect_y, rect_x - center[0])
    degree = angle / (2*np.pi) * 360
    
    # display angle, wait for user to close image
    font = cv.FONT_HERSHEY_SIMPLEX
    cv.putText(img, f'{degree:.1f}', (rect_x, rect_y), font, 2, (0, 255, 0), 2, cv.LINE_AA)
    cv.imshow("detected circles", img)
    cv.waitKey(0)