cv2.stereoCalibrate criteria error - opencv

I'm trying to perform a stereo calibration of two cameras using opencv3.4.
I have performed the calibration of the two single cameras from which I obtained the mtxleft and right to use in stereocalibrate.
But I get the following error:
in <module>
criteria = termination_criteria_extrinsics
error: C:\projects\opencv-python\opencv\modules\calib3d\src\calibration.cpp:3083:
error: (-215) nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total())
in function cv::collectCalibrationData
This is my code:
termination_criteria_extrinsics = (cv2.TERM_CRITERIA_EPS +
cv2.TERM_CRITERIA_MAX_ITER, 100, 1e-5)
ret, mtx, dist, mtx2, dist2, R, T, E, F = cv2.stereoCalibrate(
objpoints_l,
imgpoints_l ,imgpoints_r, mtxL, distL, mtxR, distR,
gray_l.shape[::-1],
flags = flags,
criteria = termination_criteria_extrinsics
)
Does anyone know what the cause could be?

Related

Error (-215) while using CLAHE Function for processing images

def AHE(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
eq = clahe.apply(gray)
return eq
IMG_SIZE = (120,120)
batch_size = 8
epoch = 10
train_image_generator = ImageDataGenerator(rescale=1./119,rotation_range=30, horizontal_flip=0.5, preprocessing_function=AHE)
validation_image_generator = ImageDataGenerator(rescale=1./119)
test_image_generator = ImageDataGenerator(rescale=1./119,preprocessing_function=AHE)
train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
directory=train_dir,
shuffle=True,
target_size=IMG_SIZE,
)
val_data_gen = validation_image_generator.flow_from_directory(batch_size=batch_size,
directory=validate_dir,
shuffle=True,
target_size=IMG_SIZE,
)
test_data_gen = test_image_generator.flow_from_directory(batch_size=batch_size,
directory=test_dir,
shuffle=True,
target_size=IMG_SIZE,
)
sample_test_images, labels = next(test_data_gen)
print(labels[0:10])
sample_test_images.shape
labels.shape
Even Though I converted the image to gray scale I'm getting this ERROR:
OpenCV(4.1.2) /io/opencv/modules/imgproc/src/clahe.cpp:351: error: (-215:Assertion failed) _src.type() == CV_8UC1 || _src.type() == CV_16UC1 in function 'apply'
I ran into just the same problem, exactly. What is asserted is significant here:
(-215:Assertion failed) _src.type() == CV_8UC1 || _src.type() == CV_16UC1
This says that the apply function is expecting to receive specific types, either "CV_8UCI" or "CV_16UCI." These correspond to np.unit8 or np.uint16, respectively, so changing the type of the input array resolves the error.
Another issue that then came up was that the shape of the image array was no longer of the same shape as the input to the preprocessing_function. According to the Keras documentation, "The function should take one argument: one image (Numpy tensor with rank 3), and should output a Numpy tensor with the same shape." To resolve this issue, I reconverted the image back to RGB color (3 channels). (BGR is native to OpenCV, but I changed to convert to and from RGB.) I also converted the array back to np.float32 type.
So your function may be along these lines:
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
def AHE(img):
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray = gray.astype(np.uint16)
eq = clahe.apply(gray)
eq = cv2.cvtColor(eq, cv2.COLOR_GRAY2RBG)
eq = eq.astype(np.float32)
return eq
BTW, with this added preprocessing, the time for each epoch has about tripled (GPU on a Colab notebook). Working with classifying chest x-rays at the moment, I hope the enhancement is worth the overhead. ;)

imshow window is not responding after I put my codes - for the connection of python and arduino- inside its loop, why is that?

