OpenCV 图像分块 分块图像合并

将整个图像划分为多个子图像,对每个图像进行局部计算

#define WIDTH 320
#define HEIGHT 240
#define M 2
#define N 2
#define SUB_WIDTH WIDTH/ M
#define SUB_HEIGHT HEIGHT/ N

//canny多窗口自适应阈值
int main()
{
	Mat Src;
	Src = imread("0.bmp",0);
	if (!Src.data) { printf("erro"); return false; }
	
	//图像分块
	vector<Mat> ceil_img;
	vector<int> name;
	for (int t = 0; t < M * N; t++) name.push_back(t);
	Mat image_cut, roi_img;
	for(int j = 0; j < N; j ++)
	{
		for (int i = 0; i < M; i++)
		{
			Rect rect(i * SUB_WIDTH, j * SUB_HEIGHT, SUB_WIDTH, SUB_HEIGHT);
			image_cut = Mat(Src, rect);
			roi_img = image_cut.clone();
			ceil_img.push_back(roi_img);
		}
	}

	//分块处理
	for (int t = 0; t < M * N; t++)
	{
		imshow(to_string(name[t]), ceil_img[t]);
	}

	//图像合并
	int t = 0;
	Mat MergeImage(Size(WIDTH, HEIGHT), CV_LOAD_IMAGE_GRAYSCALE);
	for (int j = 0; j < N; j++)
	{
		for (int i = 0; i < M; i++)
		{
			Rect ROI(i * SUB_WIDTH, j * SUB_HEIGHT, SUB_WIDTH, SUB_HEIGHT);
			ceil_img[t].copyTo(MergeImage(ROI));
			t++;
		}
	}
	imshow("merge", MergeImage);

	waitKey(0);

代码参考:
连接:
opencv将一副图像分割为多幅图像并存储
opencv把一个图片分块后再组成到一起的方法
opencv创建同样尺寸与类型的图片
2018/10/31 by bmi

你可能感兴趣的:(OpenCv)