裁剪图像,然后添加边框

Crop image and then add border

本文关键字:添加 边框 然后 图像 裁剪      更新时间:2023-10-16
Mat img = imread(input);
// Crop a part out of the image
img = img(Rect(x, y, width, height));
// Add a white border around the cropped image
int border = 100;
copyMakeBorder(img, img, border, border, border, border, BORDER_CONSTANT, Scalar(0, 255, 255));

我有一个问题..我需要为图像添加边框。

但首先我必须裁剪一些内容。

问题是之后添加边框时,我刚刚裁剪的内容又回来了。

是否可以在添加边框之前以某种方式在裁剪后"提交"更改?

您应该使用新Mat并克隆 ROI。

#include "opencv2/highgui.hpp"
using namespace cv;
int main(int argc, char* argv[])
{
    Mat img = imread(argv[1]);
    // Crop a part out of the image
    Mat cropped = img(Rect(10, 10, 100, 100)).clone();
    // Add a white border around the cropped image
    int border = 100;
    copyMakeBorder(cropped, cropped, border, border, border, border, BORDER_CONSTANT, Scalar(0, 255, 255));
    imshow("cropped", cropped);
    waitKey();
    return 0;
}