[英]How to crop a part of image in the different matrix, but the matrix doesn't change its size in openCV.
我正在使用openCV 2.4 ver进行一个项目。 C ++我想裁剪一部分图像并将其保存在不同的Matrix中。 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
确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.