How to use findChessboardCorners and calibrateCamera? - c++

I want to use findChessboardCorners with calibrateCamera but encounter errors when using calibrateCamera. The output is not very helpful.
OpenCV Error: Assertion failed (i < 0) in cv::_OutputArray::create
static const Size patterSize(8, 6);
auto image = imread("x.jpg", IMREAD_GRAYSCALE);
Mat corners;
auto found = findChessboardCorners(image, patterSize, corners);
//constructing objectPoints
vector<vector<Vec3f>> objectPoints;
objectPoints.push_back(vector<Vec3f>());
for (int row = 0; row < patterSize.height; row++) {
for (int col = 0; col < patterSize.width; col++) {
objectPoints.at(0).push_back(Vec3f(row, col, 0));
}
}
vector<vector<Vec2f>> imagePoints;
imagePoints.push_back(vector<Vec2f>());
for (int row = 0; row < patterSize.height; row++) {
for (int col = 0; col < patterSize.width; col++) {
int num = row*patterSize.width + col;
imagePoints.at(0).push_back(corners.at<Vec2f>(num, 0));
}
}
Mat cameraMatrix, distMatrix, rvecs, tvecs;
calibrateCamera(objectPoints, imagePoints, Size(image.size().width, image.size().height), cameraMatrix, distMatrix, rvecs, tvecs);

The decleration of rvecs, tvecs should be vector<Mat> rather than Mat, and then everything will be fine. For more information, visit 3calibration.cpp.

Related

c++ image rotation 90 degree

I want to read the picture in grayscale and rotate it 90 degrees, but I am making a mistake somewhere. Is there anyone who can help?
cv::Mat image = imread(".../jpg", IMREAD_GRAYSCALE);
cv::resize(image, image,cv::Size(400,400));
cv::Mat imageson;
int rows = image.rows;
int cols = image.cols;
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows ; j++)
{
imageson.at<uchar>(i,j) = image.at<uchar>(rows-1-j,i);
}
}
cv::imshow("image",imageson);
cv::waitKey(0);
// im beginner btw. output = (exit value:-1)

C++ OpenCV How can I get the values of Binary Histogram image 0 and 255?

My code shows me values that are not accurate and i am not sure what else to try. My goal is to get the values of y such as rows, so that I can read the image and put it in an array. Ive looked at examples and Stack Overflow is literally my last option.
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat Rgb;
Mat Grey;
Mat image;
//Mat Histogram;
Rgb = imread("license.jpg", WINDOW_AUTOSIZE);
cvtColor(Rgb, Grey, cv::COLOR_BGR2GRAY);
threshold(Grey, image, 150, 250, THRESH_BINARY);
int histogram[255];
for (int i = 0; i < 255; i++)
{
histogram[i] = 0;
}
for (int y = 0; y < image.rows; y++)
//for (int x = 0; x < image.cols; x++)
histogram[(int)image.at<uchar>(y)]++;
//histogram[(int)image.at<uchar>(y, x)]++;
for (int i = 0; i < 255; i++)
cout << histogram[i] << " ";
// draw the histograms
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double)hist_w / 255);
Mat histImage(hist_h, hist_w, CV_8UC1, Scalar(255, 255, 255));
int max = histogram[0];
for (int i = 1; i < 256; i++) {
if (max < histogram[i]) {
max = histogram[i];
}
}
for (int i = 0; i < 255; i++) {
histogram[i] = ((double)histogram[i] / max)*histImage.rows;
}
for (int i = 0; i < 255; i++)
{
line(histImage, Point(bin_w*(i), hist_h),
Point(bin_w*(i), hist_h - histogram[i]),
Scalar(0, 0, 0), 1, 8, 0);
}
imshow("Image", image);
waitKey(0);
cv::destroyAllWindows();
return 0;
}
Results have numbers like 319 and other values and I am only looking to get 0 or 255

OpenCV Error: Camera Calibration: Assertion failed in matrix_wrap.cpp