This project is face-recognition with barcode. It needs to detect the face first before it can scan the barcodes. The flow is fine not unless after it detect someone face the window in imshow is not responding anymore, the webcam got froze. I want the webcam to continue moving while processing my codes, how can I do that?
cap = cv2.VideoCapture(0)
if not cap.isOpened():
raise IOError("Cannot open webcam") \
temp = ""
while True:
success, eImgs = cap.read()
if success:
font = cv2.FONT_HERSHEY_PLAIN
datet = str(datetime.now())
frame = cv2.putText(eImgs, datet, (10, 50), font, 1, (0, 0, 128), 1, cv2.LINE_AA)
# eImgs_v1 = cv2.resize (eImgs, (0, 0), None, 0.25, 0,25)
eImgs_v1 = cv2.cvtColor(eImgs, cv2.COLOR_BGR2RGB)
facesWebcam = face_recognition.face_locations(eImgs_v1)
encodesWebcam = face_recognition.face_encodings(eImgs_v1, facesWebcam)
for encodeKnown_v2, faceLoc in zip(encodesWebcam, facesWebcam):
facesCompared = face_recognition.compare_faces(encodeKnown, encodeKnown_v2)
faceDistance = face_recognition.face_distance(encodeKnown, encodeKnown_v2)
faceIndex = np.argmin(faceDistance)
if facesCompared[faceIndex]:
employeeName = ListNames[faceIndex]
y = employeeName
if temp == "" or temp != name:
print(name)
temp = name
if y:
print(y)
print("AUTHORIZED")
time.sleep(1)
**# Arduino and Python connection**
*arduino = serial.Serial('COM9', 115200, timeout=.1)
time.sleep(1)
print("The system is ready!")
while True:
barcode = arduino.readline()[:-2]
strbarcode = barcode.decode('utf-8')
if strbarcode:
x = strbarcode
print(x)
if y == x:
print('Have a nice day!')
time.sleep(3)
print("Next Employee please!")
else:
print('This is not yours!')*
else:
print("UNAUTHORIZED")
p1, p2, p3, p4 = faceLoc
cv2.rectangle(eImgs, (p1, p2), (p3, p4), (0, 255, 0), 2)
cv2.putText(eImgs, y, (p1, p3), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0), 2)
cv2.imshow('EMPLOYEE', eImgs)
cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()
I recommend you to use deepface. Its stream function applies face recognition with several state-of-the-art face recognition models.
models = ['VGG-Face', 'Facenet', 'OpenFace', 'DeepFace', 'DeepID', 'Dlib', 'ArcFace']
#!pip install deepface
from deepface import DeepFace
DeepFace.stream(db_path = 'C:/my_db'
, model_name = models[0]
, enable_face_analysis = False #to disable age, gender, emotion prediction
)

error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize' OpenCV

