OpenCV实现图像切割功能

时间:2021-05-20

openCV实现将图像切成m*n块,供大家参考,具体内容如下

一、代码部分:

#include "stdafx.h"#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <iostream> #include <vector> #include<string>#include<sstream>using namespace std;using namespace cv;//Cut an image into m*n patchvoid Cut_img(Mat src_img, int m, int n, vector<Mat> ceil_img){ int t = m * n; int height = src_img.rows; int width = src_img.cols; int ceil_height = height / m; int ceil_width = width / n; Mat roi_img; //String concatenation ostringstream oss; string str, str1, str2; Point p1, p2; for (int i = 0; i<m; i++) { for (int j = 0; j<n; j++) { Rect rect(j*ceil_width, i*ceil_height, ceil_width, ceil_height); src_img(rect).copyTo(roi_img); ceil_img.push_back(roi_img); oss << i; str1 = oss.str(); oss.str(""); oss << j; str2 = oss.str(); oss.str(""); str = "roi_img_" + str1 + "_" + str2; imshow(str, roi_img); IplImage *ipl_roi_img=&IplImage(roi_img); //save processed img char tmp[100]="\0"; sprintf(tmp,"..\\post_img\\71253_%d_%d.jpg",i,j); cvSaveImage(tmp,ipl_roi_img); } }}int _tmain(int argc, _TCHAR* argv[]){ char *img_name_path="..\\image\\71253.jpg"; Mat img = imread(img_name_path,1); imshow("src_img", img); waitKey(0); int m = 2; int n = 2; vector<Mat> ceil_img ; Cut_img(img, m, n, ceil_img); cvWaitKey(0); return 0;}

二、程序运行结果:

(1)原图像:

(2)切割后图像:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章