Ask Your Question
1

move and expand image

asked 2016-07-13 18:16:17 -0500

HiHello gravatar image

updated 2018-01-20 19:24:44 -0500

image description -> image description

How can i make the image like this? I am thinking about the flow like this.

First, make a new Mat type data 256*256.

Second, fill the image with the average value of small image.

Third, copy the small image in the middle of the new image.

Is that rignt? and also how can i copy the small image in the middle of the new image? T.T

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-07-13 19:50:26 -0500

You can use the function copyMakeBorder

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "iostream"

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    Mat src = imread(argv[1]);
    if (src.empty())
    {
        cout << endl
             << "ERROR! Unable to read the image" << endl
             << "Press a key to terminate";
        cin.get();
        return 0;
    }

    imshow("Source image", src);

    Mat dst;

    Size dst_dims = Size(256,256); 

    int top = ( dst_dims.height - src.rows ) / 2;
    int bottom = ( ( dst_dims.height + 1 ) - src.rows ) / 2;
    int left = ( dst_dims.width - src.cols ) / 2;
    int right = ( ( dst_dims.width + 1 ) - src.cols ) / 2;

    copyMakeBorder(src, dst, top, bottom, left, right, BORDER_CONSTANT, Scalar(127,127,127));

    imshow("New image", dst);
    waitKey();

    return 0;
}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-07-13 18:16:17 -0500

Seen: 303 times

Last updated: Jul 13 '16