Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It is a first attempt to use the stereoscopic vision, there is two co… #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added muDIC/stereo/.DS_Store
Binary file not shown.
Binary file added muDIC/stereo/Calibration_Images/.DS_Store
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added muDIC/stereo/Cropted_images/.DS_Store
Binary file not shown.
Binary file added muDIC/stereo/Cropted_images/Image10L.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added muDIC/stereo/Cropted_images/Image10R.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added muDIC/stereo/Cropted_images/Image11L.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added muDIC/stereo/Cropted_images/Image11R.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added muDIC/stereo/Cropted_images/Image12L.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added muDIC/stereo/Cropted_images/Image12R.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added muDIC/stereo/Cropted_images/Image13L.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added muDIC/stereo/Cropted_images/Image13R.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added muDIC/stereo/Cropted_images/Image14L.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added muDIC/stereo/Cropted_images/Image14R.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added muDIC/stereo/Cropted_images/Image15L.png
Binary file added muDIC/stereo/Cropted_images/Image15R.png
Binary file added muDIC/stereo/Cropted_images/Image16L.png
Binary file added muDIC/stereo/Cropted_images/Image16R.png
Binary file added muDIC/stereo/Cropted_images/Image17L.png
Binary file added muDIC/stereo/Cropted_images/Image17R.png
Binary file added muDIC/stereo/Cropted_images/Image1L.png
Binary file added muDIC/stereo/Cropted_images/Image1R.png
Binary file added muDIC/stereo/Cropted_images/Image2L.png
Binary file added muDIC/stereo/Cropted_images/Image2R.png
Binary file added muDIC/stereo/Cropted_images/Image3L.png
Binary file added muDIC/stereo/Cropted_images/Image3R.png
Binary file added muDIC/stereo/Cropted_images/Image4L.png
Binary file added muDIC/stereo/Cropted_images/Image4R.png
Binary file added muDIC/stereo/Cropted_images/Image5L.png
Binary file added muDIC/stereo/Cropted_images/Image5R.png
Binary file added muDIC/stereo/Cropted_images/Image6L.png
Binary file added muDIC/stereo/Cropted_images/Image6R.png
Binary file added muDIC/stereo/Cropted_images/Image7L.png
Binary file added muDIC/stereo/Cropted_images/Image7R.png
Binary file added muDIC/stereo/Cropted_images/Image8L.png
Binary file added muDIC/stereo/Cropted_images/Image8R.png
Binary file added muDIC/stereo/Cropted_images/Image9L.png
Binary file added muDIC/stereo/Cropted_images/Image9R.png
23 changes: 23 additions & 0 deletions muDIC/stereo/Deapth_map_without_calib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# import OpenCV and pyplot
from matplotlib import pyplot as plt
import cv2
import glob
from PIL import Image

Images_R = glob.glob(r'./Image_test_R/*')
Images_L = glob.glob(r'./Image_test_L/*')

for i in range (len(Images_R)):
# read left and right images

imgR = cv2.imread(Images_R[i], cv2.IMREAD_GRAYSCALE)
imgL = cv2.imread(Images_L[i], cv2.IMREAD_GRAYSCALE)
# creates StereoBm object
stereo = cv2.StereoBM_create(numDisparities = 16*13, blockSize = 9)

# computes disparity
disparity = stereo.compute(imgL, imgR)

