After calibration (which has successful images ), im getting a black camera input with cv2.imshow() , how can i fix it ?
as you can imagine, that’s impossible to tell from what you said so far.
show everything you did and everything you have. you could have done that already. don’t expect further probing. spill your guts.
using code from a youtube channel , The Coding Lib
slightly changed because my camera was being weird
"import cv2
import keyboard
from time import sleep
cap = cv2.VideoCapture(2, cv2.CAP_DSHOW)
cap .set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap .set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*“MJPG”))
cap2 = cv2.VideoCapture(3, cv2.CAP_DSHOW)
cap2.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap2.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
cap2.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*“MJPG”))
num = 0
while cap.isOpened():
“”"
succes1, img = cap.read()
succes2, img2 = cap2.read()
“”"
succes1 = cap.grab()
succes1, img = cap.retrieve()
succes2 = cap2.grab()
succes2, img2 = cap2.retrieve()
k = cv2.waitKey(5)
if k == 27:
break
if keyboard.is_pressed('s'): # wait for 's' key to save and exit
cv2.imwrite('images/stereoLeft/imageL' + str(num) + '.png', img)
cv2.imwrite('images/stereoright/imageR' + str(num) + '.png', img2)
print("images saved!")
num += 1
sleep(0.5)
cv2.imshow('Img 1',img)
cv2.imshow('Img 2',img2)
Release and destroy all windows before termination
cap.release()
cap2.release()
cv2.destroyAllWindows()
"
to take pictures
"import numpy as np
import cv2 as cv
import glob
import keyboard
################ FIND CHESSBOARD CORNERS - OBJECT POINTS AND IMAGE POINTS #############################
chessboardSize = (8,6)
frameSize = (1280,720)
termination criteria
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
prepare object points, like (0,0,0), (1,0,0), (2,0,0) …,(6,5,0)
objp = np.zeros((chessboardSize[0] * chessboardSize[1], 3), np.float32)
objp[:,:2] = np.mgrid[0:chessboardSize[0],0:chessboardSize[1]].T.reshape(-1,2)
Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpointsL = [] # 2d points in image plane.
imgpointsR = [] # 2d points in image plane.
imagesLeft = glob.glob(‘images/stereoLeft/.png’)
imagesRight = glob.glob('images/stereoRight/.png’)
ct=0
for imgLeft, imgRight in zip(imagesLeft, imagesRight):
print(ct)
imgL = cv.imread(imgLeft)
imgR = cv.imread(imgRight)
grayL = cv.cvtColor(imgL, cv.COLOR_BGR2GRAY)
grayR = cv.cvtColor(imgR, cv.COLOR_BGR2GRAY)
# Find the chess board corners
retL, cornersL = cv.findChessboardCorners(grayL, chessboardSize, None)
retR, cornersR = cv.findChessboardCorners(grayR, chessboardSize, None)
# If found, add object points, image points (after refining them)
if retL and retR == True:
objpoints.append(objp)
cornersL = cv.cornerSubPix(grayL, cornersL, (11,11), (-1,-1), criteria)
imgpointsL.append(cornersL)
cornersR = cv.cornerSubPix(grayR, cornersR, (11,11), (-1,-1), criteria)
imgpointsR.append(cornersR)
# Draw and display the corners
cv.drawChessboardCorners(imgL, chessboardSize, cornersL, retL)
cv.putText(imgL, str(ct), (50,50), cv.FONT_HERSHEY_SIMPLEX, 2, (0,255,0), 2)
cv.imshow('img left', imgL)
cv.drawChessboardCorners(imgR, chessboardSize, cornersR, retR)
cv.imshow('img right', imgR)
cv.imwrite('images/stereoRes/imageL' + str(ct) + '.png', imgL)
cv.imwrite('images/stereoRes/imageR' + str(ct) + '.png', imgR)
cv.waitKey(1000)
ct+=1
#cv.destroyAllWindows()
############## CALIBRATION #######################################################
retL, cameraMatrixL, distL, rvecsL, tvecsL = cv.calibrateCamera(objpoints, imgpointsL, frameSize, None, None)
heightL, widthL, channelsL = imgL.shape
newCameraMatrixL, roi_L = cv.getOptimalNewCameraMatrix(cameraMatrixL, distL, (widthL, heightL), 1, (widthL, heightL))
retR, cameraMatrixR, distR, rvecsR, tvecsR = cv.calibrateCamera(objpoints, imgpointsR, frameSize, None, None)
heightR, widthR, channelsR = imgR.shape
newCameraMatrixR, roi_R = cv.getOptimalNewCameraMatrix(cameraMatrixR, distR, (widthR, heightR), 1, (widthR, heightR))
########## Stereo Vision Calibration #############################################
flags = 0
flags |= cv.CALIB_FIX_INTRINSIC
Here we fix the intrinsic camara matrixes so that only Rot, Trns, Emat and Fmat are calculated.
Hence intrinsic parameters are the same
criteria_stereo= (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001)
This step is performed to transformation between the two cameras and calculate Essential and Fundamenatl matrix
retStereo, newCameraMatrixL, distL, newCameraMatrixR, distR, rot, trans, essentialMatrix, fundamentalMatrix = cv.stereoCalibrate(objpoints, imgpointsL, imgpointsR, newCameraMatrixL, distL, newCameraMatrixR, distR, grayL.shape[::-1], criteria_stereo, flags)
#print(newCameraMatrixL)
#print(newCameraMatrixR)
########## Stereo Rectification #################################################
rectifyScale= 1
rectL, rectR, projMatrixL, projMatrixR, Q, roi_L, roi_R= cv.stereoRectify(newCameraMatrixL, distL, newCameraMatrixR, distR, grayL.shape[::-1], rot, trans, rectifyScale,(0,0))
stereoMapL = cv.initUndistortRectifyMap(newCameraMatrixL, distL, rectL, projMatrixL, grayL.shape[::-1], cv.CV_16SC2)
stereoMapR = cv.initUndistortRectifyMap(newCameraMatrixR, distR, rectR, projMatrixR, grayR.shape[::-1], cv.CV_16SC2)
print(“Saving parameters!”)
cv_file = cv.FileStorage(‘stereoMap.xml’, cv.FILE_STORAGE_WRITE)
cv_file.write(‘stereoMapL_x’,stereoMapL[0])
cv_file.write(‘stereoMapL_y’,stereoMapL[1])
cv_file.write(‘stereoMapR_x’,stereoMapR[0])
cv_file.write(‘stereoMapR_y’,stereoMapR[1])
cv_file.release()
"
to calibrate
"import sys
import cv2
import numpy as np
import time
import imutils
from matplotlib import pyplot as plt
Function for stereo vision and depth estimation
import triangulation as tri
import calibration
Mediapipe for face detection
import mediapipe as mp
import time
mp_facedetector = mp.solutions.face_detection
mp_draw = mp.solutions.drawing_utils
Open both cameras
cap_right = cv2.VideoCapture(3, cv2.CAP_DSHOW)
cap_right .set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap_right .set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
cap_right.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*“MJPG”))
cap_left = cv2.VideoCapture(2, cv2.CAP_DSHOW)
cap_left .set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap_left .set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
cap_left.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*“MJPG”))
cv_file = cv2.FileStorage()
cv_file.open(‘stereoMap.xml’, cv2.FileStorage_READ)
stereoMapL_x = cv_file.getNode(‘stereoMapL_x’).mat()
stereoMapL_y = cv_file.getNode(‘stereoMapL_y’).mat()
stereoMapR_x = cv_file.getNode(‘stereoMapR_x’).mat()
stereoMapR_y = cv_file.getNode(‘stereoMapR_y’).mat()
Stereo vision setup parameters
frame_rate = 30 #Camera frame rate (maximum at 120 fps)
B = 7.3 #Distance between the cameras [cm]
f = 8 #Camera lense’s focal length [mm]
alpha = 90 #Camera field of view in the horisontal plane [degrees]
avg=[]
smallest=999
biggest=0
Main program loop with face detector and depth estimation using stereo vision
with mp_facedetector.FaceDetection(min_detection_confidence=0.7) as face_detection:
while(cap_right.isOpened() and cap_left.isOpened()):
succes_right, frame_right = cap_right.read()
succes_left, frame_left = cap_left.read()
################## CALIBRATION #########################################################
frame_right = cv2.remap(frame_right, stereoMapR_x, stereoMapR_y, cv2.INTER_LANCZOS4, cv2.BORDER_CONSTANT, 0)
frame_left = cv2.remap(frame_left, stereoMapL_x, stereoMapL_y, cv2.INTER_LANCZOS4, cv2.BORDER_CONSTANT, 0)
########################################################################################
# If cannot catch any frame, break
if not succes_right or not succes_left:
break
else:
start = time.time()
# Convert the BGR image to RGB
frame_right = cv2.cvtColor(frame_right, cv2.COLOR_BGR2RGB)
frame_left = cv2.cvtColor(frame_left, cv2.COLOR_BGR2RGB)
# Process the image and find faces
results_right = face_detection.process(frame_right)
results_left = face_detection.process(frame_left)
# Convert the RGB image to BGR
frame_right = cv2.cvtColor(frame_right, cv2.COLOR_RGB2BGR)
frame_left = cv2.cvtColor(frame_left, cv2.COLOR_RGB2BGR)
################## CALCULATING DEPTH #########################################################
center_right = 0
center_left = 0
#detection.relative_keypoints[] 0 and 1 are eyes
#detection.relative_bounding_box
if results_right.detections:
for id, detection in enumerate(results_right.detections):
mp_draw.draw_detection(frame_right, detection)
bBox = detection.location_data.relative_bounding_box
h, w, c = frame_right.shape
boundBox = int(bBox.xmin * w), int(bBox.ymin * h), int(bBox.width * w), int(bBox.height * h)
#center_point_right = (boundBox[0] + boundBox[2] / 2, boundBox[1] + boundBox[3] / 2)
fy=len(frame_right)
fx=len(frame_right[0])
posx=(int)(detection.location_data.relative_keypoints[0].x*fx)
posy=(int)(detection.location_data.relative_keypoints[0].y*fy)
cv2.putText(frame_right, "1?", (posx,posy), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,255,0), 2)
posx1=(int)(detection.location_data.relative_keypoints[1].x*fx)
posy1=(int)(detection.location_data.relative_keypoints[1].y*fy)
p2distance= ((((posx - posx1 )**2) + ((posy-posy1)**2) )**0.5)
#print("DISTANCE",p2distance)
cv2.putText(frame_right, "2?", (posx1,posy1), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,255,0), 2)
center_point_right=(((posx+posx1)/2),((posy+posy1)/2))
cv2.putText(frame_right, f'{int(detection.score[0]*100)}%', (boundBox[0], boundBox[1] - 20), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,255,0), 2)
if results_left.detections:
for id, detection in enumerate(results_left.detections):
mp_draw.draw_detection(frame_left, detection)
bBox = detection.location_data.relative_bounding_box
h, w, c = frame_left.shape
boundBox = int(bBox.xmin * w), int(bBox.ymin * h), int(bBox.width * w), int(bBox.height * h)
#center_point_left = (boundBox[0] + boundBox[2] / 2, boundBox[1] + boundBox[3] / 2)
fy=len(frame_right)
fx=len(frame_right[0])
posx=(int)(detection.location_data.relative_keypoints[0].x*fx)
posy=(int)(detection.location_data.relative_keypoints[0].y*fy)
posx1=(int)(detection.location_data.relative_keypoints[1].x*fx)
posy1=(int)(detection.location_data.relative_keypoints[1].y*fy)
center_point_left=(((posx+posx1)/2),((posy+posy1)/2))
cv2.putText(frame_left, f'{int(detection.score[0]*100)}%', (boundBox[0], boundBox[1] - 20), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,255,0), 2)
# If no ball can be caught in one camera show text "TRACKING LOST"
if not results_right.detections or not results_left.detections:
cv2.putText(frame_right, "TRACKING LOST", (75,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255),2)
cv2.putText(frame_left, "TRACKING LOST", (75,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255),2)
else:
# Function to calculate depth of object. Outputs vector of all depths in case of several balls.
# All formulas used to find depth is in video presentaion
depth = tri.find_depth(center_point_right, center_point_left, frame_right, frame_left, B, f, alpha)
depth=depth*-1
cv2.putText(frame_right, "Distance: " + str(round(depth,1)), (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0,255,0),3)
cv2.putText(frame_left, "Distance: " + str(round(depth,1)), (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0,255,0),3)
# Multiply computer value with 205.8 to get real-life depth in [cm]. The factor was found manually.
#print("Depth: ", str(round(depth,1)))
p2distancecalc=p2distance*depth*(9.5/2600)
#print("DISTANCE",p2distancecalc)
if depth>biggest:
biggest=depth
if depth<smallest:
smallest=depth
print("SMALL:",int(smallest),"BIG:",int(biggest))
cv2.putText(frame_right, "Distance: " + str(p2distancecalc), (50,150), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0,255,0),3)
avg.append(p2distancecalc)
end = time.time()
totalTime = end - start
fps = 1 / totalTime
#print("FPS: ", fps)
cv2.putText(frame_right, f'FPS: {int(fps)}', (20,450), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0,255,0), 2)
cv2.putText(frame_left, f'FPS: {int(fps)}', (20,450), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0,255,0), 2)
# Show the frames
cv2.imshow("frame right", frame_right)
cv2.imshow("frame left", frame_left)
#if len(avg)>=100:
# break;
# Hit "q" to close the window
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print(“AVERAGE=”,sum(avg) / len(avg))
Release and destroy all windows before termination
cap_right.release()
cap_left.release()
cv2.destroyAllWindows()
"
to use the camera ,
images were successfully detected aswell
@yagizzha Im using the same code from THE CODING LIB, mi cameras detect the chessboard perfectly, but after remap the images are all black. were you able to solve it??
my cameras have a distortion of 2.8mm, and although it seems irrelevant, I used 2 logitech c922 webcams that have a 1mm focal length and it has improved a lot, although it does not continue to work as it should.