I have this old code that is used to run fine in Python 2.7 a while ago. I just updated the code to run in Python 3.8, but when I try to execute it code in Python 3.8 and OpenCV 3.4 I get a resize error and a warning (below)!
Here is the link to the two tif images that are required to run this code.
It's worth noting that both tif images are in the same folder as the Python code
import cv2
import matplotlib.pyplot as plt
import numpy as np
## Code for C_preferred Mask and C_images##
## There are three outputs to this code:
#"Block_order_C.PNG"
#"Out_img.PNG"
#"Output_C.txt"
## Change the image name here
filename_image = '2.tif'
filename_mask = '1.tif'
## OpenCV verison Checking
#print 'OpenCV version used', cv2.__version__
filename = open("Output_C.txt","w")
filename.write("Processing Image : " + str(filename_image) + '\n\n')
## Function to sort the contours : Parameters that you can tune : tolerance_factor and size 0f the image.Here, I have used a fix size of
## (800,800)
def get_contour_precedence(contour, cols):
tolerance_factor = 10
origin = cv2.boundingRect(contour)
return ((origin[1] // tolerance_factor) * tolerance_factor) * cols + origin[0]
## Loading the colored mask, resizing it to (800,800) and converting it from RGB to HSV space, so that the color values are emphasized
p_mask_c = cv2.cvtColor(cv2.resize(cv2.imread(filename_mask),(800,800)),cv2.COLOR_RGB2HSV);
# Loading the original Image
b_image_1 = cv2.resize(cv2.imread(filename_image),(800,800));
cv2.imshow("c_mask_preferred",p_mask_c)
cv2.waitKey();
# convert the target color to HSV, As our target mask portion to be considered is green. So I have chosen target color to be green
b = 0;
g = 255;
r = 0;
# Converting target color to HSV space
target_color = np.uint8([[[b, g, r]]])
target_color_hsv = cv2.cvtColor(target_color, cv2.COLOR_BGR2HSV)
# boundaries for Hue define the proper color boundaries, saturation and values can vary a lot
target_color_h = target_color_hsv[0,0,0]
tolerance = 20
lower_hsv = np.array([max(0, target_color_h - tolerance), 10, 10])
upper_hsv = np.array([min(179, target_color_h + tolerance), 250, 250])
# apply threshold on hsv image
mask = cv2.inRange(p_mask_c, lower_hsv, upper_hsv)
cv2.imshow("mask",mask)
cv2.waitKey()
# Eroding the binary mask, such that every white portion (grids) are seperated from each other, to avoid overlapping and mixing of
# adjacent grids
b_mask = mask;
kernel = np.ones((5,5))
#kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
sharp = cv2.erode(b_mask,kernel, iterations=2)
# Finding all the grids (from binary image)
contours, hierarchy = cv2.findContours(sharp,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print (' Number of contours', len(contours))
# Sorting contours
contours.sort(key=lambda x:get_contour_precedence(x, np.shape(b_mask)[0]))
#cv2.drawContours(b_image_1, contours, -1, (0,255,0), 1)
# Label variable for each grid/panel
label = 1;
b_image = b_image_1.copy();
temp =np.zeros(np.shape(b_image_1),np.uint8)
print (' size of temp',np.shape(temp), np.shape(b_image))
out_img = b_image_1.copy()
# Processing in each contour/label one by one
for cnt in contours:
cv2.drawContours(b_image_1,[cnt],0,(255,255,0), 1)
## Just to draw labels in the center of each grid
((x, y), r) = cv2.minEnclosingCircle(cnt)
x = int(x)
y = int(y)
r = int(r)
cv2.putText(b_image_1, "#{}".format(label), (int(x) - 10, int(y)),cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
##
cv2.drawContours(temp,[cnt],0,(255,255,255), -1)
#crop_img = np.bitwise_and(b_image,temp)
r = cv2.boundingRect(cnt)
crop_img = b_image[r[1]:r[1]+r[3], r[0]:r[0]+r[2]]
mean = cv2.mean(crop_img);
mean = np.array(mean).reshape(-1,1)
print (' Mean color', mean, np.shape(mean))
if mean[1] < 50:
cv2.putText(out_img, "M", (int(x) - 10, int(y)),cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 255), 1)
filename.write("Block number #"+ str(label)+ ' is : ' + 'Magenta'+'\n');
else:
cv2.putText(out_img, "G", (int(x) - 10, int(y)),cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 255), 1)
filename.write("Block number #"+ str(label)+ ' is : ' +'Gray'+'\n');
label = label+1;
cv2.imwrite("Block_order_C.PNG",b_image_1)
cv2.imwrite("Out_img.PNG",out_img)
filename.close()
cv2.imshow("preferred",b_image_1)
cv2.waitKey()
Error
[ WARN:0] global C:\projects\opencv-python\opencv\modules\imgcodecs\src\grfmt_tiff.cpp (449) cv::TiffDecoder::readData OpenCV TIFF: TIFFRGBAImageOK: Sorry, can not handle images with IEEE floating-point samples
Traceback (most recent call last):
File "Processing_C_preferred.py", line 32, in
p_mask_c = cv2.cvtColor(cv2.resize(cv2.imread(filename_mask),(800,800)),cv2.COLOR_RGB2HSV);
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4045: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
When you read in the image pass the cv::IMREAD_ANYDEPTH = 2 parameter as the second parameter in cv2.imread().
Changing your lines to
p_mask_c = cv2.cvtColor(cv2.resize(cv2.imread(filename_mask, 2),(800,800)),cv2.COLOR_RGB2HSV);
and
b_image_1 = cv2.resize(cv2.imread(filename_image, 2),(800,800));
removes the resize error you're seeing.
But you get another error when changing the color since your TIFF image apparently has only one channel so cv2.COLOR_RGB2HSV won't work..
You could also use multiple flags like cv::IMREAD_COLOR = 1,
p_mask_c = cv2.cvtColor(cv2.resize(cv2.imread(filename_mask, 2 | 1),(800,800)),cv2.COLOR_BGR2HSV);
to read in a color image. But you get a different error. Perhaps you understand this image better than I do and can solve the problem from here on out.

What is the shape/order of points expected by CV2 plotChessboardCorners and cornersSubPix?

I'm trying to pass an array of points to the plotChessboardCorners and cornersSubPix functions, but I'm getting these error messages:
Exception: ..\..\..\..\opencv\modules\calib3d\src\calibinit.cpp:1944:
error: (-215) nelems >= 0 in function cv::drawChessboardCorners
Exception: ..\..\..\..\opencv\modules\imgproc\src\cornersubpix.cpp:257:
error: (-215) ncorners >= 0 && corners.depth() == CV_32F in function cv::cornerSubPix
which I think have to do with the ordering of the array. However, findChessboardCorners returns an array of shape [npoints, 1, 2], which is the same as the shape of the array I'm passing?
My code is:
if (findChessboardCorners failed):
corners = (reconstructed corners of shape [npoints, 2])
corners.shape = (len(corners),1,2) #reshape to shape expected by openCV
numfound = 0 if corners is None else len(corners)
if numfound==nX*nY: #reconstruction successful
figC = plt.figure('Corners found in image ' +str(i+1) + ' in camera ' +camnames[j])
plt.imshow(I, cmap='gray')
cv2.drawChessboardCorners(Inew, (nX,nY), corners, 1)
plt.show()
It was a datatype issue: the corners array needs to be float32.
The error message was being generated by this assert statement:
int nelems = corners.checkVector(2, CV_32F, true);
CV_Assert(nelems >= 0);

Opencv can't write video: cap_mjpeg_encoder.cpp:829: error: (-215)

I use opencv to open a video, process frame by frame and write it back to a video. For some videos, this works fine, but for others, I got this wired error when I try to write the frames back to a video:
cv2.error: /home/xxx/Documents/opencv_videos/opencv/modules/videoio/src/cap_mjpeg_encoder.cpp:829: error: (-215) img.cols == width && img.rows == height && channels == 3 in function write
Here is what my code looks like:
cap = cv2.VideoCapture('cut.avi')
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.avi',fourcc, 25.0, (1280,720))
count = 0
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
processed_frame = PROCESS(frame)
cv2.imwrite('temp.jpg',processed_frame)
out.write(processed_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
I can, however, use cv2.imwrite to write the process frame to a jpeg image on disk. Does anyone know where this error comes from? Thanks!

Resources