<p dir="auto">I found an issue while using solvePnPRansac with useExtrensicGuess=True with other default parameters. With default parameters solvePnPRansac uses EPNP algorithm during RANSAC iterations and refinement is done using ITERATIVE algorithm.</p>
<p dir="auto">When useExtrensicGuess is set to True I found that rvec and tvec obtained from EPNP is not set to the best model obtained by RANSAC. It picks the model from last iteration for refinement which is not guaranteed to be good. Because of this issue final model obtained is bad. Has anyone faced this issue?</p>
<h5 dir="auto">System information (version)</h5>

<ul dir="auto">
<li>OpenCV =&gt; master</li>
</ul>
<h5 dir="auto">Detailed description</h5>
<p dir="auto">See the code<br>
</p><div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/opencv/opencv/blob/ee1e1ce377aa61ddea47a6c2114f99951153bb4f/modules/calib3d/include/opencv2/calib3d.hpp#L692-L695">opencv/modules/calib3d/include/opencv2/calib3d.hpp</a>
    </p>
    <p class="mb-0 color-fg-muted">
        Lines 692 to 695
      in
      <a data-pjax="true" class="commit-tease-sha" href="/opencv/opencv/commit/ee1e1ce377aa61ddea47a6c2114f99951153bb4f">ee1e1ce</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L692" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="692"></td>
          <td id="LC692" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> CV_EXPORTS_W <span class="pl-k">bool</span> <span class="pl-en">solvePnP</span>( InputArray objectPoints, InputArray imagePoints, </td>
        </tr>

        <tr class="border-0">
          <td id="L693" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="693"></td>
          <td id="LC693" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">                             InputArray cameraMatrix, InputArray distCoeffs, </td>
        </tr>

        <tr class="border-0">
          <td id="L694" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="694"></td>
          <td id="LC694" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">                             OutputArray rvec, OutputArray tvec, </td>
        </tr>

        <tr class="border-0">
          <td id="L695" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="695"></td>
          <td id="LC695" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">                             <span class="pl-k">bool</span> useExtrinsicGuess = <span class="pl-c1">false</span>, <span class="pl-k">int</span> flags = SOLVEPNP_ITERATIVE ); </td>
        </tr>
    </tbody></table>
  </div>
</div>
<p></p>
<p dir="auto"><code class="notranslate">rvec</code> and <code class="notranslate">tvec</code> are of type <code class="notranslate">OutputArray</code> indicating that they are used only for ouput.</p>
<p dir="auto">But the code<br>
</p><div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/opencv/opencv/blob/ee1e1ce377aa61ddea47a6c2114f99951153bb4f/modules/calib3d/src/solvepnp.cpp#L58">opencv/modules/calib3d/src/solvepnp.cpp</a>
    </p>
    <p class="mb-0 color-fg-muted">
         Line 58
      in
      <a data-pjax="true" class="commit-tease-sha" href="/opencv/opencv/commit/ee1e1ce377aa61ddea47a6c2114f99951153bb4f">ee1e1ce</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L58" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="58"></td>
          <td id="LC58" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> OutputArray _rvec, OutputArray _tvec, <span class="pl-k">bool</span> useExtrinsicGuess, <span class="pl-k">int</span> flags ) </td>
        </tr>
    </tbody></table>
  </div>
</div>
<br>
<div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/opencv/opencv/blob/ee1e1ce377aa61ddea47a6c2114f99951153bb4f/modules/calib3d/src/solvepnp.cpp#L71-L79">opencv/modules/calib3d/src/solvepnp.cpp</a>
    </p>
    <p class="mb-0 color-fg-muted">
        Lines 71 to 79
      in
      <a data-pjax="true" class="commit-tease-sha" href="/opencv/opencv/commit/ee1e1ce377aa61ddea47a6c2114f99951153bb4f">ee1e1ce</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L71" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="71"></td>
          <td id="LC71" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-k">if</span>( useExtrinsicGuess ) </td>
        </tr>

        <tr class="border-0">
          <td id="L72" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="72"></td>
          <td id="LC72" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> { </td>
        </tr>

        <tr class="border-0">
          <td id="L73" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="73"></td>
          <td id="LC73" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-k">int</span> rtype = _rvec.<span class="pl-c1">type</span>(), ttype = _tvec.<span class="pl-c1">type</span>(); </td>
        </tr>

        <tr class="border-0">
          <td id="L74" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="74"></td>
          <td id="LC74" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-c1">Size</span> rsize = _rvec.<span class="pl-c1">size</span>(), tsize = _tvec.<span class="pl-c1">size</span>(); </td>
        </tr>

        <tr class="border-0">
          <td id="L75" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="75"></td>
          <td id="LC75" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-c1">CV_Assert</span>( (rtype == CV_32F || rtype == CV_64F) &amp;&amp; </td>
        </tr>

        <tr class="border-0">
          <td id="L76" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="76"></td>
          <td id="LC76" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">                (ttype == CV_32F || ttype == CV_64F) ); </td>
        </tr>

        <tr class="border-0">
          <td id="L77" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="77"></td>
          <td id="LC77" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-c1">CV_Assert</span>( (rsize == <span class="pl-c1">Size</span>(<span class="pl-c1">1</span>, <span class="pl-c1">3</span>) || rsize == <span class="pl-c1">Size</span>(<span class="pl-c1">3</span>, <span class="pl-c1">1</span>)) &amp;&amp; </td>
        </tr>

        <tr class="border-0">
          <td id="L78" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="78"></td>
          <td id="LC78" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">                (tsize == <span class="pl-c1">Size</span>(<span class="pl-c1">1</span>, <span class="pl-c1">3</span>) || tsize == <span class="pl-c1">Size</span>(<span class="pl-c1">3</span>, <span class="pl-c1">1</span>)) ); </td>
        </tr>

        <tr class="border-0">
          <td id="L79" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="79"></td>
          <td id="LC79" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> } </td>
        </tr>
    </tbody></table>
  </div>
</div>
<br>
treats them as input when <code class="notranslate">useExtrinsicGuess==true</code>.<p></p>

<h5 dir="auto">Suggestions</h5>
<p dir="auto">The prototype should be decalared as <code class="notranslate">InputOutputArray</code>.</p>