以下是C++代码示例,将图片分成指定块并添加块与块的重叠部分。该代码使用OpenCV库进行图像处理。
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
// 读取图片
Mat image = imread("test.jpg");
// 定义块的大小和重叠部分大小
int block_width = 100;
int block_height = 100;
int overlap_size = 20;
// 获取原始图像的宽度和高度
int width = image.cols;
int height = image.rows;
// 计算分块数量
int block_count_width = width / (block_width - overlap_size) + 1;
int block_count_height = height / (block_height - overlap_size) + 1;
// 分块
for (int i = 0; i < block_count_height; i++)
{
for (int j = 0; j < block_count_width; j++)
{
// 计算当前块的左上角坐标
int x = j * (block_width - overlap_size);
int y = i * (block_height - overlap_size);
// 计算当前块的右下角坐标
int x_end = min(x + block_width, width);
int y_end = min(y + block_height, height);
// 切割当前块
Rect rect(x, y, x_end - x, y_end - y);
Mat block = image(rect);
// 显示当前块
imshow("Block", block);
waitKey(0);
}
}
return 0;
}
注:在上述代码中,读入了一张名为“test.jpg”的图片,并将其分成了100x100的块。其中,添加了一个overlap_size变量,用于指定块与块之间的重叠部分大小。然后,使用两个嵌套的循环来遍历所有的块,并使用OpenCV的Rect类来定义每个块的位置和大小。