kylemcdonald / ofxCv

Alternative approach to interfacing with OpenCv from openFrameworks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ofxCv::Calibration::load segfaults on features

xionluhnis opened this issue · comments

Hi,

I tried to calibrate my camera with ofxCv::Calibration, then saved the result to a xml file.
Up to that point, all was ok. Then I tried to reload the file, and it segfaults with:

OpenCV Error: Null pointer (Null pointer to reader or destination array) in cvReadRawDataSlice, file /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/persistence.cpp, line 3245
terminate called after throwing an instance of 'cv::Exception'
what(): /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/persistence.cpp:3245: error: (-27) Null pointer to reader or destination array in function cvReadRawDataSlice

This happens in the loading loop over the features.

The calibration file I was trying to load (after having saved it) is the following:

<?xml version="1.0"?>
<opencv_storage>
<cameraMatrix type_id="opencv-matrix">
  <rows>3</rows>
  <cols>3</cols>
  <dt>d</dt>
  <data>
    5.6983490484288513e+03 0. 7.0936105247955243e+02 0.
    5.2727231873234778e+03 4.4797449542451722e+02 0. 0. 1.</data></cameraMatrix>
<imageSize_width>1280</imageSize_width>
<imageSize_height>1024</imageSize_height>
<sensorSize_width>0</sensorSize_width>
<sensorSize_height>0</sensorSize_height>
<distCoeffs type_id="opencv-matrix">
  <rows>5</rows>
  <cols>1</cols>
  <dt>d</dt>
  <data>
    5.6904150379717668e-02 -6.0602659405402221e+01
    1.9536145001511358e-02 -4.9046475422828964e-03
    -6.4968374505584781e-01</data></distCoeffs>
<reprojectionError>3.5580790042877197e-01</reprojectionError>
<features>
  <_><_>
      2.94070099e+02 3.88889893e+02 3.57491211e+02 3.81977203e+02
      4.21359467e+02 3.75400452e+02 4.84925446e+02 3.69191620e+02
      5.49470337e+02 3.62494263e+02 6.13580872e+02 3.56779846e+02
      6.78438782e+02 3.49555328e+02 3.06867157e+02 4.20822540e+02
      3.69288574e+02 4.13790375e+02 4.33062592e+02 4.07404663e+02
      4.96213226e+02 4.01113586e+02 5.60101196e+02 3.94827240e+02
      6.23234741e+02 3.89026825e+02 6.87469238e+02 3.82149628e+02
      3.19646729e+02 4.52004486e+02 3.81761505e+02 4.45818542e+02
      4.44674255e+02 4.39175354e+02 5.07120270e+02 4.32721405e+02
      5.70410522e+02 4.26769745e+02 6.32838196e+02 4.20189453e+02
      6.96676392e+02 4.13613495e+02 3.31519501e+02 4.82808655e+02
      3.93605255e+02 4.76749786e+02 4.55662384e+02 4.70188141e+02
      5.17510193e+02 4.63821075e+02 5.80531494e+02 4.57597900e+02
      6.42806396e+02 4.51435486e+02 7.05716431e+02 4.45144043e+02
      3.43830017e+02 5.13576233e+02 4.05296082e+02 5.07819489e+02
      4.66786774e+02 5.01107727e+02 5.28237183e+02 4.94833130e+02
      5.90605530e+02 4.88506226e+02 6.52136780e+02 4.82309753e+02
      7.14429016e+02 4.75791656e+02</_></_>
  <_><_>
      4.22907623e+02 1.57985138e+02 4.91686707e+02 1.57311661e+02
      5.60688293e+02 1.56931885e+02 6.29136597e+02 1.56955811e+02
      6.98402100e+02 1.56550476e+02 7.66716064e+02 1.57227432e+02
      8.35904541e+02 1.56348495e+02 4.22496277e+02 1.95004776e+02
      4.90208099e+02 1.94007294e+02 5.59069336e+02 1.93770386e+02
      6.27069763e+02 1.93529495e+02 6.95536804e+02 1.93399796e+02
      7.63003479e+02 1.93785873e+02 8.31246399e+02 1.93129486e+02
      4.22448120e+02 2.31117889e+02 4.89791718e+02 2.30762115e+02
      5.57717468e+02 2.30188614e+02 6.24722717e+02 2.29559341e+02
      6.92686340e+02 2.29715057e+02 7.59300659e+02 2.29188126e+02
      8.27110962e+02 2.28798477e+02 4.21647614e+02 2.66522217e+02
      4.88804535e+02 2.66281311e+02 5.55983704e+02 2.65539490e+02
      6.22250488e+02 2.65087677e+02 6.89785950e+02 2.64798126e+02
      7.56014465e+02 2.64527313e+02 8.23055908e+02 2.64223480e+02
      4.21669830e+02 3.01963928e+02 4.88130524e+02 3.01829254e+02
      5.54429688e+02 3.00924316e+02 6.20384521e+02 3.00493988e+02
      6.87154663e+02 2.99862457e+02 7.52571777e+02 2.99501648e+02
      8.18752502e+02 2.98829010e+02</_></_>
  <_><_>
      4.89602295e+02 4.86574005e+02 5.46814026e+02 4.67061951e+02
      6.04778442e+02 4.47377014e+02 6.62821472e+02 4.28167084e+02
      7.21870667e+02 4.08000458e+02 7.81058960e+02 3.88833374e+02
      8.41162109e+02 3.68062073e+02 5.22861755e+02 5.26506714e+02
      5.79828613e+02 5.06980774e+02 6.37860901e+02 4.87850739e+02
      6.95533936e+02 4.68623749e+02 7.54329895e+02 4.49330353e+02
      8.13032288e+02 4.29949310e+02 8.72854675e+02 4.09983582e+02
      5.56262146e+02 5.65691833e+02 6.12703003e+02 5.47125061e+02
      6.70354004e+02 5.27997192e+02 7.27754150e+02 5.08842316e+02
      7.86491699e+02 4.89953430e+02 8.44595764e+02 4.70518433e+02
      9.04387085e+02 4.50772614e+02 5.88400513e+02 6.04567993e+02
      6.45214905e+02 5.86262512e+02 7.02051270e+02 5.67514282e+02
      7.59425659e+02 5.48820374e+02 8.17844543e+02 5.29821777e+02
      8.76109070e+02 5.10813538e+02 9.35283447e+02 4.91505585e+02
      6.20655823e+02 6.43319397e+02 6.76882446e+02 6.25574951e+02
      7.33732910e+02 6.06747314e+02 7.90845154e+02 5.88246582e+02
      8.49016541e+02 5.69412415e+02 9.06685364e+02 5.50718933e+02
      9.65716125e+02 5.31416504e+02</_></_></features>
