I am trying to undistort a fisheye image taken from a camera. I have already gotten the camera parameters needed. However, when I run the code below:
Mat cammatrix = cv::Mat::zeros(3,3, CV_64F);
cammatrix.at<double>(0,0) = 3.7089418826568277e+002;
cammatrix.at<double>(1,1) = 3.7179355652545451e+002;
cammatrix.at<double>(0,2) = 3.4450520804288089e+002;
cammatrix.at<double>(1,2) = 2.5859133287932718e+002;
cammatrix.at<double>(2,2) = 1.0;
std::vector<double> distortcoeff;
double tempdoub = -2.2022994789941803e+000;
distortcoeff.push_back(tempdoub);
tempdoub = 4.4652133910671958e+000;
distortcoeff.push_back(tempdoub);
tempdoub = 6.8050071879644780e-001;
distortcoeff.push_back(tempdoub);
tempdoub = -1.7697153575434696e+000;
distortcoeff.push_back(tempdoub);
// Process images here (optional)
Mat img_scene (current);
if(!img_scene.data )
{ std::cout<< " --(!) Error reading images " << std::endl; return -1; }
img_scene.convertTo(img_scene, CV_32FC2);
cv::fisheye::undistortPoints(img_scene, img_scene, cammatrix, distortcoeff);
I get this error:
OpenCV Error: Assertion failed (distorted.type() == CV_32FC2 || distorted.type() == CV_64FC2) in undistortPoints
Not sure why this is happening because I have the .convertTo line right before converting it to CV_32FC2. If anyone could help me fix this error, I would really appreciate it!
undistortPoints() function is retrieving undistorted pixel location, given it's current distorted location on the image. i.e it operates on points, not on images.
use fisheye::undistortImage for images.
Related
I am using OpenCV-4.2.0 (contrib). OpenCV does not show a labeled image created with connectedComponentsWithStats.
The relevant code is:
//Connected Compnents
std::cout << "Calculating Connected Components..." << std::endl;
Mat background_mask_labels(background_mask.size(), CV_32S);
Mat cc_stats,cc_centroids;
int nLabels = cv::connectedComponentsWithStats(background_mask, background_mask_labels, cc_stats, cc_centroids, 4);
// show output
imshow("Background Image", background_mask);
imshow("Background Labels", background_mask_labels);
waitKey(0);
When running the program in x64 Debug mode it throws an error:
OpenCV(4.2.0) Error: Assertion failed (src_depth != CV_16F && src_depth != CV_32S) in convertToShow, file c:\build\master_winpack-build-win64-vc15\opencv\modules\highgui\src\precomp.hpp, line 137
at
imshow("Background Labels", background_mask_labels);
But I dont't understand the error, since I declared background_mask_labels to be CV_32S in first place.
Even a background_mask_labels.convertTo(background_mask_labels , CV_32S); after the connectedComponents does not help - same output.
Thanks for helping!
I'm working cum clouds of points with PCL. I recently had to convert the color information of the points that are in RGB to Cielab.
I have seen that it is possible to do with OpenCV and then I used the following code:
pcl::PointCloud<pcl::PointXYZLAB>::Ptr convert_rgb_to_lab_opencv(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud) {
pcl::PointCloud <pcl::PointXYZLAB>::Ptr cloud_lab(new pcl::PointCloud <pcl::PointXYZLAB>);
cloud_lab->height = cloud->height;
cloud_lab->width = cloud->width;
for (pcl::PointCloud<pcl::PointXYZRGB>::iterator it = cloud->begin(); it != cloud->end(); it++) {
// Color conversion
cv::Mat pixel(1, 1, CV_8UC3, cv::Scalar(it->r, it->g, it->b));
cv::Mat temp;
cv::cvtColor(pixel, temp, CV_BGR2Lab);
pcl::PointXYZLAB point;
point.x = it->x;
point.y = it->y;
point.z = it->z;
point.L = temp.at<uchar>(0, 0);
point.a = temp.at<uchar>(0, 1);
point.b = temp.at<uchar>(0, 2);
cloud_lab->push_back(point);
}
return cloud_lab;
}
My question is: are the values I got correct? Should not LAB values be decimal and vary with negative numbers?
So I tried to do the conversion "manually" with the code available here.
When I visualized the two clouds in the CloudCompare I saw that they produced very similar views, even in the histogram.
Can someone explain to me why?
I use imread function by OpenCV3.0 gold on Ubuntu 14.04, and followed the web to installed OpenCV3.0 But the imread function dosen't work with absolute path.It can work like imread("a.jpg"),but not imread("\home\a\a.jpg") .I want to use the function to read image sequence.Here is my code:
char filename1[200];
char filename2[200];
sprintf(filename1, "/home/image_2/%06d.png", 0);
sprintf(filename2, "/home/image_2/%06d.png", 1);
//read the first two frames from the dataset
Mat img_1_c = imread(filename1);
Mat img_2_c = imread(filename2);
if ( !img_1_c.data || !img_2_c.data ) {
std::cout<< " --(!) Error reading images " << std::endl; return -1;
}
There are images in folder a,like 000000.png .When I run it ,it says"--(!) Error reading images".Can someone help me ?Thank you.
I'm already looking for hours but I can't find the problem.
I get the following error when I want to stitch two images together:
OopenCV error: assertion failed (y==0 || data && dims >=1 && (unsigned)y < (unsigned > size.p[0])) in unkown function...
This is the code (pano.jpg was already stitched together in a previous run of the algorithm were the same algorithm did work...):
cv::Mat img1 = imread("input2.jpg");
cv::Mat img2 = imread("pano.jpg");
std::vector<cv::Mat> vectest;
vectest.push_back(img2);
vectest.push_back(img1);
cv::Mat result;
cv::Stitcher stitcher = cv::Stitcher::createDefault( false );
stitcher.setPanoConfidenceThresh(0.01);
detail::BestOf2NearestMatcher *matcher = new detail::BestOf2NearestMatcher(false, 0.001/*=match_conf*/);
detail::SurfFeaturesFinder *featureFinder = new detail::SurfFeaturesFinder(100);
stitcher.setFeaturesMatcher(matcher);
stitcher.setFeaturesFinder(featureFinder);
cv::Stitcher::Status status = stitcher.stitch( vectest, result );
You can find the images here:
pano.jpg: https://dl.dropbox.com/u/5276376/pano.jpg
input2.jpg: https://dl.dropbox.com/u/5276376/input2.jpg
Edit:
I compiled opencv 2.4.2 myself but still the same problem...
The system crashes in the stitcher.cpp file on the following line:
blender_->feed(img_warped_s, mask_warped, corners[img_idx]);
In this feed function it crashed at this line:
int y_ = y - y_tl;
const Point3_<short>* src_row = src_pyr_laplace[i].ptr<Point3_<short> >(y_);
Point3_<short>* dst_row = dst_pyr_laplace_[i].ptr<Point3_<short> >(y);
And finally this assertion in mat.hpp:
template<typename _Tp> inline _Tp* Mat::ptr(int y)
{
CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
return (_Tp*)(data + step.p[0]*y);
}
strange that everything works fine for some people here...
I stitching images now,but not using stitch High Level Functionality instead encode every step by opencv2.4.2. As far as I know, you could have a try about first SurfFeaturesFinder, second BestOf2NearestMatcher. Just a try, Good luck!
I'm using cv::imread to load a image and do some processes with that image,
but I don't know why I can't read values of the returned Mat from imread function.
I used Mat.at method:
Mat iplimage = imread("Photo.jpg",1); //input
for(int i=0;i<iplimage.rows;i++){
for(int j=0;j<iplimage.cols;j++){
cout<<(int)iplimage.at<int>(i,j)<<" ";
}
cout<<endl;
}
But it appeared an error:
OpenCV Error: Assertion failed ( dims <= 2 && data && (unsigned)i0 <
(unsigned)size.p[0] &&
(unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) &&
((((Sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) -1))*4) & 15)
== elemSize1()) is unknown function, file: "c:\opencv2.2\include\opencv2\core\mat.hpp", line 517
But it is ok if I use the direct access way:
Mat iplimage = imread("Photo.jpg",1); //input
for(int i=0;i<iplimage.rows;i++){
for(int j=0;j<iplimage.cols;j++){
cout<<(int)iplimage.data[i*iplimage.cols + j]<<" ";
}
cout<<endl;
}
Could anyone tell me how can I use the Mat.at method to access the above Mat?
Thanks for your help!
See this answer. In your case, the returned Mat is 3 dimensional, hence iplimage.at<int> fails the assertion, you just need to access the intensities in each channel like the way the mentioned answer explain.
you are trying to load with 3 channel of image it will be fine if you change to this Mat iplimage = imread("Photo.jpg",0); //input
I found the solution. It is because I used :
inputImage.at<int>(i,j) or inputImage.at<float>(1,2)
instead of, (int)inputImage.at<uchar>(1,2) or (float)inputImage.at<uchar>(1,2)
Sorry for my carelessness!
Mat iplimage = imread("Photo.jpg",1) this read in a 3 channel colour image. You can use Mat iplimage = imread("Photo.jpg",0) to read in the image as greyscale so that your iplimage.at(i,j) would work. Please note that you should use .at if your image is 8bit instead of .at.
If your image is not 8bit, you should use iplimage = imread("Photo.jpg",CV_LOAD_IMAGE_ANYDEPTH)