2017-04-25 106 views
0

我试图做使用打开Cv2的的Python 2.7一个multipectral摄像机标定,以及代码与我从3单色有照片运作良好传感器(RED,NIR和GRE)和RGB。只有在代码中无效的图片才是来自REG传感器的图片(红色绿色)。摄像机标定

代码读取图片将它们转换为灰色然后发现拐角子像素,最后一个producres相机校准矩阵

我已经建立了多个打印的代码中的每个部分,这样我就知道这是不工作的具体部分,所以我知道这是不是在cv2.findChessboardCorners线工作的REG情况。

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() 

回答

0

如果同一段代码来自其他传感器工作了图像必须有什么问题你交给cv2.findChessboardCorners的图片。

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

cv2.findChessboardCorners还应该使用彩色图像,所以最简单的解决方案可能只是跳过颜色转换,如果这是弄乱你的图像。