</opencv_storage>

I've seen online that there are several issues with saving vectors of vectors and opencv's FileStorage, but I don't really understand the storage part in ofxCv::Calibration. Why are vector elements wrapped with "[: ]" in Calibration::save? Is that a special trick for vectors of vectors?

Since it's my first time using the FileStorage of OpenCV, I don't know whether the file above seems correct (i.e. it's a bug with the reader) or not (then it's a bug with the writer).

Actually, it seems to be a bug with the writing part, because after replacing <_><_> with <_> and </_></_> with </_>, the loading part seems to work.

Ok, so I fixed it by replacing the line at line 125 with fs << imagePoints[i]; which is sufficient since OpenCV supports saving vectors of primitive values (points here).

Since it's been around for a while and load seems to have been fixed a few times, I wonder if it's a version-based bug? Does it really work for you?

Note: I used the circle-based calibration (I read it's more robust than checkerboard patterns).

great bug hunting! this seems to be a change since the previous version of the opencv yml parser.

i know it's a small change, but could you send a PR for it?

I also just bumped into that :/

Is there a reason why this issue still exists? Seems that the (open) pull request from @xionluhnis fixes that?!

Same issue here, why not accept that PR and get rid of the bug.

@m9dfukc can you help me fix this on my local installation? I tried to patch all files as described in 3179772 but I get errors compiling after that... am I missing something?

@alberto2000 I only applied lines 123 - 127 in Calibration.cpp

@m9dfukc I still get the same error after changing that piece of code. And what's also weird is that my calibration.cpp is slightly different from the one that commit is using anyway... for instance those lines are at 127 - 130 and not 123 - 127. I'm on the master branch of https://github.com/kylemcdonald/ofxCv ... are you using another branch?

Also, I'm using an .yml file, not .xml...

No, I'm on the stable branch as I'm still using OF 0.9.8 ... replacing line 128 width fs << imagePoints[i]; is not working for you? ... At least it shouldn't give you a compiler error!

@m9dfukc ok I was reading it wrong, obviously I had to regenerate the .yml file again after doing the fix, it seems to work now. Still I wonder why my Calibration.cpp looks slightly different than https://github.com/xionluhnis/ofxCv/blob/3179772704677b6059e9e3252e083afeace1b997/libs/ofxCv/src/Calibration.cpp

The PR was a while ago, there has been some changes in the code over the past year.

@xionluhnis is that why there's the conflicts or are they related to something different?

I think the main reason my PR has not been merged yet was that I tried to fix two different issues, one of which doesn't have a consensus about whether it actually is an issue.
Maybe I should just redo the pull request with only the fs << imagePoints[i]; line since that issue was deemed clearly defined and the solution was accepted.

That would make sense provided it will be accepted and pulled into the release version

I created a new PR that only does the aforementioned fix.

i believe this was fixed by #243