8、opencv图像分割合并
opencv
·
opencv图像分割合并
分割合并
#include <opencv2\opencv.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
//#define WIDTH 800
//#define HEIGHT 800
//#define M 5
//#define N 5
//#define SUB_WIDTH WIDTH/ M
//#define SUB_HEIGHT HEIGHT/ N
vector<Mat> splitImg(Mat image, int row, int col)
{
int width = image.cols;
int height = image.rows;
int sub_width = width / col; //M 列
int sub_height = height / row; //N 行
//图像分块
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++)
{
////可添加处理程序
//imshow(to_string(name[t]), ceil_img[t]);
Rect rect = Rect(0, 0, ceil_img[t].cols, ceil_img[t].rows);
//参数说明:1矩形左上角点的横坐标 2矩形左上角点的纵坐标 3矩形的长 4矩形的宽
Scalar color = Scalar(0, 255, 0);
rectangle(ceil_img[t], rect, color, 2, LINE_8); //2表示线的宽度
//可添加处理程序
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);
/*
//图像分块
vector<Mat> ceil_img;
vector<int> name;
for (int t = 0; t < M * N; t++) name.push_back(t);
Mat image_cut, roi_img;
for (int j = 0; j < N; j++)
{
for (int i = 0; i < M; i++)
{
Rect rect(i * SUB_WIDTH, j * SUB_HEIGHT, SUB_WIDTH, SUB_HEIGHT);
image_cut = Mat(src, rect);
roi_img = image_cut.clone();
ceil_img.push_back(roi_img);
}
}
//分块处理
for (int t = 0; t < M * N; t++)
{
////可添加处理程序
//imshow(to_string(name[t]), ceil_img[t]);
Rect rect = Rect(0, 0, SUB_WIDTH, SUB_HEIGHT);
//参数说明:1矩形左上角点的横坐标 2矩形左上角点的纵坐标 3矩形的长 4矩形的宽
Scalar color = Scalar(0, 255, 0);
rectangle(ceil_img[t], rect, color, 2, LINE_8); //2表示线的宽度
//可添加处理程序
imshow(to_string(name[t]), ceil_img[t]);
}
//图像合并
int t = 0;
Mat MergeImage(Size(WIDTH, HEIGHT), CV_8UC3);
for (int j = 0; j < N; j++)
{
for (int i = 0; i < M; i++)
{
Rect ROI(i * SUB_WIDTH, j * SUB_HEIGHT, SUB_WIDTH, SUB_HEIGHT);
ceil_img[t].copyTo(MergeImage(ROI));
t++;
}
}
namedWindow("merge", WINDOW_NORMAL);
imshow("merge", MergeImage);
waitKey(0);
*/
}
更多推荐
所有评论(0)