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.
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With