Efficient way to scan an image with a window

asked 2014-05-27 11:30:48 -0600

Lorenzo_P gravatar image

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

edit retag flag offensive close merge delete

Comments

Blur filters (like yours, too) are separable. So you should compute first the mean of the pixels in the X direction to a temporary buffer, then you make a vertical blur of the temporary buffer to the final image.

The process can be further optimized by adding the next pixel and subtracting the last one during the x blur instead of making the sum of all the elements.

kbarni gravatar imagekbarni ( 2014-05-27 11:57:49 -0600 )edit