Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
ws1314:die_zukunft [2014/02/13 15:13] maximilian.obst |
ws1314:die_zukunft [2016/05/10 14:46] (aktuell) |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
================ Robotik - Wie es weitergeht ================= | ================ Robotik - Wie es weitergeht ================= | ||
- | Nächstes Mal: Punkte in einem Bild erkennen! Siehe Funktion filterSpeckles und Programm BildverarbeitenGruppe4 | + | Das Projekt geht nun seinem Ende entgegen, es folgt jetzt nur noch die morgige Präsentation. Wer möchte, kann natürlich aufbauend auf den hier vorgestellten Inhalten das Projekt weiterführen oder sein eigenes Projekt hierauf aufbauen. Viel Erfolg dabei! |
[[Robotik]] | [[Robotik]] | ||
- | |||
- | import numpy as np | ||
- | import cv2 | ||
- | import pylab | ||
- | from scipy.signal import convolve2d | ||
- | import threading | ||
- | import time | ||
- | |||
- | |||
- | def lies_kamera(): | ||
- | |||
- | global bild_aktuell | ||
- | while not stop_flag: | ||
- | print "Bild gelesen" | ||
- | res,bild_aktuell=capture.read() | ||
- | |||
- | |||
- | |||
- | capture= cv2.VideoCapture(1) | ||
- | stop_flag=False | ||
- | kamera_thread=threading.Thread(target=lies_kamera) | ||
- | kamera_thread.start() | ||
- | time.sleep(2) | ||
- | |||
- | while True: | ||
- | #res,bild=capture.read() | ||
- | #bild_grau=cv2.cvtColor(bild,cv2.cv.CV_BGR2GRAY) | ||
- | bild=bild_aktuell.copy() | ||
- | cv2.namedWindow("Fenster 1") | ||
- | cv2.namedWindow("Fenster Rot") | ||
- | cv2.namedWindow("Fenster Blau") | ||
- | cv2.imshow("Fenster 1",bild) | ||
- | cv2.waitKey(50) | ||
- | |||
- | #print bild.shape | ||
- | #print bild#_grau | ||
- | """ | ||
- | weights=np.array([[0,1,1,1,1,1,0],[1,1,2,2,2,1,1],[1,2,2,3,2,2,1],[1,2,4,8,4,2,1],[1,2,2,3,2,2,1],[1,1,2,2,2,1,1],[0,1,1,1,1,1,0]]) | ||
- | bild_blau = convolve2d(bild_blau,weights) #blurrt das bild | ||
- | |||
- | print bild_grau.max() #findet den hellsten wert, ausser wenn es ausrutscht | ||
- | |||
- | indexmass = bild_grau.argmax() #dieser part findet die pixelkoordinaten des hellsten punktes(normalerweise) | ||
- | print indexmass""" | ||
- | bildhsv= cv2.cvtColor(bild,cv2.COLOR_BGR2HSV) | ||
- | bild_blau=cv2.inRange(bildhsv,np.array([100,0,200]),np.array( [110,255,255])) #fur PS-Move | ||
- | bild_rot=cv2.inRange(bildhsv,np.array([24,0,200]),np.array( [24,255,255])) #fur PS-Move | ||
- | cv2.filterSpeckles(bild_blau, 00, 1, 1) | ||
- | #Hier Kameraeinstellungen fur PS-Move: Helligkeit 0 Kontrast 255 Sattigung 255 Alles 0 ausser Scharfe 63 | ||
- | cv2.imshow("Fenster Rot", bild_rot) | ||
- | cv2.imshow("Fenster Blau", bild_blau) | ||
- | """ | ||
- | index_x = (indexmass - (indexmass % 640))/640 | ||
- | print index_x | ||
- | index_y = indexmass % 640 | ||
- | print index_y | ||
- | |||
- | cv2.circle(bild_grau, (index_y,index_x), 10,(86,250,150)) #malt einen kreis um den hellsten punkt | ||
- | cv2.imshow("Fenster 2",bild_grau)""" | ||
- | cv2.waitKey(30) | ||
- | |||
- | capture.release() | ||
- | |||
- | #!/usr/bin/env python | ||
- | # -*- coding: utf-8 -*- | ||
- | # | ||
- | # unbenannt.py | ||
- | # | ||
- | # Copyright 2013 Stefan Born <born@math.tu-berlin.de> | ||
- | # | ||
- | # This program is free software; you can redistribute it and/or modify | ||
- | # it under the terms of the GNU General Public License as published by | ||
- | # the Free Software Foundation; either version 2 of the License, or | ||
- | # (at your option) any later version. | ||
- | # | ||
- | # This program is distributed in the hope that it will be useful, | ||
- | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
- | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
- | # GNU General Public License for more details. | ||
- | # | ||
- | # You should have received a copy of the GNU General Public License | ||
- | # along with this program; if not, write to the Free Software | ||
- | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
- | # MA 02110-1301, USA. | ||
- | # | ||
- | # | ||
- | |||
- | |||
- | |||
- | import numpy as np | ||
- | import cv2 | ||
- | from thread import start_new_thread | ||
- | from time import sleep | ||
- | |||
- | KAMERA_NR=1 | ||
- | STOP_FLAG=False | ||
- | |||
- | def lies_kamera(): | ||
- | ''' Endloschleife um Kamera auslesen; | ||
- | das zuletzt gelesene Bild befindet sich in der globalen Variable bild_aktuell''' | ||
- | global bild_aktuell | ||
- | n=0 | ||
- | while not STOP_FLAG: | ||
- | n+=1 | ||
- | #print "Frame " ,n | ||
- | |||
- | # Ein Bild auslesen | ||
- | res,bild=capture.read() | ||
- | bild_aktuell=bild.copy() | ||
- | |||
- | |||
- | |||
- | ## Kamera initialisieren | ||
- | |||
- | capture= cv2.VideoCapture(KAMERA_NR) | ||
- | capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH,640) | ||
- | capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT,480) | ||
- | sleep(5) | ||
- | #capture.set(cv2.cv.CV_CAP_PROP_FPS,60.) | ||
- | capture.set(cv2.cv.CV_CAP_PROP_SATURATION,0.1) | ||
- | capture.set(cv2.cv.CV_CAP_PROP_BRIGHTNESS,0.0) | ||
- | #capture.set(cv2.cv.CV_CAP_PROP_EXPOSURE,0) | ||
- | start_new_thread(lies_kamera,()) | ||
- | sleep(2) | ||
- | |||
- | cv2.namedWindow("Bild") | ||
- | |||
- | ###### Schleife, die das Schachbrettmuster im Bild sucht, | ||
- | ###### und die gefundenen Punkte in img_points speichert. | ||
- | ###### | ||
- | |||
- | abbruch=False | ||
- | |||
- | pattern_size = (9, 6) | ||
- | pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 ) | ||
- | pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)*2.54 | ||
- | # das Kalibrierungsbrett hat 10x 7 1 inch * 1 inch-Felderpyth | ||
- | |||
- | obj_points = [] | ||
- | img_points = [] | ||
- | h, w = 0, 0 | ||
- | |||
- | |||
- | |||
- | while not abbruch: | ||
- | |||
- | img=bild_aktuell.copy() | ||
- | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) | ||
- | |||
- | h, w = img.shape[:2] | ||
- | found, corners = cv2.findChessboardCorners(img, pattern_size) | ||
- | if found: | ||
- | term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 ) | ||
- | cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), term) | ||
- | |||
- | cv2.drawChessboardCorners(img, pattern_size, corners, found) | ||
- | |||
- | |||
- | print 'ok' | ||
- | cv2.imshow('Bild',img) | ||
- | key=cv2.waitKey(0) | ||
- | if not(key==ord('u')): | ||
- | img_points.append(corners.reshape(-1, 2)) | ||
- | obj_points.append(pattern_points) | ||
- | |||
- | if key==ord('q'): | ||
- | abbruch=True | ||
- | |||
- | cv2.imshow('Bild',img) | ||
- | key=cv2.waitKey(20) | ||
- | if key==ord('q'): | ||
- | abbruch=True | ||
- | |||
- | camera_matrix = np.zeros((3, 3)) | ||
- | dist_coefs = np.zeros(4) | ||
- | img_n = len(img_points) | ||
- | rvecs = np.array([np.zeros(3) for i in xrange(img_n)]) | ||
- | tvecs = np.array([np.zeros(3) for i in xrange(img_n)]) | ||
- | rms, camera_matrix,dist_coefs,rvecs,tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), camera_matrix, dist_coefs , rvecs, tvecs) | ||
- | print rms | ||
- | print "Kamera Matrix ", camera_matrix | ||
- | print "Distortion Coefficients ", dist_coefs | ||
- | print "Rotation ", rvecs | ||
- | print "Translation ", tvecs | ||
- | cv2.destroyAllWindows() | ||
- | STOP_FLAG=True | ||
- | |||
- | #!/usr/bin/env python | ||
- | # -*- coding: utf-8 -*- | ||
- | # | ||
- | # kalibrierung-stereo2.py | ||
- | # | ||
- | # Copyright 2013 Stefan Born <born@math.tu-berlin.de> | ||
- | # | ||
- | # This program is free software; you can redistribute it and/or modify | ||
- | # it under the terms of the GNU General Public License as published by | ||
- | # the Free Software Foundation; either version 2 of the License, or | ||
- | # (at your option) any later version. | ||
- | # | ||
- | # This program is distributed in the hope that it will be useful, | ||
- | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
- | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
- | # GNU General Public License for more details. | ||
- | # | ||
- | # You should have received a copy of the GNU General Public License | ||
- | # along with this program; if not, write to the Free Software | ||
- | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
- | # MA 02110-1301, USA. | ||
- | # | ||
- | # | ||
- | |||
- | |||
- | |||
- | import numpy as np | ||
- | import cv2 | ||
- | import pickle | ||
- | from thread import start_new_thread | ||
- | from time import sleep | ||
- | |||
- | KAMERA1_NR=1 | ||
- | KAMERA2_NR=2 | ||
- | STOP_FLAG=False | ||
- | |||
- | def lies_kamera(): | ||
- | ''' Endloschleife um Kamera auslesen; | ||
- | das zuletzt gelesene Bild befindet sich in der globalen Variable bild_aktuell''' | ||
- | global bild1_aktuell | ||
- | global bild2_aktuell | ||
- | n=0 | ||
- | while not STOP_FLAG: | ||
- | n+=1 | ||
- | #print "Frame " ,n | ||
- | |||
- | # Ein Bild auslesen | ||
- | res,bild=capture1.read() | ||
- | bild1_aktuell=bild.copy() | ||
- | res,bild=capture2.read() | ||
- | bild2_aktuell=bild.copy() | ||
- | |||
- | |||
- | |||
- | ## Kamera initialisieren | ||
- | |||
- | capture1= cv2.VideoCapture(KAMERA1_NR) | ||
- | capture1.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH,640) | ||
- | capture1.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT,480) | ||
- | sleep(2) | ||
- | #capture1.set(cv2.cv.CV_CAP_PROP_FPS,60.) | ||
- | capture1.set(cv2.cv.CV_CAP_PROP_CONTRAST,0.1) | ||
- | capture1.set(cv2.cv.CV_CAP_PROP_SATURATION,0.1) | ||
- | capture1.set(cv2.cv.CV_CAP_PROP_BRIGHTNESS,0.0) | ||
- | capture1.set(cv2.cv.CV_CAP_PROP_GAIN,0.1) | ||
- | |||
- | capture2= cv2.VideoCapture(KAMERA2_NR) | ||
- | capture2.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH,640) | ||
- | capture2.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT,480) | ||
- | sleep(2) | ||
- | #capture2.set(cv2.cv.CV_CAP_PROP_FPS,60.) | ||
- | capture2.set(cv2.cv.CV_CAP_PROP_CONTRAST,0.1) | ||
- | capture2.set(cv2.cv.CV_CAP_PROP_SATURATION,0.1) | ||
- | capture2.set(cv2.cv.CV_CAP_PROP_BRIGHTNESS,0.0) | ||
- | capture2.set(cv2.cv.CV_CAP_PROP_GAIN,0.1) | ||
- | #capture.set(cv2.cv.CV_CAP_PROP_EXPOSURE,0) | ||
- | |||
- | |||
- | start_new_thread(lies_kamera,()) | ||
- | sleep(4) | ||
- | |||
- | cv2.namedWindow("Bild1") | ||
- | cv2.namedWindow("Bild2") | ||
- | |||
- | ###### Schleife, die das Schachbrettmuster im Bild sucht, | ||
- | ###### und die gefundenen Punkte in img_points speichert. | ||
- | ###### | ||
- | |||
- | abbruch=False | ||
- | |||
- | pattern_size = (9, 6) | ||
- | pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 ) | ||
- | pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)*2.54 | ||
- | # das Kalibrierungsbrett hat 10x 7 1 inch * 1 inch-Felderpyth | ||
- | |||
- | obj_points1 = [] | ||
- | img_points1 = [] | ||
- | obj_points2 = [] | ||
- | img_points2 = [] | ||
- | |||
- | h, w = 0, 0 | ||
- | |||
- | |||
- | |||
- | while not abbruch: | ||
- | |||
- | img1=bild1_aktuell.copy() | ||
- | gray1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY) | ||
- | img2=bild2_aktuell.copy() | ||
- | gray2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY) | ||
- | |||
- | |||
- | h, w = img1.shape[:2] | ||
- | found1, corners1 = cv2.findChessboardCorners(img1, pattern_size) | ||
- | found2, corners2 = cv2.findChessboardCorners(img2, pattern_size) | ||
- | |||
- | if found1 and found2: | ||
- | term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 ) | ||
- | cv2.cornerSubPix(gray1, corners1, (11, 11), (-1, -1), term) | ||
- | cv2.cornerSubPix(gray2, corners2, (11, 11), (-1, -1), term) | ||
- | |||
- | cv2.drawChessboardCorners(img1, pattern_size, corners1, found1) | ||
- | cv2.drawChessboardCorners(img2, pattern_size, corners2, found2) | ||
- | |||
- | |||
- | |||
- | print 'ok' | ||
- | cv2.imshow('Bild1',img1) | ||
- | cv2.imshow('Bild2',img2) | ||
- | |||
- | key=cv2.waitKey(0) | ||
- | if not(key==ord('u')): | ||
- | img_points1.append(corners1.reshape(-1, 2)) | ||
- | obj_points1.append(pattern_points) | ||
- | img_points2.append(corners2.reshape(-1, 2)) | ||
- | obj_points2.append(pattern_points) | ||
- | |||
- | if key==ord('q'): | ||
- | abbruch=True | ||
- | |||
- | cv2.imshow('Bild1',img1) | ||
- | cv2.imshow('Bild2',img2) | ||
- | key=cv2.waitKey(20) | ||
- | if key==ord('q'): | ||
- | abbruch=True | ||
- | |||
- | K1=file("Kameramatrix1","r") | ||
- | p=pickle.Unpickler(K1) | ||
- | |||
- | camera_matrix1 = p.load()#np.array([[552.718,0.,318.714],\ | ||
- | #[ 0. ,552.567,208.716],\ | ||
- | #[ 0. ,0.,1. ]]) | ||
- | K1.close() | ||
- | K2=file("Kameramatrix2","r") | ||
- | p=pickle.Unpickler(K2) | ||
- | camera_matrix2= p.load()#np.array([[568.1268,0.,299.0498],\ | ||
- | #[ 0. ,569.2292,236.8351],\ | ||
- | #[ 0. ,0.,1. ]]) | ||
- | K2.close() | ||
- | |||
- | dist_coefs1 = np.array([-0.1384 , 0.3435 , -0.0055 , 0.0060 , -0.3011]) | ||
- | #camera_matrix2 = np.zeros((3, 3)) | ||
- | dist_coefs2 = np.array([-0.1208 , 0.3704 , -0.0079 , 0.0010 , -0.3450]) | ||
- | #R=np.zeros((3,3)) | ||
- | #T=np.zeros(3) | ||
- | #E=np.zeros((3,3)) | ||
- | #F=np.zeros((3,3)) | ||
- | |||
- | img_n = len(img_points1) | ||
- | #rvecs = np.array([np.zeros(3) for i in xrange(img_n)]) | ||
- | #tvecs = np.array([np.zeros(3) for i in xrange(img_n)]) | ||
- | flags = 0 | ||
- | flags |= cv2.CALIB_FIX_INTRINSIC | ||
- | #flags |= cv2.CALIB_USE_INTRINSIC_GUESS | ||
- | #flags |= cv2.CALIB_FIX_PRINCIPAL_POINT | ||
- | #flags |= cv2.CALIB_FIX_FOCAL_LENGTH | ||
- | flags |= cv2.CALIB_FIX_ASPECT_RATIO | ||
- | flags |= cv2.CALIB_ZERO_TANGENT_DIST | ||
- | flags |= cv2.CALIB_SAME_FOCAL_LENGTH | ||
- | flags |= cv2.CALIB_RATIONAL_MODEL | ||
- | flags |= cv2.CALIB_FIX_K3 | ||
- | flags |= cv2.CALIB_FIX_K4 | ||
- | flags |= cv2.CALIB_FIX_K5 | ||
- | term_crit = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, 100, 1e-5) | ||
- | |||
- | rms, camera_matrix1,dist_coefs1,camera_matrix2, distcoefs2,R,T,E,F =\ | ||
- | cv2.stereoCalibrate(obj_points1, img_points1, img_points2,(w,h),cameraMatrix1=camera_matrix1,distCoeffs1=dist_coefs1, cameraMatrix2=camera_matrix2,distCoeffs2=dist_coefs2,\ | ||
- | criteria=term_crit,flags=flags)#,camera_matrix1,dist_coefs1,camera_matrix2,dist_coefs2,\ | ||
- | #R,T,E,F) | ||
- | |||
- | K1=file("Kameramatrix1","w") | ||
- | p=pickle.Pickler(K1) | ||
- | p.dump(camera_matrix1) | ||
- | K1.close() | ||
- | K2=file("Kameramatrix2","w") | ||
- | p=pickle.Pickler(K2) | ||
- | p.dump(camera_matrix2) | ||
- | K2.close() | ||
- | Rot=file("Rotationsmatrix","w") | ||
- | p=pickle.Pickler(Rot) | ||
- | p.dump(R) | ||
- | Rot.close() | ||
- | Trans=file("Translationsmatrix","w") | ||
- | p=pickle.Pickler(Trans) | ||
- | p.dump(T) | ||
- | Trans.close() | ||
- | EM=file("E","w") | ||
- | p=pickle.Pickler(EM) | ||
- | p.dump(E) | ||
- | EM.close() | ||
- | FM=file("F","w") | ||
- | p=pickle.Pickler(FM) | ||
- | p.dump(F) | ||
- | FM.close() | ||
- | Coef1=file("Coef 1","w") | ||
- | p=pickle.Pickler(Coef1) | ||
- | p.dump(dist_coefs1) | ||
- | Coef1.close() | ||
- | Coef2=file("Coef 2","w") | ||
- | p=pickle.Pickler(Coef2) | ||
- | p.dump(dist_coefs2) | ||
- | Coef2.close() | ||
- | |||
- | print rms | ||
- | print "Kamera Matrix 1", camera_matrix1 | ||
- | print "Kamera Matrix 2", camera_matrix2 | ||
- | print "Rotation ", R | ||
- | print "Translation ", T | ||
- | print "E ", E | ||
- | print "F ", F | ||
- | cv2.destroyAllWindows() | ||
- | STOP_FLAG=True | ||
- | |||
- | |||
- | import cv | ||
- | import pickle | ||
- | import cv2 | ||
- | import numpy as np | ||
- | |||
- | dist_coefs1=[-0.1384, 0.3435,-0.0055, 0.0060, -0.3011] | ||
- | |||
- | dist_coefs2=[-0.1208, 0.3704, -0.0079, 0.0010, -0.3450] | ||
- | |||
- | Coef1=file("Coef1","w") | ||
- | p=pickle.Pickler(Coef1) | ||
- | p.dump(dist_coefs1) | ||
- | Coef1.close() | ||
- | |||
- | Coef2=file("Coef2","w") | ||
- | p=pickle.Pickler(Coef2) | ||
- | p.dump(dist_coefs2) | ||
- | Coef2.close() | ||
- | |||
- | K1=file("Kameramatrix1","r") | ||
- | p=pickle.Unpickler(K1) | ||
- | camera_matrix1=p.load() | ||
- | K1.close() | ||
- | |||
- | K2=file("Kameramatrix2","r") | ||
- | p=pickle.Unpickler(K2) | ||
- | camera_matrix2=p.load() | ||
- | K2.close() | ||
- | |||
- | Rot=file("Rotationsmatrix","r") | ||
- | p=pickle.Unpickler(Rot) | ||
- | R=p.load() | ||
- | Rot.close() | ||
- | |||
- | Trans=file("Translationsmatrix","r") | ||
- | p=pickle.Unpickler(Trans) | ||
- | T=p.load() | ||
- | Trans.close() | ||
- | |||
- | EM=file("E","r") | ||
- | p=pickle.Unpickler(EM) | ||
- | E=p.load() | ||
- | EM.close() | ||
- | |||
- | FM=file("F","r") | ||
- | p=pickle.Unpickler(FM) | ||
- | F=p.load() | ||
- | FM.close() | ||
- | |||
- | Coef1=file("Coef1","r") | ||
- | p=pickle.Unpickler(Coef1) | ||
- | dist_coefs1=p.load() | ||
- | Coef1.close() | ||
- | |||
- | Coef2=file("Coef2","r") | ||
- | p=pickle.Unpickler(Coef2) | ||
- | dist_coefs2=p.load() | ||
- | Coef2.close() | ||
- | |||
- | #R1=np.zeros(3,3) | ||
- | #R2=np.zeros(3,3) | ||
- | #P1=np.zeros(3,4) | ||
- | #P2=np.zeros(3,4) | ||
- | |||
- | |||
- | R1, R2, P1, P2, Q, valid1,valid2=\ | ||
- | cv2.stereoRectify(camera_matrix1, np.array(dist_coefs1), camera_matrix2, np.array(dist_coefs2), (640,480), R, T) | ||
- | print "R1" | ||
- | print R1 | ||
- | print "R2" | ||
- | print R2 | ||
- | print "P1" | ||
- | print P1 | ||
- | print "P2" | ||
- | print P2 | ||
- | |||
- |