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

CameraCalibration problems #935

Open
xiaoli-user opened this issue Jul 23, 2024 · 5 comments
Open

CameraCalibration problems #935

xiaoli-user opened this issue Jul 23, 2024 · 5 comments

Comments

@xiaoli-user
Copy link

I download code from https://github.com/spmallick/learnopencv/tree/master/CameraCalibration
I get valid camera parameters when I use the python code. But when I run the code in vs studio 2019, it is very slow and finally get invalid parameters.
This is the result by cpp code.
1111

python code
3333
I guess is there something wrong?

@brmarkus
Copy link

In your C++ environment you very likely have a (very) different version of OpenCV installed than in your Python environment.

Depending on your camera stream you might need or not need corresponding video codecs (whether your camera provides a "raw" stream (no codec required) or a compressed stream (codec required like MPEG-decoder or AVC/h.264-decoder).

@xiaoli-user
Copy link
Author

Thank you very much, bro! I am confused about codec, so I show my cpp code with which I take images form camera! Could you please tell me what should I do if possible?

I took 49 images from the camera. They are used to calibrate my camera as the above code demos do. In C++ environment, findChessboardCorners and imshow both run correct, but I can't get correct results that are not same with python results. The two environments use the same images. I really confused which step of my codes that make the difference.

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/opencv.hpp>  
#include <iostream>
#include <stdio.h>
#include <iomanip>

using namespace cv;
using namespace std;


int main(int, char**)
{
	Mat frame;
	//--- INITIALIZE VIDEOCAPTURE
	VideoCapture cap;

	// open the default camera using default API
	// cap.open(0);
	// OR advance usage: select any API backend
	int deviceID = 0; // 0 = open default camera
	int apiID = cv::CAP_ANY; // 0 = autodetect default API
	// open selected camera using selected API
	cap.open(deviceID, apiID);
	cap.set(CAP_PROP_FRAME_WIDTH, 1280);
	cap.set(CAP_PROP_FRAME_HEIGHT, 720);
	// check if we succeeded
	if (!cap.isOpened()) {
		cerr << "ERROR! Unable to open camera\n";
		return -1;
	}

	//--- GRAB AND WRITE LOOP
	cout << "Start grabbing" << endl
		<< "Press any key to terminate" << endl;

	int count = 0;
	for (;;)
	{
		// wait for a new frame from camera and store it into 'frame'
		cap.read(frame);
		// check if we succeeded
		if (frame.empty()) {
			cerr << "ERROR! blank frame grabbed\n";
			break;
		}
		// show live and wait for a key with timeout long enough to show images
		imshow("Live", frame);

		int key = waitKey(10);
		if (key == 32) {
			std::ostringstream name;
			name << std::setw(6) << std::setfill('0') << std::to_string(++count);
			imwrite("photos\\" + name.str() + ".jpg", frame);
			cout << "photos\\" + name.str() + ".jpg" << endl;
		}
		else if (key == 27) {
			break;
		}
	}
	// the camera will be deinitialized automatically in VideoCapture destructor
	return 0;
}

@brmarkus
Copy link

brmarkus commented Jul 24, 2024

This code to grab and capture a frame from the camera and displaying it looks good.

There is a lot happening in the background automatically (grabbing and capturing and decoding if needed to finally store each pixel into Mat frame).

More interesting is the calibration - and how it is done.
When I did the calibration I often needed multiple "interactive experiments": displaying the different chess-patterns, moving the camera, changing light conditions and watching how OpenCV displays the detected patterns on the screen, drawing detected lines in different colors; and when the drawn lines align with the chess-patterns then press a key to stop calibration and the code will write the calibration matrix into a file.
See the pictures with the drawn lines under e.g. "https://docs.opencv.org/4.x/d4/d94/tutorial_camera_calibration.html":

image

image

And watch the Youtube-video: sometimes you need to move the different patterns in front of the camera until you see the lines matching suddenly.

First check the installed versions and variants of OpenCV you have installed; there are different versions available to download and install; when building OpenCV from source-code you can influence more parameters (e.g. "headless" without rendering&displaying capabilities; using ffmpeg or gstreamer or Intel-MediaSDK/OneVPP for decoding).

@xiaoli-user
Copy link
Author

Thank you very very much!

I got promising result by set Calibrate_FixPrincipalPointAtTheCenter 0 and Calibrate_AssumeZeroTangentialDistortion 0. I understand that is equal to flags==0 in function calibrateCameraRO .

The code is elegant, really! Thank you for sharing! I learnt a lot details about calibrating. But I still feel a little confused by the parameter Calibrate_FixPrincipalPointAtTheCenter finnaly used by calibrateCameraRO.

I also review the code in https://github.com/spmallick/learnopencv/blob/master/CameraCalibration/cameraCalibrationWithUndistortion.cpp. It may mistake the image size in line cv::calibrateCamera(objpoints, imgpoints,cv::Size(gray.rows,gray.cols),cameraMatrix,distCoeffs,R,T);.
It works well by cv::Size(gray.cols, gray.rows).

1 similar comment
@xiaoli-user
Copy link
Author

Thank you very very much!

I got promising result by set Calibrate_FixPrincipalPointAtTheCenter 0 and Calibrate_AssumeZeroTangentialDistortion 0. I understand that is equal to flags==0 in function calibrateCameraRO .

The code is elegant, really! Thank you for sharing! I learnt a lot details about calibrating. But I still feel a little confused by the parameter Calibrate_FixPrincipalPointAtTheCenter finnaly used by calibrateCameraRO.

I also review the code in https://github.com/spmallick/learnopencv/blob/master/CameraCalibration/cameraCalibrationWithUndistortion.cpp. It may mistake the image size in line cv::calibrateCamera(objpoints, imgpoints,cv::Size(gray.rows,gray.cols),cameraMatrix,distCoeffs,R,T);.
It works well by cv::Size(gray.cols, gray.rows).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants