使用OpenCV2和Python 2.7 / cv2.findChessboardCorners进行相机校准

时间:2017-04-25 10:47:03

标签: python-2.7 opencv

我正在尝试使用 Open Cv2 Python 2.7 进行多光谱相机校准,并且代码与我从3个单色传感器获得的图片效果很好(RED,NIR和GRE)和RGB。唯一没有在代码中工作的图片是REG传感器(红绿)的图片。

代码读取图片将其转换为灰色,然后找到角落 SubPixels ,最后生产相机校准矩阵

我已经在每个部分中构建了多个打印的代码,以便我知道哪个是不起作用的确切部分,所以我知道它不适用于 cv2.findChessboardCorners <中的REG情况/ strong>行。

    ret, Bords_Trouves = cv2.findChessboardCorners(Images_en_gris, Lignes_et_Collones, None)

所以我的问题是:

代码中没有用的是什么?或者这是REG传感器图片的问题,我将不得不尝试另一种方法将它们转换为灰色?或者是什么 ?

以下是所有代码:

    # -*- coding: utf-8 -*-
import numpy as np
import cv2
import glob
import math
import pickle
import matplotlib.pyplot as plt


critere = (cv2.TERM_CRITERIA_MAX_ITER | cv2.TERM_CRITERIA_COUNT, 10, 0.01)

Lignes = 13
Collones = 13
Collones_et_Lignes = (Collones, Lignes)
Lignes_et_Collones = (Lignes, Collones)

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

# Préparer deux tableaux pour sauvegarder les points objet et points images de totues les images trouvées.
Tableau_points_reels = []
Tableau_points_imaginaires = []

# Les photos du damier qu'on a pris pour le test
Source = "C:/Users/Mourad/Desktop/Calib1804/REG/"
Mes_Images = glob.glob(Source + '*.TIF')

print "les images ont bien étaient récupérées." if Mes_Images else "PROBLEME: images non récupérés !! "

for leo, fname in enumerate(Mes_Images):
    print("Image : " + fname)
    #if leo > 10:
     # break
    image_originale = cv2.imread(fname)

    Images_en_gris = cv2.cvtColor(image_originale, cv2.COLOR_BGR2GRAY)

    print "les images ont bien étaient transformés en gris." if Images_en_gris.all() else "PROBLEME: images non transformés en gris!! "

    ret, Bords_Trouves = cv2.findChessboardCorners(Images_en_gris, Lignes_et_Collones, None)

    print str(len(Bords_Trouves)) + " Bords trouvés" if ret else "PROBLEME: Bords Non trouvés !!"

    Tableau_points_reels.append(Points_Reels)

    PL = (11, 11)

    Plus_de_precision = cv2.cornerSubPix(Images_en_gris, Bords_Trouves[1], PL, (-1, -1), critere)


    print "Sub pixels trouvées" if Plus_de_precision else "PROBLEME: Pas de sub pixels trouvées"

    Tableau_points_imaginaires.append(Bords_Trouves)

   # far = cv2.drawChessboardCorners(image_originale, Lignes_et_Collones, Bords_Trouves, ret)

    #cv2.imshow("Bords trouvées déssinés sur l'image originale", image_originale)
    #cv2.waitKey(500)
    #()
print "Nombre de points réels trouvés: " + str(len(Tableau_points_reels))
print "Nombres de points imaginaires trouvés: " + str(len(Tableau_points_imaginaires))

h, w = Images_en_gris.shape[:2]

derik, matrice, distortion, vecteur_de_rotation, vecteur_de_translation = cv2.calibrateCamera(Tableau_points_reels, Tableau_points_imaginaires, (w, h), None, None, flags=cv2.CALIB_RATIONAL_MODEL)

print "La matrice de calibration est: "
print matrice
print "La distortion est egale a: "
print distortion
print "Le vecteur de rotation est egal a: "
print vecteur_de_rotation
print "Le vecteur de translation est egal a: "
print vecteur_de_translation

print "\n La matrice de calibration trouvée et données récupérés" if derik else "PROBLEME: Pas de calibration"

newcameramtx, roi = cv2.getOptimalNewCameraMatrix(matrice, distortion, (w, h), 1, (w, h))

# undistortion
Image_calibree = cv2.undistort(image_originale, matrice, distortion, None, newcameramtx)

fgh = cv2.imread("C:/Users/Mourad/Desktop/Calib1804/RGB/IMG_700101_000800_0000_RGB.JPG")

h, w = fgh.shape[:2]

x, y, w, h = roi
Image_calibree = Image_calibree[y:y+h, x:x+w]
cv2.imwrite('Desktop/imagecalibre.png', Image_calibree)

plt.subplot(121), plt.imshow(fgh), plt.title('image originale')
plt.subplot(122), plt.imshow(Image_calibree), plt.title('image calibree')
plt.show()

Erreur_totale = 0
for i in xrange(len(Tableau_points_reels)):
    imgpoints2, _ = cv2.projectPoints(Tableau_points_reels[i], vecteur_de_rotation[i], vecteur_de_translation[i], matrice, distortion)
    Erreur = cv2.norm(Tableau_points_imaginaires[i], imgpoints2, cv2.NORM_L2)/len(imgpoints2)
    Erreur_totale += Erreur
    print "Erreur totale: ", Erreur_totale/len(Tableau_points_reels)

cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

如果同一段代码适用于其他传感器的图像,那么您交给cv2.findChessboardCorners的图片就会出现问题。

在进入该功能之前,尝试用cv2.imshow可视化图像。可能颜色转换是错误的,因为cv2.COLOR_BGR2GRAY将3通道图像转换为灰度,但如果我理解你的格式正确,你应该有两个通道。

cv2.findChessboardCorners也应该使用彩色图像,因此最简单的解决方案可能就是跳过颜色转换,如果那是弄乱图像的。