将一幅图像分割成很多个小图像并保存


#include <opencv2/core/core.hpp>    

#include "opencv2/opencv.hpp"

#include <opencv2/highgui/highgui.hpp>    

#include <iostream>  

#include <opencv2/imgproc/imgproc.hpp>  

#include <vector>  

using namespace std;

using namespace cv;



void main()

{       //设置分割后图像存储路径

	string outpath = "F:\\data\\";

	Mat img = imread("digits.png"); //将图像放在项目工程里,使用imread函数读取

	int t = 0;

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

	int n = img.rows /20;    

	Vector<Mat> ceil_img;  //迭代器ceil_img存储子图像

	Vector<int> name;     //迭代器name存储子图像的名字,从0到m*n-1

	for (t; t < m*n; t++)

		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 * 20, j * 20, 20, 20);

			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]);

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值