Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV: set image borders, without padding

Tags:

c++

opencv

how can I set an image's border of a given size to black, WITHOUT padding, i.e., I just want to set the image borders to black (zero), and the output will be an image with the same size as source.

like image 462
manatttta Avatar asked Dec 15 '22 10:12

manatttta


2 Answers

You can simply draw a black rectangle on your image:

cv::Mat image;
cv::Rect border(cv::Point(0, 0), image.size());
cv::Scalar color(0, 0, 0);
int thickness = 1;

cv::rectangle(image, border, color, thickness);
like image 151
prazuber Avatar answered Dec 29 '22 00:12

prazuber


You can use copyMakeBorder with the BORDER_ISOLATED flag.

cv::Mat image = cv::imread("lena.png");
cv::Mat output;

const int border = 10;
const int borderType = cv::BORDER_CONSTANT | cv::BORDER_ISOLATED;
const cv::Scalar value(0, 0, 0);
const cv::Rect roi(border, border, image.cols-2*border, image.rows-2*border);
cv::copyMakeBorder(image(roi), output, border, border, border, border, borderType, value);

cv::imshow("input", image);
cv::imshow("output", output);
cv::waitKey(0);
like image 33
boaz001 Avatar answered Dec 29 '22 01:12

boaz001