将一幅图像分割成M*N个图像并保存

#include <opencv2\opencv.hpp>
#include <iostream>  
#include <vector>  
using namespace std;
using namespace cv;

//可单独封装成一个函数调用
void main()

{    
	//设置分割后图像存储路径
	string outpath = "D:\\aopencv\\sattest\\sattest\\photoes\\";//需要预先建立文件夹,可自己添加代码实现自动建立
	Mat img = imread("0021.jpg"); //将图像放在项目工程里,使用imread函数读取

	int t = 0;
	int m = img.cols / 1024;    //m*n是切割后子图像的个数
	int n = img.rows / 1024;

	vector<Mat> ceil_img;  //迭代器ceil_img存储子图像
	vector<int> name;     //迭代器name存储子图像的名字,从0到m*n-1
	for (t; t < m*n; t++)//得到图像名字存取到vector
	{
		name.push_back(t);
	}
	
	Mat image_cut, roi_img, tim_img;
	for (int j = 0; j <n; j++)
	{
		for (int i = 0; i < m; i++)
		{
			Rect rect(i * 1024, j * 1024, 1024, 1024);//新建的图像大小 1024*1024。如果改动大小,四个1024都要对应改变
			image_cut = Mat(img, rect);
			roi_img = image_cut.clone();
			ceil_img.push_back(roi_img);
		}
	}
	for (int t = 0; t < m*n; t++)
		
	{
		//imwrite(outpath + to_string(long long((name[t]))) + ".png", ceil_img[t]);
		imwrite(outpath + to_string(long long((name[t]))) + ".png", ceil_img[t]);

	}
}
发布了35 篇原创文章 · 获赞 37 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42244181/article/details/104389364