snipt

Ctrl+h for KB shortcuts
Notice: Snipt is moving away from free accounts on May 1st, 2014. Read more about the transition here.
#18331

C++

crop an image with OpenCV

// source image (at least 128x128)
IplImage *source = cvLoadImage("lena.jpg", 1);
 
// sets the ROI at the middle of the image
int roiSize = 128;
cvSetImageROI(source, cvRect((source->width - roiSize)/2, (source->height - roiSize)/2, roiSize, roiSize));

// cropped image
IplImage *cropSource = cvCreateImage(cvGetSize(source), source->depth, source->nChannels);
 
// copy
cvCopy(source, cropSource, NULL);
 
// always reset the ROI
cvResetImageROI(source);
https://snipt.net/embed/78c867c85f9523a87cb3197707cd6e12/
https://snipt.net/raw/78c867c85f9523a87cb3197707cd6e12/
78c867c85f9523a87cb3197707cd6e12
cpp
C++
15
2014-04-24T14:04:51
True
False
/api/public/snipt/18331/
crop-an-image-with-opencv
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a> <a href="#L-13">13</a> <a href="#L-14">14</a> <a href="#L-15">15</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="c1">// source image (at least 128x128)</span> </span><span id="L-2"><a name="L-2"></a><span class="n">IplImage</span> <span class="o">*</span><span class="n">source</span> <span class="o">=</span> <span class="n">cvLoadImage</span><span class="p">(</span><span class="s">&quot;lena.jpg&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="c1">// sets the ROI at the middle of the image</span> </span><span id="L-5"><a name="L-5"></a><span class="kt">int</span> <span class="n">roiSize</span> <span class="o">=</span> <span class="mi">128</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a><span class="n">cvSetImageROI</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">cvRect</span><span class="p">((</span><span class="n">source</span><span class="o">-&gt;</span><span class="n">width</span> <span class="o">-</span> <span class="n">roiSize</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="n">source</span><span class="o">-&gt;</span><span class="n">height</span> <span class="o">-</span> <span class="n">roiSize</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="n">roiSize</span><span class="p">,</span> <span class="n">roiSize</span><span class="p">));</span> </span><span id="L-7"><a name="L-7"></a> </span><span id="L-8"><a name="L-8"></a><span class="c1">// cropped image</span> </span><span id="L-9"><a name="L-9"></a><span class="n">IplImage</span> <span class="o">*</span><span class="n">cropSource</span> <span class="o">=</span> <span class="n">cvCreateImage</span><span class="p">(</span><span class="n">cvGetSize</span><span class="p">(</span><span class="n">source</span><span class="p">),</span> <span class="n">source</span><span class="o">-&gt;</span><span class="n">depth</span><span class="p">,</span> <span class="n">source</span><span class="o">-&gt;</span><span class="n">nChannels</span><span class="p">);</span> </span><span id="L-10"><a name="L-10"></a> </span><span id="L-11"><a name="L-11"></a><span class="c1">// copy</span> </span><span id="L-12"><a name="L-12"></a><span class="n">cvCopy</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">cropSource</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span> </span><span id="L-13"><a name="L-13"></a> </span><span id="L-14"><a name="L-14"></a><span class="c1">// always reset the ROI</span> </span><span id="L-15"><a name="L-15"></a><span class="n">cvResetImageROI</span><span class="p">(</span><span class="n">source</span><span class="p">);</span> </span></pre></div> </td></tr></table>
c++, opencv