How to change the size of an image and add a border in OpenCV using C++?

OpenCVC++Server Side ProgrammingProgramming

<p>In this topic, we will see another application of trackbar. Here, we will use track-bar to change the size of an image and add a border to the image and change the border&#39;s size using the track-bar.</p><p>Using the following program, we can change the size of an image, add a border, change the border&#39;s size and rotate the image. It is similar to the previous example.</p><p>The following program demonstrates how to add multiple sliders in the same track-bar.</p><h2>Example</h2><pre class="prettyprint notranslate">#include&lt;iostream&gt; #include&lt;opencv2/highgui/highgui.hpp&gt; #include&lt;opencv2/imgproc/imgproc.hpp&gt; using namespace std; using namespace cv; &nbsp; &nbsp;int Rotate = 180;//initializing angle// &nbsp; &nbsp;int Scale = 50;//initializing scale// &nbsp; &nbsp;int Border = 0;//initial Border// &nbsp; &nbsp;Mat before_Rotate;//declaring matrix for before rotation// &nbsp; &nbsp;int vertical = 0;//initial vertical value// &nbsp; &nbsp;int horizontal = 0;//initial horizontal value// &nbsp; &nbsp;void rotator(int, void*){ //function to rotate image// &nbsp; &nbsp;Mat Rotation = getRotationMatrix2D(Point(horizontal, vertical),(Rotate - 180), Scale / 50.0);//affine transformation matrix for 2D rotation// &nbsp; &nbsp;Mat Rotated;//matrix for rotated image &nbsp; &nbsp;warpAffine(before_Rotate, Rotated, Rotation, before_Rotate.size(), INTER_LINEAR, Border, Scalar());//applying affine transformation// &nbsp; &nbsp;imshow(&quot;RotatedImage&quot;, Rotated);//show rotated image// } int main(int argc,char**argv) { &nbsp; &nbsp;before_Rotate = imread(&quot;sky.jpg&quot;);//loading image in the matrix// &nbsp; &nbsp;vertical = before_Rotate.rows / 2;//getting midpoint of vertical pixels// &nbsp; &nbsp;horizontal = before_Rotate.cols / 2;//getting midpoints of horizontal pixels// &nbsp; &nbsp;namedWindow(&quot;BeforeRotate&quot;);//declaring window to show image before rotation// &nbsp; &nbsp;imshow(&quot;BeforeRotate&quot;, before_Rotate);//showing image before rotation// &nbsp; &nbsp;namedWindow(&quot;AfterRotate&quot;);//declaring window to show image after rotation// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;createTrackbar(&quot;Angle&quot;, &quot;AfterRotate&quot;, &amp;Rotate, 360, rotator);//creating trackbar for rotation// &nbsp; &nbsp;createTrackbar(&quot;Scale&quot;, &quot;AfterRotate&quot;, &amp;Scale, 100, rotator);//creating trackbar to change size// &nbsp; &nbsp;createTrackbar(&quot;Border Mode&quot;, &quot;After Rotate&quot;, &amp;Border, 5, rotator);//creating trackbar to add border// &nbsp; &nbsp;int cbfunction = 0;//initiate value of rotator function&#39;s argument// &nbsp; &nbsp;rotator(cbfunction, &amp;cbfunction);//call back rotator function// &nbsp; &nbsp;waitKey(0);//wait till keystroke// &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><p><img src="" class="fr-fic fr-dib" width="600" height="196"></p>
Updated on 10-Mar-2021 08:46:49