Ask Your Question
0

Efficient way to apply a minimum filter

asked 2016-06-06 02:27:04 -0600

hyder gravatar image

updated 2020-11-02 10:37:16 -0600

Hi,

I am trying to implement a function which takes an image (type: CV_64FC3) and applies two operations on it:

  1. Each RGB pixel is replaced by the minimum channel value resulting in a CV_64C1 type image.
  2. A minimum filter of size blockSize is applied.

The function is performing as required but it takes approximately 1600ms to process a single image of resolution 720 x 576 with blockSize of 15. My question is whether I can do anything to make it computationally efficient, particularly for loop. Code:

Mat darkChannel(Mat im, int blockSize)
{
    int padSize = (blockSize - 1) / 2;
    double minVal;

    Mat temp, minChannel, borderMinChannel, bgr[3];

    split(im, bgr); //split into RGB channels
    (cv::min)(bgr[0], bgr[1], temp); //find minimum between R and G channels
    (cv::min)(temp, bgr[2], minChannel); 

    copyMakeBorder(minChannel, borderMinChannel, padSize, padSize, padSize, padSize, BORDER_REPLICATE); //padding

    Mat dc = Mat::zeros(borderMinChannel.rows, borderMinChannel.cols, CV_64FC1); //create Mat to store final result
    double* p;

    for (int i = padSize; i < borderMinChannel.rows - padSize; i++)
    {
        p = dc.ptr<double>(i);
        for (int j = padSize; j < borderMinChannel.cols - padSize; j++)
        {
            minMaxLoc(borderMinChannel(Rect(j - padSize, i - padSize, 2 * padSize + 1, 2 * padSize + 1)), &minVal, NULL); //find the minimum value in a block
            p[j] = minVal; //put the minimum value in Mat
        }
    }

    dc = dc(Rect(padSize, padSize, minChannel.cols, minChannel.rows)); //remove padding to return to original size
    return dc;
}
edit retag flag offensive close merge delete

Comments

1

May be you can use erode instead of looking minimum

LBerger gravatar imageLBerger ( 2016-06-06 02:41:00 -0600 )edit
1

Yep. Your min of three channels looks fine, but the second step is the morphological erode operation, which has a nice method for it HERE. I am happy to note that it does take CV_64F images.

Tetragramm gravatar imageTetragramm ( 2016-06-06 20:19:01 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-06-06 23:29:02 -0600

hyder gravatar image

Thank you @LBerger and @Tetragramm. cv::erode works fine! Also, I noted the speed difference between images of type CV_64FC3 and CV_8UC3. So I think it would be computationally better if I keep my images in CV_8UC3 type.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-06-06 02:27:04 -0600

Seen: 2,250 times

Last updated: Jun 06 '16