Merge two Mat images into one

前端 未结 3 1132
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 23:15

I have a problem. I have an image. Then I have to split the image into two equal parts. I made this like that (the code is compiled, everything is good):

Mat         


        
相关标签:
3条回答
  • 2020-12-31 23:29

    Seems that cv::Mat::push_back is exactly what are you looking for:

    C++: void Mat::push_back(const Mat& m) : Adds elements to the bottom of the matrix.

    Parameters:    
        m – Added line(s).
    

    The methods add one or more elements to the bottom of the matrix. When elem is Mat , its type and the number of columns must be the same as in the container matrix.

    Optionally, you could create new cv::Mat of proper size and place image parts directly into it:

    Mat image_temp1 = image(Rect(0, 0, image.cols, image.rows/2)).clone();
    Mat image_temp2 = image(Rect(0, image.rows/2, image.cols, image.rows/2)).clone();
    ...
    cv::Mat result(image.rows, image.cols);
    image_temp1.copyTo(result(Rect(0, 0, image.cols, image.rows/2)));
    image_temp2.copyTo(result(Rect(0, image.rows/2, image.cols, image.rows/2));
    
    0 讨论(0)
  • 2020-12-31 23:36

    There is several way to do this, but the best way I found is to use cv::hconcat(mat1, mat2, dst) for horizontal merge orcv::vconcat(mat1, mat2, dst) for vertical.

    Don't forget to take care of empty matrix merge case !

    0 讨论(0)
  • 2020-12-31 23:46

    How about this:

    Mat newImage = image.clone();
    Mat image_temp1 = newImage(Rect(0, 0, image.cols, image.rows/2));
    Mat image_temp2 = newImage(Rect(0, image.rows/2, image.cols, image.rows/2));
    

    By not using clone() to create the temp images, you're implicitly modifying newImage when you modify the temp images without the need to merge them again. After changing image_temp1 and image_temp2, newImage will be exactly the same as if you had split, modified, and then merged the subimages.

    0 讨论(0)
提交回复
热议问题