The Error:
OpenCV(3.4.1) Error: Assertion failed (i < 0) in cv::debug_build_guard::_InputArray::getMat_,
file C:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\matrix_wrap.cpp, line 50
The Code inside "matrix_wrap.cpp" that triggers the Error:
if( k == STD_VECTOR )
{
CV_Assert( i < 0 );
int t = CV_MAT_TYPE(flags);
const std::vector<uchar>& v = *(const std::vector<uchar>*)obj;
return !v.empty() ? Mat(size(), t, (void*)&v[0]) : Mat();
}
Entire Code:
#include <iostream>
#include <opencv2/opencv.hpp>
int main(int argc, char *argv[])
{
cv::Mat img = cv::imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
cv::Size imageSize = cv::Size(img.size[0], img.size[1]);
cv::Mat cameraMatrix, distCoeffs;
double squareSize = 30;
cv::Size boardSize = cv::Size(6, 11);
std::vector<cv::Point2f> imagePoints;
std::vector<cv::Point3f> objectPoints;
for (int i = 0; i < boardSize.height; i++) {
for (int j = 0; j < boardSize.width; j++) {
objectPoints.push_back(
cv::Point3f(float(j * squareSize), float(i * squareSize), 0));
}
}
std::vector<cv::Mat> rvecs, tvecs;
bool found = false;
if (img.size[0] > 1)
{
bool found = findChessboardCorners(img, boardSize, imagePoints, cv::CALIB_CB_ADAPTIVE_THRESH);
if (found)
{
drawChessboardCorners(img, boardSize, imagePoints, found);
}
objectPoints.resize(imagePoints.size(), objectPoints[0]);
double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs);
cv::namedWindow("My Image");
cv::imshow("My Image", img);
cv::waitKey(1000);
}
return 0;
}
I want to use a basic camera calibration with a straigth chessboard.
Until the function "calibrateCamera" everything works fine, but I can't figure out why this Error shows up.
The size of "objectPoints" and "imagePoints" is the same.
Thank you in advance.
P.S.:
I'm new in this forum and also in OpenCV ;)
As per this source of opencv, lines 3139 and 3142 :
CV_Error(CV_StsUnsupportedFormat, "objectPoints should contain vector of vectors of points of type Point3f");
CV_Error(CV_StsUnsupportedFormat, "imagePoints1 should contain vector of vectors of points of type Point2f");
the types for objectPoints and imagePoints should be std::vector<std::vector<Point{2|3}f>>

from float array to mat , concatenate blocks of image

