1 year ago

#389128

test-img

rotimi abayomi

OpenCV - cv.remap() - getting black pixels

import numpy as np
import cv2 as cv
from os import listdir, mkdir
from os.path import join, isdir
import matplotlib.pyplot as plt

### Paths
ROOT = r'C:\Users\rotim\Documents\STEREO_VISION'
output_id = '7.jpg'


### Termination Critera, Modes
criteria_calib = (cv.TERM_CRITERIA_MAX_ITER + cv.TERM_CRITERIA_EPS, 1000, 1e-6)
params_ransac = (cv.FM_RANSAC, 2.5, 0.9)
flags_thresh = (cv.CALIB_CB_ADAPTIVE_THRESH + cv.CALIB_CB_NORMALIZE_IMAGE)
flags_indiv_calib = (0)
flags_stereo_calib = (cv.CALIB_FIX_INTRINSIC)


def sort_id(e):
    return int(e.split('.')[0])


def rescaleROI(src, roi):
    x, y, w, h = roi
    dst = src[y:y+h, x:x+w]
    return dst


file_list = [i for i in listdir(join(ROOT, 'L'))]
file_list.sort(key=sort_id)

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

# Store computed points
objPts = []
imgPL, imgPR = [], []

imgSize = (640, 480) # (768, 1024), 1296x972


for f in file_list:
    fname = str(f)

    imgL = cv.imread(join(ROOT, 'L', fname))
    imgR = cv.imread(join(ROOT, 'R', fname))
    grayL = cv.cvtColor(imgL, cv.COLOR_RGB2GRAY)
    grayR = cv.cvtColor(imgR, cv.COLOR_RGB2GRAY)
    # h,w = grayL.shape
    retL, cornersL = cv.findChessboardCorners(grayL, (9,6), flags=flags_thresh)
    retR, cornersR = cv.findChessboardCorners(grayR, (9,6), flags=flags_thresh)

    if retL and retR:
        objPts.append(objp)
        corners2L = cv.cornerSubPix(grayL, cornersL, (5,5), (-1,-1), criteria_calib)
        imgPL.append(corners2L)
        # objPR.append(objp)
        corners2R = cv.cornerSubPix(grayR, cornersR, (5,5), (-1,-1), criteria_calib)
        imgPR.append(corners2R)
    else:
        print(f'Corners not found in {fname} (Left={retL}, Right={retR})')
        break

objPts = np.asarray(objPts, np.float32)
imgPL = np.asarray(imgPL, np.float32)
imgPR = np.asarray(imgPR, np.float32)



mse1, C1, D1, R1, T1 = cv.calibrateCamera(objPts, imgPL, imgSize, 
                        None, None, flags=flags_indiv_calib, criteria=criteria_calib)
mse2, C2, D2, R2, T2 = cv.calibrateCamera(objPts, imgPR, imgSize, 
                        None, None, flags=flags_indiv_calib, criteria=criteria_calib)

mseTotal,CL,DL,CR,DR,R,T,E,F = cv.stereoCalibrate(objPts, imgPL, imgPR, 
                        C1, D1, C2, D2, imgSize, flags=flags_stereo_calib, criteria=criteria_calib)

RL,RR,PL,PR,Q,validROIL,validROIR = cv.stereoRectify(CL, DL, CR, DR, imgSize, R, T, alpha=0, 
                                                     newImageSize=imgSize, flags=cv.CALIB_ZERO_DISPARITY)


''' Print critical parameters and per-view reprojection error '''
np.set_printoptions(suppress=True, precision=3)

print('CameraMatrix_L = \n{}\n\nCameraMatrix_R = \n{}\n'.format(CL, CR))
print('RotationStereo = \n{}\n\nTranslationStereo = \n{}\n'.format(R, T))
print('DistCoeffStereo_L = \n{}\n\nDistCoeffStereo_R = \n{}\n'.format(DL, DR))
print('Q = \n{}\n'.format(Q))

print(f'Left MSE: {mse1:0.6f}')
print(f'Right MSE: {mse1:0.6f}')
print(f'Overall MSE: {mseTotal:0.6f} ({len(file_list)} image pairs)\n')

labels = []

''' Rectification mapping '''
undistL, rectifL = cv.initUndistortRectifyMap(CL, DL, RL, PL, imgSize, cv.CV_32FC1)
undistR, rectifR = cv.initUndistortRectifyMap(CR, DR, RR, PR, imgSize, cv.CV_32FC1)


''' Preview rectification & remap '''
img1 = cv.imread(join(ROOT, 'L', output_id))
img2 = cv.imread(join(ROOT, 'R', output_id))
img1 = cv.cvtColor(img1, cv.COLOR_BGR2RGB)
img2 = cv.cvtColor(img2, cv.COLOR_BGR2RGB)

img1 = cv.remap(img1, undistL, rectifL, cv.INTER_LINEAR, cv.BORDER_CONSTANT)
img2 = cv.remap(img2, undistR, rectifR, cv.INTER_LINEAR, cv.BORDER_CONSTANT)


plt.figure(figsize=(9,6))
plt.subplot(221); plt.imshow(img1); plt.title('remap_L: ' + str(img1.shape))
plt.subplot(222); plt.imshow(img2); plt.title('remap_R: ' + str(img2.shape))

img11 = rescaleROI(img1, validROIL)
img22 = rescaleROI(img2, validROIR)

# dsize = (img11.shape[1], img11.shape[0])
# img22 = cv.resize(img22, dsize, interpolation=cv.INTER_LINEAR)

### Compare rectified and remapped images
plt.subplot(223); plt.imshow(img11); plt.title('ROI_L: ' + str(img11.shape))
plt.subplot(224); plt.imshow(img22); plt.title('ROI_R: ' + str(img22.shape))
plt.tight_layout()
plt.show()


