我正在使用 openCV 2.4 ver 做一个项目。 C++ 我想裁剪一部分图像并将其保存在不同的矩阵中。 cropped_image 不是每次循环都获取一个新的裁剪图像,而是保留以前的图像,并继续在前一个图像的旁边构建。我不确定我做错了什么..
当 n = 64 和 m = 240 时,这个循环也会停止。我也不明白为什么..
谁能帮帮我?
openCV 2.4v C++
using namespace cv;
using namespace std;
original_image = imread("image.jpg",1);
int n, m, cols_ss, rows_ss;
int cols = 640;
int rows = 480;
cols_ss = 64 // arbitrary number;
rows_ss = 48 // arbitrary number;
Mat cropped_image;
for (n = 0; n < cols - cols_ss; n = n + cols_ss) {
for (m = 0; m < rows - rows_ss; m = m + rows_ss) {
// initialize cropped_image as zeros.
Mat cropped_image(cols_ss, rows_ss, CV_8UC1, Scalar::all(0));
// Crop a small part of an original_image to cropped_image.
cropped_image = original_image(Rect(n, m, n + cols_ss, m + rows_ss));
}
}
最佳答案
根据 OpenCV 文档,Rect
的构造函数是 Rect(x, y, width, height)
。你确定你的意思不是 cropped_image = original_image(Rect(n, m, cols_ss, rows_ss));
?
ROI 函数通过指针将 1:1 映射到图像中的区域。确保 cropped_image
符合预期的另一种方法是:
Mat cropped_image(original_image, Rect(n, m, cols_ss, rows_ss));
这将确保 cropped_image
的大小由 ROI 的 Rect
决定。
关于c++ - 如何裁剪不同矩阵中的一部分图像,但矩阵不会改变其在 openCV 中的大小。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37982624/