I have an image 800x800 which is broken down to 16 blocks of 200x200.
(you can see previous post here)
These blocks are : vector<Mat> subImages;
I want to use float pointers on them , so I am doing :
float *pdata = (float*)( subImages[ idxSubImage ].data );
1) Now, I want to be able to get again the same images/blocks, going from float array to Mat data.
int Idx = 0;
pdata = (float*)( subImages[ Idx ].data );
namedWindow( "Display window", WINDOW_AUTOSIZE );
for( int i = 0; i < OriginalImgSize.height - 4; i+= 200 )
{
for( int j = 0; j < OriginalImgSize.width - 4; j+= 200, Idx++ )
{
Mat mf( i,j, CV_32F, pdata + 200 );
imshow( "Display window", mf );
waitKey(0);
}
}
So , the problem is that I am receiving an
OpenCV Error: Assertion failed
in imshow.
2) How can I recombine all the blocks to obtain the original 800x800 image?
I tried something like:
int Idx = 0;
pdata = (float*)( subImages[ Idx ].data );
Mat big( 800,800,CV_32F );
for( int i = 0; i < OriginalImgSize.height - 4; i+= 200 )
{
for( int j = 0; j < OriginalImgSize.width - 4; j+= 200, Idx++ )
{
Mat mf( i,j, CV_32F, pdata + 200 );
Rect roi(j,i,200,200);
mf.copyTo( big(roi) );
}
}
imwrite( "testing" , big );
This gives me :
OpenCV Error: Assertion failed (!fixedSize()) in release
in mf.copyTo( big(roi) );.
First, you need to know where are your subimages into the big image. To do this, you can save the rect of each subimage into the vector<Rect> smallImageRois;
Then you can use pointers (keep in mind that subimages are not continuous), or simply use copyTo to the correct place:
Have a look:
#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
Mat3b img = imread("path_to_image");
resize(img, img, Size(800, 800));
Mat grayImg;
cvtColor(img, grayImg, COLOR_BGR2GRAY);
grayImg.convertTo(grayImg, CV_32F);
int N = 4;
if (((grayImg.rows % N) != 0) || ((grayImg.cols % N) != 0))
{
// Error
return -1;
}
Size graySize = grayImg.size();
Size smallSize(grayImg.cols / N, grayImg.rows / N);
vector<Mat> smallImages;
vector<Rect> smallImageRois;
for (int i = 0; i < graySize.height; i += smallSize.height)
{
for (int j = 0; j < graySize.width; j += smallSize.width)
{
Rect rect = Rect(j, i, smallSize.width, smallSize.height);
smallImages.push_back(grayImg(rect));
smallImageRois.push_back(rect);
}
}
// Option 1. Using pointer to subimage data.
Mat big1(800, 800, CV_32F);
int big1step = big1.step1();
float* pbig1 = big1.ptr<float>(0);
for (int idx = 0; idx < smallImages.size(); ++idx)
{
float* pdata = (float*)smallImages[idx].data;
int step = smallImages[idx].step1();
Rect roi = smallImageRois[idx];
for (int i = 0; i < smallSize.height; ++i)
{
for (int j = 0; j < smallSize.width; ++j)
{
pbig1[(roi.y + i) * big1step + (roi.x + j)] = pdata[i * step + j];
}
}
}
// Option 2. USing copyTo
Mat big2(800, 800, CV_32F);
for (int idx = 0; idx < smallImages.size(); ++idx)
{
smallImages[idx].copyTo(big2(smallImageRois[idx]));
}
return 0;
}
For concatenating the sub-images into a single squared image, you can use the following function:
// Important: all patches should have exactly the same size
Mat concatPatches(vector<Mat> &patches) {
assert(patches.size() > 0);
// make it square
const int patch_width = patches[0].cols;
const int patch_height = patches[0].rows;
const int patch_stride = ceil(sqrt(patches.size()));
Mat image = Mat::zeros(patch_stride * patch_height, patch_stride * patch_width, patches[0].type());
for (size_t i = 0, iend = patches.size(); i < iend; i++) {
Mat &patch = patches[i];
const int offset_x = (i % patch_stride) * patch_width;
const int offset_y = (i / patch_stride) * patch_height;
// copy the patch to the output image
patch.copyTo(image(Rect(offset_x, offset_y, patch_width, patch_height)));
}
return image;
}
It takes a vector of sub-images (or patches as I refer them to) and concatenates them into a squared image. Example usage:
vector<Mat> patches;
vector<Scalar> colours = {Scalar(255, 0, 0), Scalar(0, 255, 0), Scalar(0, 0, 255)};
// fill vector with circles of different colours
for(int i = 0; i < 16; i++) {
Mat patch = Mat::zeros(100,100, CV_32FC3);
circle(patch, Point(50,50), 40, colours[i % 3], -1);
patches.push_back(patch);
}
Mat img = concatPatches(patches);
imshow("img", img);
waitKey();
Will produce the following image
print the values of i and j before creating Mat mf and I believe you will soon be able to find the error.
Hint 1: i and j will be 0 the first time
Hint 2: Use the copyTo() with a ROI like:
cv::Rect roi(0,0,200,200);
src.copyTo(dst(roi))
Edit:
Hint 3: Try not to do such pointer fiddling, you will get in trouble. Especially if you're ignoring the step (like you seem to do).

OpenCV Camera Calibration, Assertion Failed <ni > 0 && ni==ni1>