''' Write parameters to .txt files '''
output_dir = r'Calibration_Files_expm'
prompt = input('Save parameters to "{}\\"? (y/n): '.format(output_dir))

if (prompt == 'y'):
    if not isdir(output_dir):
        mkdir(output_dir)
    # np.savetxt(r'Calibration_Files\C1.txt', C1, fmt='%.5e')   # identical to CL
    # np.savetxt(r'Calibration_Files\D1.txt', D1, fmt='%.5e')   # identical to DL
    np.savetxt(join(output_dir, 'Q.txt'), Q, fmt='%.5e')
    # np.savetxt(join(output_dir, 'FundMat.txt'), F, fmt='%.5e')
    np.savetxt(join(output_dir, 'CmL.txt'), CL, fmt='%.5e')
    np.savetxt(join(output_dir, 'CmR.txt'), CR, fmt='%.5e')
    np.savetxt(join(output_dir, 'DcL.txt'), DL, fmt='%.5e')
    np.savetxt(join(output_dir, 'DcR.txt'), DR, fmt='%.5e')
    np.savetxt(join(output_dir, 'Rtn.txt'), R, fmt='%.5e')
    np.savetxt(join(output_dir, 'Trnsl.txt'), T, fmt='%.5e')
    # np.savetxt(join(output_dir, 'RtnL.txt'), R1, fmt='%.5e')  # Contains 'n' estimate arrays from 'n' images
    # np.savetxt(join(output_dir, 'TrnslL.txt'), T1, fmt='%.5e')
    np.savetxt(join(output_dir, 'RectifL.txt'), RL, fmt='%.5e')
    np.savetxt(join(output_dir, 'ProjL.txt'), PL, fmt='%.5e')
    np.savetxt(join(output_dir, 'ProjR.txt'), PR, fmt='%.5e')
    np.savetxt(join(output_dir, 'umapL.txt'), undistL, fmt='%.5e')
    np.savetxt(join(output_dir, 'rmapL.txt'), rectifL, fmt='%.5e')
    np.savetxt(join(output_dir, 'umapR.txt'), undistR, fmt='%.5e')
    np.savetxt(join(output_dir, 'rmapR.txt'), rectifR, fmt='%.5e')
    np.savetxt(join(output_dir, 'ROIL.txt'), validROIL, fmt='%.5e')
    np.savetxt(join(output_dir, 'ROIR.txt'), validROIR, fmt='%.5e')
    print(f'Parameters saved to folder: [{output_dir}]')

I'm working on stereo vision(Computer vision). I want to calibrate my camera's but the cv.remap is giving a black image.remap plot.

This is affecting img11 and img22 by not giving it any values to work with.

This is the main error below i tried fixing that lead me to find this problem

ValueError: zero-size array to reduction operation minimum which has no identity

OUTPUT AND ERROR SHOWN

CameraMatrix_L = 
[[2756.143    0.     521.702]
 [   0.    2812.585  327.337]
 [   0.       0.       1.   ]]

CameraMatrix_R = 
[[1670.954    0.    1205.327]
 [   0.    2524.826  254.341]
 [   0.       0.       1.   ]]

RotationStereo = 
[[ 0.975  0.044 -0.217]
 [-0.031  0.998  0.062]
 [ 0.219 -0.054  0.974]]

TranslationStereo = 
[[-11.733]
 [ -2.918]
 [ 12.289]]

DistCoeffStereo_L = 
[[  -1.132 -135.598   -0.015   -0.123 6468.129]]

DistCoeffStereo_R = 
[[-3.015  2.26  -0.001 -0.877 -7.401]]

Q = 
[[    1.        0.        0.    -7785.808]
 [    0.        1.        0.      842.122]
 [    0.        0.        0.    16191.828]
 [    0.        0.        0.058    -0.   ]]

Left MSE: 0.185031
Right MSE: 0.185031
Overall MSE: 0.242667 (8 image pairs)

Traceback (most recent call last):

  File "C:\Users\rotim\AppData\Local\Programs\Python\Python37\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
    exec(code, globals, locals)

  File "c:\users\rotim\desktop\stereo path planning\stereo-camera-path-planning\calibration.py", line 124, in <module>
    plt.subplot(223); plt.imshow(img11); plt.title('ROI_L: ' + str(img11.shape))

  File "C:\Users\rotim\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\_api\deprecation.py", line 456, in wrapper
    return func(*args, **kwargs)

  File "C:\Users\rotim\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\pyplot.py", line 2647, in imshow
    **kwargs)

  File "C:\Users\rotim\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\_api\deprecation.py", line 456, in wrapper
    return func(*args, **kwargs)

  File "C:\Users\rotim\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\__init__.py", line 1412, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)

  File "C:\Users\rotim\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes\_axes.py", line 5488, in imshow
    im.set_data(X)

  File "C:\Users\rotim\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\image.py", line 724, in set_data
    if self._A.min() < 0 or high < self._A.max():

  File "C:\Users\rotim\AppData\Roaming\Python\Python37\site-packages\numpy\ma\core.py", line 5701, in min
    axis=axis, out=out, **kwargs).view(type(self))

  File "C:\Users\rotim\AppData\Roaming\Python\Python37\site-packages\numpy\core\_methods.py", line 44, in _amin
    return umr_minimum(a, axis, None, out, keepdims, initial, where)

ValueError: zero-size array to reduction operation minimum which has no identity

The sample images used Left Sample image Left Camera image rotated 180 degrees

Right Sample image Right Camera image rotated 180 degrees

Please any help will be appreciated. Thank you.

python

opencv

0 Answers

Your Answer

Accepted video resources