openCV实现图像切割

桃扇骨 2022-06-12 04:51 274阅读 0赞

openCV实现将图像切成m*n块:
一、代码部分:

  1. #include "stdafx.h"
  2. #include <opencv2/core/core.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <iostream>
  5. #include <vector>
  6. #include<string>
  7. #include<sstream>
  8. using namespace std;
  9. using namespace cv;
  10. //Cut an image into m*n patch
  11. void Cut_img(Mat src_img, int m, int n, vector<Mat> ceil_img)
  12. {
  13. int t = m * n;
  14. int height = src_img.rows;
  15. int width = src_img.cols;
  16. int ceil_height = height / m;
  17. int ceil_width = width / n;
  18. Mat roi_img;
  19. //String concatenation
  20. ostringstream oss;
  21. string str, str1, str2;
  22. Point p1, p2;
  23. for (int i = 0; i<m; i++)
  24. {
  25. for (int j = 0; j<n; j++)
  26. {
  27. Rect rect(j*ceil_width, i*ceil_height, ceil_width, ceil_height);
  28. src_img(rect).copyTo(roi_img);
  29. ceil_img.push_back(roi_img);
  30. oss << i;
  31. str1 = oss.str();
  32. oss.str("");
  33. oss << j;
  34. str2 = oss.str();
  35. oss.str("");
  36. str = "roi_img_" + str1 + "_" + str2;
  37. imshow(str, roi_img);
  38. IplImage *ipl_roi_img=&IplImage(roi_img);
  39. //save processed img
  40. char tmp[100]="\0";
  41. sprintf(tmp,"..\\post_img\\71253_%d_%d.jpg",i,j);
  42. cvSaveImage(tmp,ipl_roi_img);
  43. }
  44. }
  45. }
  46. int _tmain(int argc, _TCHAR* argv[])
  47. {
  48. char *img_name_path="..\\image\\71253.jpg";
  49. Mat img = imread(img_name_path,1);
  50. imshow("src_img", img);
  51. waitKey(0);
  52. int m = 2;
  53. int n = 2;
  54. vector<Mat> ceil_img ;
  55. Cut_img(img, m, n, ceil_img);
  56. cvWaitKey(0);
  57. return 0;
  58. }

二、程序运行结果:
(1)原图像:
这里写图片描述
(2)切割后图像:
这里写图片描述

发表评论

表情:
评论列表 (有 0 条评论,274人围观)

还没有评论,来说两句吧...

相关阅读

    相关 opencv实现图像细化效果

    在图像处理中,有时候我们会想要提取图像的骨架,这是就需要对图像进行细化,opencv中没有直接进行细化的算法,网上大部分的细化算法都是基于以前IplImage结构的,对于想要使

    相关 openCV实现图像边缘检测

    最近自己在做一个有关图像处理的小项目,涉及到图像的边缘检测、直线检测、轮廓检测以及角点检测等,本文首先介绍图像的边缘检测,使用的是Canny边缘检测算法,具体代码以及检测效果如