I am trying to perform camera calibration using openCV. On the same example data from openCV samples this works great, but my source generates an error:
Assertion failed <ni > 0 && ni==ni1>... collectCallibrationData.. calibration.cpp line 3193"
objectPoints and imagePoints are the same size. Changing other inputs dont affect error code.
Can anyone help me deal with it? The code follows:
void main()//enter code here
{
//initial operations, declatarions etc
int *Asize; float *APoints;
float AAPoints[]= {/* My Array */};
int AAsize[] = {4,54,2}; //My array size
Asize = AAsize; APoints = AAPoints; // will be called as dll that is why this wierd attribution
int page = *(Asize); int row = *(Asize+1); int col = *(Asize +2);
Point3f pointBuf;
vector<vector<Point3f>> imagePoints;
vector<Point3f> vectBuf;
Size boardSize;
boardSize.height = 6; boardSize.width = 9;
Mat cameraMatrix, distCoeffs;
float squareSize=50;
//change 1d array to vector<vector<point3f>>
for (int i = 0; i<page; i++)
{
for (int j = 0; j<row; j++)
{
pointBuf.x = *(APoints + (i*(row*col))+j*2);
pointBuf.y = *(APoints + (i*(row*col))+j*2+1);
pointBuf.z = 0;
vectBuf.push_back(pointBuf);
}
imagePoints.push_back(vectBuf);
vectBuf.clear();
}
// create objectPoint vector<vector<Points3f>>
vector<vector<Point3f>> objectPoints;
vectBuf.clear();
for( int i = 0; i < boardSize.height; ++i )
{
for( int j = 0; j < boardSize.width; ++j )
vectBuf.push_back(Point3f(float( j*squareSize ), float( i*squareSize ), 0));
}
objectPoints.resize(imagePoints.size(),vectBuf);
//initialize starting variables for calibration
cameraMatrix = Mat::eye(3, 3, CV_64F);
distCoeffs = Mat::zeros(1, 1, CV_64F);
cameraMatrix.at<double>(0,0) = 1.0;
vector<Mat> rvecs, tvecs;
Size imageSize; imageSize.width = 2040; imageSize.height = 2040;
double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs);
}
There is a trouble with the use of cv::calibrateCamera(). It requires std::vector<std::vector<cv::Point3f>> as the first argument and std::vector<std::vector<cv::Point2f>> as the second.
e.g.:
cv::Size imageSize(2040, 2040);
cv::Mat cameraMatrix, distCoeffs;
std::vector<cv::Mat> rvecs, tvecs;
std::vector<std::vector<cv::Point2f> > imagePoints;
std::vector<std::vector<cv::Point3f> > objectPoints;
// Fill imagePoints and objectPoints
...
cv::calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs);
I met the same issue, too. In my case, there is a bug to convert vector< vector < Point3f > > to InputArrayOfArrays.
You may run the following sample code to check it in your system.
using namespace std;
using namespace cv;
/** #function main */
int main( void )
{
vector<Point3f> objectPoints_tmp(54);
objectPoints_tmp.clear();
for( int i = 0; i < 9; ++i )
for( int j = 0; j < 6; ++j )
objectPoints_tmp.push_back(Point3f(j*50, i*50, 0.0f));
vector<vector<Point3f> > objectPoints(7, objectPoints_tmp);
InputArrayOfArrays OBJPOINT = objectPoints; std::cout << (int) OBJPOINT.total() << std::endl;
for( int i = 0; i < 7; ++i )
std::cout << OBJPOINT.getMat(i).checkVector(3, CV_32F) << std::endl;
waitKey(0);
return 0;
}
Normally, it should print out
7
54
54
54
54
54
54
54
In my NG case, it prints out random numbers.
I guess that the opencv lib is not fit with my system(win7 x64 + vs2012). After rebuild opencv myself, it may work now. You may google keyword "Win7x64 VS2012 OpenCV CMake TBB".