# displays image as grayscale and plotted
plt.imshow(disparity, vmin=0, vmax=1200, cmap='viridis')
plt.show()
Binary file added muDIC/stereo/Image_calib_L/.DS_Store
Binary file not shown.
Binary file added muDIC/stereo/Image_calib_L/frame10_1.png
Binary file added muDIC/stereo/Image_calib_L/frame11_1.png
Binary file added muDIC/stereo/Image_calib_L/frame12_1.png
Binary file added muDIC/stereo/Image_calib_L/frame13_1.png
Binary file added muDIC/stereo/Image_calib_L/frame14_1.png
Binary file added muDIC/stereo/Image_calib_L/frame15_1.png
Binary file added muDIC/stereo/Image_calib_L/frame16_1.png
Binary file added muDIC/stereo/Image_calib_L/frame17_1.png
Binary file added muDIC/stereo/Image_calib_L/frame18_1.png
Binary file added muDIC/stereo/Image_calib_L/frame1_1.png
Binary file added muDIC/stereo/Image_calib_L/frame2_1.png
Binary file added muDIC/stereo/Image_calib_L/frame3_1.png
Binary file added muDIC/stereo/Image_calib_L/frame4_1.png
Binary file added muDIC/stereo/Image_calib_L/frame5_1.png
Binary file added muDIC/stereo/Image_calib_L/frame6_1.png
Binary file added muDIC/stereo/Image_calib_L/frame7_1.png
Binary file added muDIC/stereo/Image_calib_L/frame8_1.png
Binary file added muDIC/stereo/Image_calib_L/frame9_1.png
Binary file added muDIC/stereo/Image_calib_R/.DS_Store
Binary file not shown.
Binary file added muDIC/stereo/Image_calib_R/frame10_0.png
Binary file added muDIC/stereo/Image_calib_R/frame11_0.png
Binary file added muDIC/stereo/Image_calib_R/frame12_0.png
Binary file added muDIC/stereo/Image_calib_R/frame13_0.png
Binary file added muDIC/stereo/Image_calib_R/frame14_0.png
Binary file added muDIC/stereo/Image_calib_R/frame15_0.png
Binary file added muDIC/stereo/Image_calib_R/frame16_0.png
Binary file added muDIC/stereo/Image_calib_R/frame17_0.png
Binary file added muDIC/stereo/Image_calib_R/frame18_0.png
Binary file added muDIC/stereo/Image_calib_R/frame1_0.png
Binary file added muDIC/stereo/Image_calib_R/frame2_0.png
Binary file added muDIC/stereo/Image_calib_R/frame3_0.png
Binary file added muDIC/stereo/Image_calib_R/frame4_0.png
Binary file added muDIC/stereo/Image_calib_R/frame5_0.png
Binary file added muDIC/stereo/Image_calib_R/frame6_0.png
Binary file added muDIC/stereo/Image_calib_R/frame7_0.png
Binary file added muDIC/stereo/Image_calib_R/frame8_0.png
Binary file added muDIC/stereo/Image_calib_R/frame9_0.png
Binary file added muDIC/stereo/Image_test_L/.DS_Store
Binary file not shown.
Binary file added muDIC/stereo/Image_test_L/frame10_1.png
Binary file added muDIC/stereo/Image_test_L/frame14_1.png
Binary file added muDIC/stereo/Image_test_L/frame17_1.png
Binary file added muDIC/stereo/Image_test_L/frame19_1.png
Binary file added muDIC/stereo/Image_test_L/frame20_1.png
Binary file added muDIC/stereo/Image_test_L/frame21_1.png
Binary file added muDIC/stereo/Image_test_L/frame22_1.png
Binary file added muDIC/stereo/Image_test_L/frame23_1.png
Binary file added muDIC/stereo/Image_test_L/frame24_1.png
Binary file added muDIC/stereo/Image_test_L/frame25_1.png
Binary file added muDIC/stereo/Image_test_L/frame27_1.png
Binary file added muDIC/stereo/Image_test_L/frame29_1.png
Binary file added muDIC/stereo/Image_test_L/frame31_1.png
Binary file added muDIC/stereo/Image_test_L/frame5_1.png
Binary file added muDIC/stereo/Image_test_L/frame6_1.png
Binary file added muDIC/stereo/Image_test_L/frame7_1.png
Binary file added muDIC/stereo/Image_test_L/frame8_1.png
Binary file added muDIC/stereo/Image_test_R/.DS_Store
Binary file not shown.
Binary file added muDIC/stereo/Image_test_R/frame10_0.png
Binary file added muDIC/stereo/Image_test_R/frame14_0.png
Binary file added muDIC/stereo/Image_test_R/frame17_0.png
Binary file added muDIC/stereo/Image_test_R/frame19_0.png
Binary file added muDIC/stereo/Image_test_R/frame20_0.png
Binary file added muDIC/stereo/Image_test_R/frame21_0.png
Binary file added muDIC/stereo/Image_test_R/frame22_0.png
Binary file added muDIC/stereo/Image_test_R/frame23_0.png
Binary file added muDIC/stereo/Image_test_R/frame24_0.png
Binary file added muDIC/stereo/Image_test_R/frame25_0.png
Binary file added muDIC/stereo/Image_test_R/frame27_0.png
Binary file added muDIC/stereo/Image_test_R/frame29_0.png
Binary file added muDIC/stereo/Image_test_R/frame31_0.png
Binary file added muDIC/stereo/Image_test_R/frame5_0.png
Binary file added muDIC/stereo/Image_test_R/frame6_0.png
Binary file added muDIC/stereo/Image_test_R/frame7_0.png
Binary file added muDIC/stereo/Image_test_R/frame8_0.png
106 changes: 106 additions & 0 deletions muDIC/stereo/calibration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import numpy as np
import cv2
import glob


