I have an image and I want to perform OpenCV boxFilter
on a ROI
of this image.
image = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);
cv::Rect roi( 10, 10, 64, 64);
cv::Mat output;
cv::boxFilter(image(roi),output,-1,cv::Size(scale_size,scale_size));
I want to know what happen if the kernel is out of the ROI
? Use the pixels out of the ROI
but still within the image to do the filtering or just use the value specified by the BorderType
?
Actually I want to use the former to do the filtering.
5
I think the accepted answer is wrong.
In the past I tested it with Sobel filter and I’m pretty sure I also found the original documentation or a semi-official answer from opencv that filters in subimages use the real border pixels from the original image.
EDIT: Found it (not a bug but a feature):
https://github.com/opencv/opencv/issues/5263
I now tested this for box-filter with this code:
int main()
{
try
{
cv::Mat image = cv::imread("Lenna.png");
cv::Rect roi(10, 10, 64, 64);
// box filter on roi which is a subimage of the full image
cv::Mat output_roi;
cv::boxFilter(image(roi), output_roi, -1, cv::Size(5, 5));
// box filter on a deep copy of the roi/subimage region
cv::Mat copy = image(roi).clone();
cv::Mat output_copy;
cv::boxFilter(copy, output_copy, -1, cv::Size(5, 5));
// highlight the differences between both results
cv::Mat diff;
cv::absdiff(output_roi, output_copy, diff);
cv::cvtColor(diff, diff, cv::COLOR_BGR2GRAY);
std::cout << "non-zero diff between clone and roi: " << cv::countNonZero(diff) << std::endl;
// now compute on full image and extract roi result:
cv::Mat output_full;
cv::boxFilter(image, output_full, -1, cv::Size(5, 5));
cv::Mat diff_full;
cv::absdiff(output_roi, output_full(roi), diff_full);
cv::cvtColor(diff_full, diff_full, cv::COLOR_BGR2GRAY);
std::cout << "non-zero diff between full(roi) and roi: " << cv::countNonZero(diff_full) << std::endl;
cv::imshow("diff>0", diff > 0);
cv::imshow("diff_full>0", diff_full > 0);
cv::waitKey(0);
}
catch(cv::Exception &e)
{
std::cout << e.what() << std::endl;
}
return 0;
}
Result is a diff between the clone and the roi but no diff between the the roi and the fullimage-result (in the roi region).
non-zero diff between clone and roi: 297
non-zero diff between full(roi) and roi: 0
here are the result images:
diff>0
diff_full>0
result images (hard to see the difference 😉 ):
roi:
copy:
full:
3
… the answer is actually incorrect (will be deleted when it is un-accepted) …
8