分割合并
#include <opencv2\opencv.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
vector<Mat> splitImg(Mat image, int row, int col)
{
int width = image.cols;
int height = image.rows;
int sub_width = width / col;
int sub_height = height / row;
vector<Mat> ceil_img;
Mat image_cut, roi_img;
for (int j = 0; j < row; j++)
{
for (int i = 0; i <col ; i++)
{
Rect rect(i * sub_width, j * sub_height, sub_width, sub_height);
image_cut = Mat(image, rect);
roi_img = image_cut.clone();
ceil_img.push_back(roi_img);
}
}
return ceil_img;
}
Mat MergeImg(Mat image,vector<Mat> ceil_img, int row,int col)
{
int width = image.cols;
int height = image.rows;
int sub_width = width / col;
int sub_height = height / row;
int t = 0;
Mat MergeImage(image.size(), image.type());
for (int j = 0; j < row; j++)
{
for (int i = 0; i < col; i++)
{
Rect ROI(i * sub_width, j * sub_height, sub_width, sub_height);
ceil_img[t].copyTo(MergeImage(ROI));
t++;
}
}
return MergeImage;
}
int main()
{
Mat src;
src = imread("../Image.bmp");
if (!src.data) { printf("erro"); return false; }
vector<Mat> ceil_img=splitImg(src, 2, 3);
int n = 2, m = 3;
vector<int> name;
for (int t = 0; t < m * n; t++) name.push_back(t);
for (int t = 0; t < m * n; t++)
{
Rect rect = Rect(0, 0, ceil_img[t].cols, ceil_img[t].rows);
Scalar color = Scalar(0, 255, 0);
rectangle(ceil_img[t], rect, color, 2, LINE_8);
imshow(to_string(name[t]), ceil_img[t]);
}
Mat MergeImage =MergeImg(src, ceil_img, 2, 3);
namedWindow("merge", WINDOW_NORMAL);
imshow("merge", MergeImage);
waitKey(0);
}