[Python] Can someone help me understand why my corners detection code is not working?

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    I want to do camera calibration using a chessboard. And to do that i have developped a code based on some corner detection scripts I found on internet.

    The actual issue i am facing is that the my script is able to detect corners on a chessboard whose image has been taken by a camera (let's call it camera 1). But the same script is unable to dectect corners on other chessboards whose images has been taken with two other cameras (let's say camera 2 and camera 3).

    I've tried many things to understand and solve the issue, but all my attempts have failed. For eg , I've tried the script on the same image while varying the my specified chessboard size but this attempt failed. I've also tried to take images a different period of the day (because I was wondering if ambient luminosity affect the detection) but with my failed tests I guess the issue was not there.

    Currently, I am lost. I don't what else I can try.

    I attach to this description one image of chessboard from each camera.camera1_chessboard, camera2_chessboard, camera3_chessboard

    [camera1_chessboard][1]
    [camera2_chessboard][2]
    [camera3_chessboard][3]


    Here is my code:

    import numpy as np
    import cv2 as cv
    import glob
    import pickle
    import os

    chessboardSize = (50,15) # Nombre de coins dans chaque direction du damier

    # Critère d'arrêt des itérations de recherches de coins
    # 30 itérations max ou erreur entre 2 itérations < 0.001
    criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)

    threedpoints = [] # Vecteur de points 3D
    twodpoints = [] # Vecteur de points 2D

    objectp3d = np.zeros((1, chessboardSize[0]*chessboardSize[1], 3), np.float32)
    objectp3d[0, :, :2] = np.mgrid[:chessboardSize[0], :chessboardSize[1]].T.reshape(-1,
    2)


    path = "Camera2.jpeg"

    image = cv.imread(path)
    img_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

    # Si nombre de coin attendu est trouvé alors return cornersFound = True
    cornersFound, corners = cv.findChessboardCorners(img_gray, chessboardSize, None)
    # If found, add object points, image points (after refining them)
    print(cornersFound)
    if cornersFound == True:
    threedpoints.append(objectp3d)
    corners2 = cv.cornerSubPix(img_gray,corners, (11,11), (-1,-1), criteria)
    twodpoints.append(corners2)
    # Draw and display the corners
    for corner in corners2:
    x, y = corner.ravel()
    cv.circle(image, (int(x), int(y)), 4, (0, 0, 255), -1)

    ####################### Nomenclature de l'image avec coins détectés
    #########################################################
    directory, file = os.path.split(path)
    base_name, _ = os.path.splitext(file)

    new_format = ".jpg"
    coin_name = f"{base_name}_Corners{new_format}"
    cv.imshow('img', image)
    cv.waitKey(0)
    cv.imwrite(coin_name, image)
    cv.destroyAllWindows()

    ret, cameraMatrix, dist, rvecs, tvecs = cv.calibrateCamera(threedpoints, twodpoints,
    img_gray.shape[::-1], None, None)

    print("\nCamera Calibrated: ", ret) # ret est l'erreur de calibration
    print("\nCamera Matrix:\n", cameraMatrix)
    print("\nDistorsion Coefficients:\n", dist)
    # print("\nRotation Vectors:\n", rvecs)
    # print("\nTranslation Vectors:\n", tvecs)


    Can someone help me? please!

    Continue reading...

Compartilhe esta Página