Hi, I need to develop, in an efficient way, a blur filter that considers only the pixels with value different from 0, which are provided by a mask image.
I have developed the following function, but it requires too much time, in case of a large blur kernel size (example: kernel size = 49).
void adaptiveMeanFilter(cv::Mat& source_p, cv::Mat& destination_p, cv::Mat& mask_p, int filterSize_p){
if ((filterSize_p % 2) == 0){
filterSize_p += 1;
}
float xWeightedMean = 0;
float yWeightedMean = 0;
int count = 0;
cv::Mat temp = cv::Mat::zeros(source_p.rows, source_p.cols, source_p.type());
for (int yIter = (filterSize_p / 2); yIter < source_p.rows - (filterSize_p / 2); yIter++){
for (int xIter = (filterSize_p / 2); xIter < source_p.cols - (filterSize_p / 2); xIter++){
xWeightedMean = 0;
yWeightedMean = 0;
count = 0;
for (int yIterFilter = -(filterSize_p / 2); yIterFilter <= (filterSize_p / 2); yIterFilter++){
for (int xIterFilter = -(filterSize_p / 2); xIterFilter <= (filterSize_p / 2); xIterFilter++){
if (mask_p.at<uchar>(yIter + yIterFilter, xIter + xIterFilter) != 0){
count++;
xWeightedMean += source_p.at<cv::Vec2f>(yIter + yIterFilter, xIter + xIterFilter)[0];
yWeightedMean += source_p.at<cv::Vec2f>(yIter + yIterFilter, xIter + xIterFilter)[1];
}
}
}
temp.at<cv::Vec2f>(yIter, xIter)[0] = (xWeightedMean / count);
temp.at<cv::Vec2f>(yIter, xIter)[1] = (yWeightedMean / count);
}
}
temp.copyTo(destination_p);
}
Can someone help me?
Thanks