8、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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值