2020-01-30 03:32:17 -0600
| received badge | ● Famous Question
(source)
|
2017-11-15 01:17:18 -0600
| received badge | ● Notable Question
(source)
|
2017-04-26 02:44:59 -0600
| received badge | ● Popular Question
(source)
|
2016-09-09 06:54:39 -0600
| answered a question | findContours - OpenCV 2.4.5, Heap Corruption I firmly confirm that findcontours is broken with Visual Studio 2010, in either Debug or Release builds ! See the sample minimal code below ( no resource or image is required, the input image is created on the fly). - It will fail with Visual studio 2010 in either debug build or release build
- It will faill with OpenCV 2.4 and OpenCV 3.1.0
- It will not fail with under linux/gcc or clang/osx or Visual Studio 2013
@lowlander256 answer seems to have nailed a potential reason. #include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/imgproc/imgproc.hpp>
void findcontours_OldApi(cv::Mat & segmentedImage, std::vector< std::vector<cv::Point> > & contoursList)
{
IplImage segmentedImageIpl = segmentedImage;//just clone header with no copying of data
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* contoursSeq = 0;
int numCont = cvFindContours(
&segmentedImageIpl, storage, &contoursSeq, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0, 0));
CvSeq *oneContour = contoursSeq;
while(oneContour)
{
std::vector<cv::Point> oneContourAsVector;
CvSeqReader reader;
cvStartReadSeq (oneContour, &reader);
for( int i = 0; i < oneContour->total; i++ )
{
CvPoint pt;
CV_READ_SEQ_ELEM( pt, reader );
oneContourAsVector.push_back(cv::Point(pt.x, pt.y));
}
oneContour = oneContour->h_next;
contoursList.push_back(oneContourAsVector);
}
cvReleaseMemStorage(&storage);
}
void test_findcontours()
{
std::vector< std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
cv::Mat inputImage = cv::Mat::zeros(cv::Size(400, 400), CV_8UC1);
for (int i = 0; i < 50; i++)
{
cv::Point2d pt(rand() % 400, rand() % 400);
cv::Size size(rand() % 40, rand() % 25);
cv::rectangle(inputImage, cv::Rect(pt, size), cv::Scalar(255), 2);
}
cv::imshow("inputImage", inputImage);
cv::waitKey();
//cv::findContours will corrupt the content of the contours vectors !
cv::findContours( inputImage, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE );
// The alternative solution below works; it uses a wrapper around the old C API !
//findcontours_OldApi(inputImage, contours);
cv::Mat debugImage = cv::Mat::zeros(inputImage.size(), CV_8UC3);
// The program will fail in these loops, because of the corrupted vector !
for( size_t i = 0; i < contours.size(); ++i )
{
const auto & oneContour = contours[i];
cv::Vec3b contourColor(255, rand() % 255, rand() % 255);
for( size_t j = 0; j < oneContour.size(); ++j )
{
auto &pt = oneContour[j];
debugImage.at<cv::Vec3b>(pt.y, pt.x) = contourColor;
}
}
cv::imshow("contours", debugImage);
cv::waitKey();
}
|
2016-09-09 04:54:50 -0600
| commented answer | opencv/findContour crashes, v2.4.4, MS visual studio 2010. edit:damaged head. Hum, I tried your solution with open 3.1.0 and 2.4.11. I still have the crash. Thanks anyway |
2016-02-17 01:24:26 -0600
| received badge | ● Enthusiast
|
2016-02-16 05:11:17 -0600
| received badge | ● Nice Question
(source)
|
2016-02-15 17:46:33 -0600
| commented answer | solvePnP fails with perfect coordinates (and cvPOSIT passes) Sorry about the confusion about unstable results : it was caused by an error in the code (see Eduardo's answer). |
2016-02-15 17:44:01 -0600
| commented answer | solvePnP fails with perfect coordinates (and cvPOSIT passes) Thanks Eduardo,
You were right about the 7th argument that was commented out. It was causing the unstable results under osx. I now have the exact same results as you :
with cv::SOLVEPNP_DLS, cv::SOLVEPNP_EPNP, cv::SOLVEPNP_UPNP the reprojection error is about 44, with SOLVE_ITERATIVE about 8.94 (and zero if you set useExtrinsicGuess to true with initial vector set to zero). |
2016-02-15 12:04:22 -0600
| commented answer | solvePnP fails with perfect coordinates (and cvPOSIT passes) I teste the program under linux, and it works ok. So it seems that the issue of having unpredictable results is limited to OSX. |
2016-02-15 02:50:45 -0600
| received badge | ● Supporter
(source)
|
2016-02-15 02:49:54 -0600
| commented answer | solvePnP fails with perfect coordinates (and cvPOSIT passes) |
2016-02-15 02:31:30 -0600
| received badge | ● Editor
(source)
|
2016-02-15 02:26:07 -0600
| answered a question | solvePnP fails with perfect coordinates (and cvPOSIT passes) Note 1 : this answer was edited following Eduardo's edit of his own answer. The initial unstable results under OSX were due to an error in the code. Sorry about this. Note 2 :
I made a more thorough analysis, that compares several strategies (after having added the distorsion coeffs of the camera). The code would not fit here, but it can be found at https://github.com/pthom/TestSolvePnp enum SolvePnpStrategy
{
Strategy_MySolvePnp_Epnp, //an home baked adapter of epnp using the epnp library source code
Strategy_MySolvePnpPosit, //an home baked adapter of cvPOSIT (deprecated "OpenCV 1" pose estimation method)
Strategy_solvePnp_P3p, // opencv SOLVEPNP_P3P method
Strategy_solvePnp_Iterative_InitialGuess, // opencv SOLVEPNP_ITERATIVE method with an initial guess
Strategy_solvePnp_Epnp //opencv SOLVEPNP_EPNP method
};
Based on my experimentations, the order of the 3d points and image points does matter. It has to be adapted depending upon the strategy ! - With opencv's SOLVEPNP_EPNP the error can go down to 23.03 pixel. The order of the points does matter
- With MySolvePnpEpnp (an home baked adapter of epnp using the epnp library source code), the error is about 6.742 pixels, and the order is important It is strange that this "rewrite" gives different results
- With MySolvePnpPosit, the error is about 4.911 pixels and the order is important (in other cases the reprojection error is about 1278 pixels !)
- With solvePnp_P3p (cv::SOLVEPNP_P3P) the error is about 0.02961 pixels and the order does not matter much
- With solvePnp_Iterative_InitialGuess (cv::SOLVEPNP_ITERATIVE) the error can be 0 pixels if a good initial extrinsic guess is given (otherwise don't hope for any convergence). The order does not matter much with SOLVEPNP_ITERATIVE.
Original answer below Many thanks Eduardo for your thorough analysis, and the time you invested for it :-) This is not an answer, but a recap of my results following your suggestions. Note : my tests were run under OSX El Capitan. I did not test linux or windows yet.
I have the same results as you : - With cv::SOLVEPNP_DLS, cv::SOLVEPNP_EPNP, cv::SOLVEPNP_UPNP , the reprojection error is 44.8698 pixels
- With cv::SOLVEPNP_ITERATIVE the reprojection error is 8.94 pixels (except if you set useExtrinsicGuess to true and use null vectors as inital rotation and translation}, and the returned pose is behind the camera
- With cv::SOLVEPNP_P3P the reprojection error is about 0.21 pixels
I was much less sucessful than you : I saw unstable results when using the optimized version, and no success when disabling the optimizations.
Here is a recap of what I saw: - using opencv 2.4.11: cv::setUseOptimized(false) does not change the result
- using opencv 3.1 : cv::setUseOptimized(false) leads to unstable results!
Here is a full recap of my results with opencv 3.1. I also tested the master branch, with the same results: Using cv::SOLVEPNP_UPNP - optimized version : if you run the program several times, the result you get can be 4535.94, 711.716, 302630, nan or 0 !!!
- unoptimized version : result is always 711.832
Using cv::SOLVEPNP_DLS - optimized version: result vary. I obtained ...
(more) |
2016-02-14 02:06:02 -0600
| commented question | solvePnP fails with perfect coordinates (and cvPOSIT passes) millimeters also, with a chessboard and opencv calibration functions. Are you surprised like me to see that cvPOSIT works here and solvePnp seems to fail ? |
2016-02-13 14:41:54 -0600
| commented question | solvePnP fails with perfect coordinates (and cvPOSIT passes) Hello FooBar,
The pixel coordinates and object Points + rotation were obtained from a real image on which my project works. The rotation was obtained through cvPOSIT and confirmed with Matlab. Nothing special about this (rodrigues) rotation vector.
Concerning the unit, the sizes are in millimeters. I tried to change it to meters (i.e divide the object size and translation by 1000), with no success. However if I divide the object size and translation by 10000, the error goes down to zero. Strange... |
2016-02-12 23:58:17 -0600
| received badge | ● Student
(source)
|
2016-02-12 23:50:19 -0600
| asked a question | solvePnP fails with perfect coordinates (and cvPOSIT passes) Hi, I have found a set of parameters for which solvePnP does not give correct results although : - the image points are perfect coordinates obtained with projectPoints applied to the objectPoints (no error at all in the image points)
- the objects points consist of 4 non coplanar points
- the parameters are quite innocent looking
- cvPOSIT is perfectly able to reconstruct the pose, when solvePnp seems not to be able to do so
This problem occurs with opencv 2.4.1 and 3.1.0, under osx and windows When I reproject the point using the rotation and translation provided by solvePnP, I have a total error of about 8 pixels (I have tried all flags : ITERATIVE, P3P and EPNP) If instead I use the rotation and translation provided by cvPOSIT the reprojection error can be reduced to 0.00014 pixels ! Here is a short program that demonstrates the error : I simply instantiate a camera matrix, some object points and their corresponding image points obtained through projectPoints.
Then I compare the reprojection and I get 8 pixels of error! #include <opencv2/core/core.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <iostream>
void TestSolvePnp()
{
std::vector<cv::Point3f> objectPoints;
objectPoints.push_back(cv::Point3f(0.f, 0.f, 0.f));
objectPoints.push_back(cv::Point3f(62.3174629f, 15.7940502f, 0.819983721f));
objectPoints.push_back(cv::Point3f(-62.1225319f, 15.7540569f, 0.819464564f));
objectPoints.push_back(cv::Point3f(-0.372639507f, 16.4230633f, -36.5060043f));
cv::Mat_<double> cameraIntrinsicParams(cv::Size(3, 3));
cameraIntrinsicParams = 0.;
cameraIntrinsicParams(0, 0) = 3844.4400000000001f;
cameraIntrinsicParams(1, 1) = 3841.0599999999999f;
cameraIntrinsicParams(0, 2) = 640.f;
cameraIntrinsicParams(1, 2) = 380.f;
cameraIntrinsicParams(2, 2) = 1.f;
cv::Mat_<double> distCoeffs(cv::Size(1, 4));
distCoeffs = 0.f;
cv::Mat_<double> rotation(cv::Size(3, 1));
rotation(0,0) = 0.07015543380659847f;
rotation(0,1) = 0.06922079477774973f;
rotation(0,2) = -0.00254676088325f;
cv::Mat_<double> translation(cv::Size(3, 1));
translation(0,0) = -35.3236f;
translation(0,1) = -48.1699f;
translation(0,2) = 769.068f;
std::vector<cv::Point2f> imagePoints;
cv::projectPoints(objectPoints, rotation, translation, cameraIntrinsicParams, distCoeffs, imagePoints);
cv::Mat_<double> rotation2(cv::Size(3, 1));
cv::Mat_<double> translation2(cv::Size(3, 1));
cv::solvePnP(objectPoints, imagePoints,
cameraIntrinsicParams, distCoeffs,
rotation2, translation2,
//false,//useExtrinsicGuess
//Choose solvepnp flag below
//cv::SOLVEPNP_UPNP
//cv::SOLVEPNP_DLS
//cv::SOLVEPNP_EPNP
//cv::SOLVEPNP_P3P
cv::SOLVEPNP_ITERATIVE
);
std::vector<cv::Point2f> imagePoints_Reproj(3);
cv::projectPoints(objectPoints, rotation2, translation2, cameraIntrinsicParams, distCoeffs, imagePoints_Reproj);
float sum = 0.;
for (size_t i = 0; i < imagePoints.size(); i++)
sum += DistPoint(imagePoints_Reproj[i], imagePoints[i]);
std::cout << "sum=" << sum << std::endl;
}
int main(int argc, char **argv)
{
TestSolvePnp();
}
However, if I replace the call to solvePnp by a call to cvPOSIT, the reprojection error goes down to 0.00014 pixels ! Below is the implementation of a function named MySolvePnpPosit() that reproduces the behavior of solvePnP, but uses cvPOSIT internally => when I use it, the error goes down almost zero. namespace
{
void Posit_IdealCameraCoords(const std::vector<cv::Point3f> & objectPoints, const std::vector<cv::Point2f> & imagePoints,
cv::Mat &outRotationEstimated, cv::Mat & outTranslationEstimated)
{
CvPoint2D32f * imagePoints_c = (CvPoint2D32f ... (more) |