class Calibration(object):
def __init__(self):
pass

def cameras_calib(path, points_in_width=6, points_in_height=9):
"""
Aims to calibrate the cameras so that we can remove distortion afterwards
---------
path:
The folder in which we can find the differnets pictures of the chessboard.
It is important that the photos are taken from the same camera and from diferents angles.
width:
Number of points required in width for calibration
height:
Number of points required in height for calibration
"""
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)

# prepare object points
objp = np.zeros((points_in_width*points_in_height,3), np.float32)
objp[:,:2] = np.mgrid[0:points_in_height,0:points_in_width].T.reshape(-1,2)

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.

for fname in path:
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (points_in_height,points_in_width),None)

# If found, add object points, image points (after refining them)
if ret == True:
objpoints.append(objp)

corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
imgpoints.append(corners2)

# Draw and display the corners
img = cv2.drawChessboardCorners(img, (points_in_height,points_in_width), corners2,ret)
#If you want to see the images of the calibration (the chessboard) uncomment the 3 lines below
#cv2.imshow('image',img)
#cv2.waitKey(500) #It's the display time

#cv2.destroyAllWindows()

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
return([ret, mtx, dist, rvecs, tvecs])

def crop(imageStack,calib_path, side='R'):
"""
Allow the reshape the picture and to avoid curves on the sides of images
--------
cost: 'R' or 'L'
The cost which the picture was taken
"""
nb_frame=1
ret=Calibration.cameras_calib(calib_path,6,9)[0]
mtx=Calibration.cameras_calib(calib_path,6,9)[1]
dist=Calibration.cameras_calib(calib_path,6,9)[2]
rvecs=Calibration.cameras_calib(calib_path,6,9)[3]
tvecs=Calibration.cameras_calib(calib_path,6,9)[4]
for frame in imageStack:
img = cv2.imread(frame)
h, w = img.shape[:2]
newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))

# undistort
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)

# crop the image
x,y,w,h = roi
dst = dst[y:y+h, x:x+w]
cv2.imwrite(r'./Cropted_images/Image'+str(nb_frame)+side+'.png',dst)
nb_frame+=1

class Distortion(object):
def __init__(self):
pass

def comparison(imageStack):
from matplotlib import pyplot as plt

# read left and right images
for i in range (1,int(len(imageStack)/2)):
imgR = cv2.imread(r'./Cropted_images/Image'+str(i)+'R.png', cv2.IMREAD_GRAYSCALE)
imgL = cv2.imread(r'./Cropted_images/Image'+str(i)+'L.png', cv2.IMREAD_GRAYSCALE)
# creates StereoBm object, it is possible to play with the value of numDisparities but it must remain a multiple of 16
stereo = cv2.StereoBM_create(numDisparities = 16*2, blockSize = 15)

# computes disparity
disparity = stereo.compute(imgL, imgR)

# displays image as grayscale and plotted
plt.imshow(disparity, 'gray')
plt.show()


30 changes: 30 additions & 0 deletions muDIC/stereo/quick_start_stereo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This allows for running the example when the repo has been cloned
import sys
from os.path import abspath
sys.path.extend([abspath(".")])
import muDIC as dic
import logging
import matplotlib.pyplot as plt
from muDIC.stereo.calibration import Calibration, Distortion
import glob

# Set the amount of info printed to terminal during analysis
logging.basicConfig(format='%(name)s:%(levelname)s:%(message)s', level=logging.INFO)

path_l = glob.glob(r'./Image_test_L/*')
path_r = glob.glob(r'./Image_test_R/*')

calib_path_l = glob.glob(r'./Image_calib_L/*')
calib_path_r = glob.glob(r'./Image_calib_R/*')
Calibration.crop(imageStack=path_l, calib_path=calib_path_l, side='L')
# We assume that the calibration required here is the same for the 2 cameras (if they are the same), so calib_path=calib_path_l again
Calibration.crop(imageStack=path_r, calib_path=calib_path_l, side='R')

# We are going to work with the path which containing all the cropted images
cropted_path=glob.glob(r'./Cropted_images/*')
Distortion.comparison(imageStack=cropted_path)

# Allow the user to recover the cropted images of each cameras of both sides
path_l_cropted=glob.glob(r"./Cropted_images/*L.tif")
path_r_cropted=glob.glob(r"./Cropted_images/*R.tif")