当前位置:   article > 正文

openCV实现图像分割_opencv 树叶 分割

opencv 树叶 分割

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 patch
void 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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/77485
推荐阅读
相关标签
  

闽ICP备14008679号