简体   繁体   中英

OpenCV filter2D inside non rectangular ROI

I have an image in which only a small non-rectangular portion is useful. I have a binary mask indicating the useful ROI.

How can I apply cv::filter2D in OpenCV to that ROI only, defined by the binary mask?


Edit

My pixels outside the ROI have a value of 0. The other have float values of around 300-500, so the problem with filter2D in the borders of the ROI have high value transitions.

It would also be acceptable to just set the pixel values outside the ROI as the nearest pixel inside the ROI, something similar to cv::BORDER_REPLICATE

May be like this. I think there is no problem near border mask

#include "opencv2/opencv.hpp"
#include <iostream>

using namespace cv;
using namespace std;


int main(int argc, char* argv[])
{
Mat m = imread("f:/lib/opencv/samples/data/lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);

Mat mask=Mat::zeros(m.size(), CV_8UC1),maskBlur,mc;
// mask is a disk
circle(mask, Point(200, 200), 100, Scalar(255),-1);

Mat negMask;
// neg mask
bitwise_not(mask, negMask);
circle(mask, Point(200, 200), 100, Scalar(255), -1);
Mat md,mdBlur,mdint;

m.copyTo(md);

// All pixels outside mask set to 0
md.setTo(0, negMask);
imshow("mask image", md);
// Convert image to int
md.convertTo(mdint, CV_32S);
Size fxy(13, 13);
blur(mdint, mdBlur, fxy);
mdBlur.convertTo(mc, CV_8U);
imshow("Blur without mask", mc);
imwrite("blurwithoutmask.jpg",mc);
mask.convertTo(maskBlur, CV_32S);
// blur mask
blur(maskBlur, maskBlur, fxy);
Mat mskB;
mskB.setTo(1, negMask);
divide(mdBlur,maskBlur/255,mdBlur);

mdBlur.convertTo(mc, CV_8U);
imshow("Blur with mask", mc);
imwrite("blurwithmask.jpg",mc);
waitKey();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM