EADST

C++: Copy ROI and Add Padding

Recently, I did some data augmentation work with C++ OpenCV. Here is one example.

// load image
string test_path = "D:\\test.jpg";
cv::Mat img = cv::imread(test_path, CV_8UC3);
// crop image and get ROI
cv::Rect r(5, 5, 25, 25);
cv::Mat roi = img(r).clone();
cv::imwrite("test_cropped.jpg", roi);
// set padding background
int padding = 20;
cv::Mat img_pad = cv::Mat::zeros(roi.rows + padding * 2, roi.cols + padding * 2, CV_8UC3);
img_pad.setTo(cv::Scalar(255, 255, 255));
// pad image
cv::Rect roi_rect = cv::Rect(padding, padding, roi.cols, roi.rows);
roi.copyTo(img_pad(roi_rect));
cv::imwrite("test_padded.jpg", img_pad);
相关标签
C++
About Me
XD
Goals determine what you are going to be.
Category
标签云
站点统计

本站现有博文266篇,共被浏览440706

本站已经建立2019天!

热门文章
文章归档
回到顶部