opencv将一副图像分割为多幅图像并存储

本文介绍了一种使用OpenCV库进行图像分割的方法,将原始图像分割为64*64大小的子图像,并详细展示了实现这一功能的C++代码。通过调整代码中的参数,可以灵活改变子图像的尺寸。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将图像放在项目工程里,本程序将图像分割成64*64大小的子图像。先看结果图:

当然,可以用能整除图片的宽和高的其他数字代替代码中所有64,来改变子图大小。

    #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;    
  //出现to_string重载时的模板!
  #include <string>
  #include <type_traits>
  namespace std
 { 
 template<typename T>
  typename enable_if<is_convertible<T, _Longlong>::value, string>::type     to_string(T rhs)
 {
    return to_string(static_cast<_Longlong>(rhs));
 } 
}  
    void main()
    {       //设置分割后图像存储路径
    	string outpath = "E:\\result\\";
    	Mat img = imread("girl_1088x768.jpg"); //将图像放在项目工程里,使用imread函数读取
    	int t = 0;
    	int m = img.cols / 64;    //m*n是切割后子图像的个数
    	int n = img.rows /64;    
    	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 * 64, j * 64, 64, 64);
    			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(name[t])+".png", ceil_img[t]);
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zqx951102

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值