c++ - 如何裁剪不同矩阵中的一部分图像,但矩阵不会改变其在 openCV 中的大小。

标签 c++ image opencv matrix crop

我正在使用 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/

相关文章:

c++ - is_convertible is_assignable 有什么区别

c# - 在 WPF 中调整图像大小

python - 出不来这个洞: can't use pre-learnt model's output

opencv - 无法分割 RGB channel

c++ - 了解快速排序期间的递归

c++ - 使用 <random> 和函数生成随机双 vector

c++ - 在 Windows 中查找推荐的显示器分辨率

python - 使用 Python 的 PIL,如何增强图像的对比度/饱和度?

c++ - 从 RGB 值构建像素

c++ - openCV 到 dlib - 垫到 array2d